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: Zero value of time.Time does not remain equal under JSON marshal/unmarshal #12141

Closed
daishi opened this issue Aug 13, 2015 · 4 comments
Closed

Comments

@daishi
Copy link

@daishi daishi commented Aug 13, 2015

Please see:

http://play.golang.org/p/VXUScwoGAH

I can see and understand why the final equality test returns false, but at the same time given that the default timezone is Z, would nevertheless expect the result to be true.

@cespare
Copy link
Contributor

@cespare cespare commented Aug 13, 2015

The locations aren't the same, as you can see from your printouts.

This is why Time.Equal exists. See http://play.golang.org/p/fEfW-7nfZV.

@daishi
Copy link
Author

@daishi daishi commented Aug 13, 2015

I think you are missing my point. The time.Time documentation specifically says:

The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC.

therefore one would reasonably expect a zero-value time.Time (as constructed by time.Time{}) to compare equal to the one in which the timezone is explicitly UTC rather than nil.

@cespare
Copy link
Contributor

@cespare cespare commented Aug 13, 2015

Here are some other quotes from the time docs:

Note that the Go == operator compares not just the time instant but also the Location. 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

Two times can be equal even if they are in different locations. For example, 6:00 +0200 CEST and 4:00 UTC are Equal. This comparison is different from using t == u, which also compares the locations.

This issue is more or less a duplicate of #10089.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 13, 2015

Yes, working as expected.

@mikioh mikioh changed the title Zero value of time.Time does not remain equal under JSON marshal/unmarshal time: Zero value of time.Time does not remain equal under JSON marshal/unmarshal Aug 13, 2015
@golang golang locked and limited conversation to collaborators Aug 22, 2016
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.