(Edit/update): Thanks to @djd for pointing out that we can skip all the JSON/struct decoding business; the key issue is with time.Parse
.
The same issue comes up here where the Location
is "empty" rather than UTC (I would've expected UTC based on the docs: https://golang.org/pkg/time/#Parse
"In the absence of a time zone indicator, Parse returns a time in UTC."
Here's the example code: https://play.golang.org/p/pb3eMbjSmv
package main
import (
"fmt"
"time"
)
func main() {
// Ignoring the err just for this example's sake!
parsed, _ := time.Parse(time.RFC3339, "2017-08-15T22:30:00+00:00")
fmt.Printf("String(): %v
", parsed.String())
fmt.Printf("Location(): %v
", parsed.Location())
}
which outputs
String(): 2017-08-15 22:30:00 +0000 +0000
Location():
So while the offset of the time.Time
's Location
appears to be correct, its timezone name is just an empty string. Running in on other machines (and The Go Playground) give the expected "UTC" location.
[Original post]:
When decoding a timestamp field from JSON into a struct
on my local OS X machine, the Location
of the time.Time
field is "empty" rather than UTC. This is problematic for me running unit tests locally (vs. on a CI server where the Location
is being set correctly to be UTC).
When I run that on my machine, I see
go run main.go
TimeField.String(): 2017-08-15 22:30:00 +0000 +0000
TimeField.Location():
So while the offset of the time.Time
's Location
appears to be correct, its timezone name is just an empty string. This is using Go 1.5:
go version
go version go1.5 darwin/amd64