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

time: behavior of time compare result not same #26564

Closed
isayme opened this issue Jul 24, 2018 · 2 comments
Closed

time: behavior of time compare result not same #26564

isayme opened this issue Jul 24, 2018 · 2 comments

Comments

@isayme
Copy link

@isayme isayme commented Jul 24, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.9.2

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

linux amd64

What did you do?

https://play.golang.org/p/2mwUyy__IY2

image

What did you expect to see?

with different(UTC) timezone, t1 == t2 return same true.

What did you see instead?

t1 == t2 return false when timezone is UTC.

@bronze1man
Copy link
Contributor

@bronze1man bronze1man commented Jul 24, 2018

@isayme
You can not override == operation in golang.
So with different(UTC) timezone, t1 == t2 return same true. can not be implemented only with golang code.
Please see the document:
https://golang.org/ref/spec#Comparison_operators

Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

time.Time is a struct. So it will follow the rule. So t1 == t2 return false when timezone is UTC. because the loc field is not the same.

@agnivade
Copy link
Contributor

@agnivade agnivade commented Jul 24, 2018

Please use time.Equal

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.

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

Closing since this is working as documented.

@agnivade agnivade closed this Jul 24, 2018
@golang golang locked and limited conversation to collaborators Jul 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.