4

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

Gravis
  • 30,149
  • 5
  • 23
  • 20
  • 1
    It's even stranger for me. If I run is with time zone set in the shell (`TZ=MSK go run foo.go`) it gives me UTC. By the way, if you want your dates to be UTC you can replace `+00:00` with `Z`. – Ainar-G Sep 16 '16 at 20:06
  • I don't want to, that's a json output from postgresql (and it's valid) :) – Gravis Sep 16 '16 at 20:27
  • The time package is mocked in the playground, results may be different on the real runtime - https://blog.golang.org/playground – foo Sep 19 '16 at 04:36

1 Answers1

2

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.

nemo
  • 55,207
  • 13
  • 135
  • 135
  • My english must be too poor, because that part of the doc is very unclear to me :( – Gravis Sep 16 '16 at 20:12
  • the problem is, I need to compare this timestamp. It's using a +00:00 offset, so I should compare it with `time.Date(2015, time.June, 01, 00, 0, 0, 0, time.UTC)` (my test shouldn't depend on my location). Here, the 2 times differ :( – Gravis Sep 16 '16 at 20:14
  • 1
    Then use [`time.ParseInLocation`](https://golang.org/pkg/time/#ParseInLocation) instead. – nemo Sep 16 '16 at 23:20
  • I don't parse location directly, Unmarshall does, and I don't have control on this :( – Gravis Sep 18 '16 at 19:59
  • Looks like a go bug to me. Parsing timestamps with a specified offset should REALLY not depend on my location. UTC is UTC, not matter where I am – Gravis Sep 18 '16 at 20:03
  • @Gravis And it *is* UTC, only the location name is not looked up. – nemo Sep 18 '16 at 23:49
  • I understand that, but this makes reflect.deepEqual failing when comparing structs. So again, `time.Date(2015, time.June, 01, 00, 0, 0, 0, time.UTC)` should return the exact same date, but they are still different. Do you think I should open an issue in the go repo? – Gravis Sep 20 '16 at 11:31
  • I see. In that case it is probably best to open an issue and discuss it there, yes. When you do, please update your question with the link to the issue. – nemo Sep 20 '16 at 14:40