Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quickfix: suggest time.Time.Equal instead of == #932

Closed
leventov opened this issue Feb 15, 2021 · 2 comments
Closed

quickfix: suggest time.Time.Equal instead of == #932

leventov opened this issue Feb 15, 2021 · 2 comments

Comments

@leventov
Copy link

https://golang.org/pkg/time/#Time says:

Note that the Go == operator compares not just the time instant but also the Location and the monotonic clock reading. Therefore, Time values should not be used as map or database keys without first guaranteeing that the identical Location has been set for all values, which can be achieved through use of the UTC or Local method, and that the monotonic clock reading has been stripped by setting t = t.Round(0). In general, prefer t.Equal(u) to t == u, since t.Equal uses the most accurate comparison available and correctly handles the case when only one of its arguments has a monotonic clock reading.

@leventov leventov added the needs-triage Newly filed issue that needs triage label Feb 15, 2021
@dominikh
Copy link
Owner

As the quoted passage says, it is possible to compare time.Time values directly and successfully, by stripping monotonic time and ensuring timezones are identical. I know of at least one major project that does this successfully. As such, we cannot categorically ban comparing time.Time values.

See #47 and #134 for prior discussions on this.

@dominikh dominikh added new-check wontfix and removed needs-triage Newly filed issue that needs triage labels Feb 16, 2021
@dominikh
Copy link
Owner

For ==, we can at least display a quickfix in editors that pushes users towards using Equal instead. There's not much we can do about maps, though.

@dominikh dominikh reopened this Feb 16, 2021
@dominikh dominikh removed the wontfix label Feb 16, 2021
@dominikh dominikh changed the title New check: prohibit comparing time.Time by == quickfix: suggest time.Time.Equal instead of == Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants