What you see is the "default" formatting, e.g. when printed like fmt.Println(ts)
, which is the result of Time.String()
. Quoting from Time.String()
:
String returns the time formatted using the format string
"2006-01-02 15:04:05.999999999 -0700 MST"
As you can see, it contains the zone offset and the zone name in the end.
Also note that you used time.Parse()
which documents that:
In the absence of a time zone indicator, Parse returns a time in UTC.
When parsing a time with a zone offset like -0700, if the offset corresponds to a time zone used by the current location (Local), then Parse uses that location and zone in the returned time. Otherwise it records the time as being in a fabricated location with time fixed at the given zone offset.
Since on the Go Playground the local time is set to UTC
whose offset matches the offset of the time you parse, that zone will be used, so printing it on the Go Playground you'll see UTC
.
When you parse it locally (on your computer) and the local time zone is not UTC or another zone that has 0 offset, as per the doc, that location is recorded as a fabricated location with the name having the offset.
So when you parse and print that time locally, since the default format includes both the zone offset and the zone name, and since both are +0000
, you'll see +0000
printed twice.
Worry not, the parsed time is of course correct:
ts, err := time.Parse("2006-01-02T15:04:05-0700", "2021-01-19T16:20:04+0000")
if err != nil {
panic(err)
}
fmt.Println(ts)
fmt.Println(ts.Format("2006-01-02 16:04:05 -0700"))
fmt.Println(ts.Format("2006-01-02 16:04:05 MST"))
This outputs on the Go Playground:
2021-01-19 16:20:04 +0000 UTC
2021-01-19 16:20:04 +0000
2021-01-19 16:20:04 UTC
Locally (having CET
zone) it outputs:
2021-01-19 16:20:04 +0000 +0000
2021-01-19 16:20:04 +0000
2021-01-19 16:20:04 +0000
Note that if you'd parse a time with a different zone offset (not being 0), you'll see the same on the Go Playground too. For example:
ts, err := time.Parse("2006-01-02T15:04:05-0700", "2021-01-19T16:20:04+1100")
if err != nil {
panic(err)
}
fmt.Println(ts)
fmt.Println(ts.Format("2006-01-02 16:04:05 -0700"))
fmt.Println(ts.Format("2006-01-02 16:04:05 MST"))
This outputs both on the Go Playground and locally for me (with CET
zone):
2021-01-19 16:20:04 +1100 +1100
2021-01-19 16:20:04 +1100
2021-01-19 16:20:04 +1100