TAGS :Viewed: 6 - Published at: a few seconds ago

[ Why go is parsing my timestamp with Local location instead of UTC ]

I don't understand this behaviour (or the doc) of this: https://play.golang.org/p/vz2UTz-3Yy

On the playground, it return the expected results:

t = 2015-06-01 00:00:00 +0000 UTC
t.Location() = UTC
parsed = 2015-06-01 00:00:00 +0000 UTC
parsed.Location() = UTC

On my mac, I get:

t = 2015-06-01 00:00:00 +0000 +0000
t.Location() =
parsed = 2015-06-01 00:00:00 +0000 +0000
parsed.Location() =

The problem is, if I create a date with

time.Date(2015, time.June, 01, 00, 0, 0, 0, time.UTC)

the 2 times are different, because one has a location ("UTC"), and the other not (or "Local"). I'm bit lost here.

Thanks

Answer 1


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. [time.Parse]

t.Location (a name) is only set when the local offset matches the offset that is in the parsed date string. You probably have a different time zone set.

So the offset is recorded but the location is not looked up.