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: Marshal/Unmarshal Text/JSON - inconsistent results with +00:00 offset times #43501
Comments
note: the playground version is go1.15.6. See #12141, #10089, #17875, and others. I don't believe this is a bug, nor should MarshalBinary behave similarly to MarshalJSON/MarshalText. |
The first two both relate to the zero Time. As of go1.15.6 the examples in those issues preserve the value across marshal/unmarshal (eg https://play.golang.org/p/VXUScwoGAH). This may have been fixed by 5fbf35d. #17875 does appear related to this issue. There's a subtle difference, in that it focuses on the in-memory representation rather than the marshaled output. To be clear, I know that Go has different representations for UTC and "local time with 0s offset from UTC" and that these will not compare equal with Additionally, the suggestion there to use
I'm not sure I understand this point. Go's Time binary encoding has distinct representations for UTC and "local time with 0s offset from UTC", as far as I can see: https://play.golang.org/p/Wm0zG75c93a As shown in that example, those two representations are preserved across a binary marshal/unmarshal pair. Am I misunderstanding what you're saying here?
To clarify, I'm not saying that I expect MarshalText/MarshalJSON to behave similarly to MarshalBinary in general. I mentioned MarshalBinary because it shows that there's no technical reason for UTC and "local time with 0s offset from UTC" to marshal into the same output. The in-memory representations differ, and this is demonstrated by MarshalBinary outputting them in a way that preserves that difference. |
What version of Go are you using (
go version
)?Go Playground (go1.14.13)
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Go Playground (go1.14.13)
What did you do?
https://play.golang.org/p/Mk0RB83WgR-
What did you expect to see?
What did you see instead?
Details
See extended example at: https://play.golang.org/p/ABJgLlT-XvG.
That shows how UnmarshalText and UnmarshalJSON both treat times with "+00:00" offset as distinct from those with an offset of "Z". The "+00:00" unmarshals as a local time with 0s offset from UTC, while the "Z" case unmarshals as a UTC time.
MarshalText and MarshalJSON, on the other hand, treat local times with 0 offset from UTC as identical to UTC times: both are output in "Z" form.
Compare with UnmarshalBinary and MarshalBinary, which are symmetrical for this case. A local time with 0s offset from UTC is preserved as such after passing through MarshalBinary and UnmarshalBinary.
I'd expect text and JSON Marshal/Unmarshal to be symmetrical for this case too.
The text was updated successfully, but these errors were encountered: