-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
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-
timeText := "2020-12-26T14:36:48+00:00"
fmt.Printf("%v\n", timeText)
var t time.Time
t.UnmarshalText([]byte(timeText))
text, _ := t.MarshalText()
fmt.Printf("%v\n", string(text[:]))
What did you expect to see?
2020-12-26T14:36:48+00:00
2020-12-26T14:36:48+00:00
What did you see instead?
2020-12-26T14:36:48+00:00
2020-12-26T14:36:48Z
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.