-2

I've tried the following format which should catch the +0000 timezone offset as I read https://golang.org/pkg/time/#pkg-constants:

ts, err := time.Parse("2006-01-02T15:04:05-0700", "2021-01-19T16:20:04+0000")

But that looks rather strange when the result ist printed (no error):

2021-01-19 16:47:00 +0000 +0000

UPDATE

Running https://play.golang.org/p/wHBYz7iKnLT locally (OSX 11.1) gives the strange result:

❯ gor time.go 
2021-01-19 16:20:04 +0000 +0000 <nil>
3
  • 1
    I copied & pasted your sample string and format string and it works as expected in playground, if it's not working for you, please show the full code to reproduce the issue. play.golang.org/p/wHBYz7iKnLT
    – Adrian
    Jan 19, 2021 at 18:40
  • You call it "strange" twice - what's strange about it exactly?
    – Adrian
    Jan 19, 2021 at 20:26
  • It looked to me like double timezone, but @icza explained it very well
    – andig
    Jan 20, 2021 at 7:30

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.