Golang time zone parsing not returning the correct zone on ubuntu server

827 views Asked by At

Using Ubuntu 12.04

clientSendTimeHeaderFormat := "2006-01-02T15:04:05-0700"
ctx := "2015-04-01T10:04:00-0700"
clientSendTime, err  := time.Parse(clientSendTimeHeaderFormat, ctx)
name, offset := clientSendTime.Zone()

On the server, the name returns empty, whereas offset is correct at -25200. The clientSendTime prints out as "2015-04-01 10:04:00 -0700 -0700".

Running it locally on my mac returns both name and offset correctly. The name returns the correct zone "PDT". Locally the clientSendTime prints out as "2015-04-01 10:04:00 -0700 PDT"

I also copied over the /usr/share/zoneinfo folder from the server when running locally to make sure it wasnt due to differences in that.

Anyone know what might be causing these differences?

1

There are 1 answers

0
Jon Skeet On

Okay, so I think I know why this is happening - but not a decent fix for it.

The documentation for Parse says:

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.

As you're in Pacific time, the zone offset does happen to match your location - so it can use your local time zone "name" (ick; PDT isn't actually a time zone name, but we'll leave that to one side). When your server parses it, it has to fabricate the location - that's why you're seeing "-0700 -0700".

Fundamentally, you can't determine the time zone just from an offset - there can be multiple time zones with the same offset at the same moment in time, so you can't predict what the offset will be in the original time zone at any other time. I suggest you just record what you actually know - i.e. the offset - and don't try to infer information that isn't actually present.