Reputation: 1532
I ran the go code following.
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
i, err := strconv.ParseInt("1405544146", 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0).Format(time.RFC3339)
fmt.Println(tm)
fmt.Println(time.RFC3339)
}
Then the result on Linux is
2014-07-16T20:55:46Z
2006-01-02T15:04:05Z07:00
and on macOS is
2014-07-17T05:55:46+09:00
2006-01-02T15:04:05Z07:00
It's the same time but formatted results are different. Do you know the reason?
Upvotes: 2
Views: 4191
Reputation: 1
This problem is due to time location on golang.
Default time location of Linux golang is "UTC" and MacOS golang is "Local". ("Local" use time location of os.)
You can check time location.
fmt.Println(time.Local) // Result of MacOS is "Local"
If you want to get "UTC" results from MacOS, you should use "In" function.
lo, _ := time.LoadLocation("UTC")
tmUTC := time.Unix(1405544146, 0).In(lo).Format(time.RFC3339)
if you run code on MacOS, you can get result like linux.
i, err := strconv.ParseInt("1405544146", 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0).Format(time.RFC3339)
fmt.Println(tm)
lo, _ := time.LoadLocation("UTC")
tmUTC := time.Unix(i, 0).In(lo).Format(time.RFC3339)
Upvotes: 0
Reputation: 22147
The reason two different OS's produce differing outputs for the same input is because of the OS's timezone configuration. The timezone on the Linux box does not appear to be set.
go
will attempt to pick up the timezone from the local OS. If not available, it defaults to UTC
. If you want to get consistent output from both MacOS and Linux - ensure the timezone is the same. To do this explicitly, set TZ
environment variable. This will work on Linux & MacOS etc.
$ go build -o gotime ./main.go
$ uname -s
Linux
$ TZ=CET ./gotime
2014-07-16T22:55:46+02:00
2006-01-02T15:04:05Z07:00
$ TZ="" ./gotime
2014-07-16T20:55:46Z
2006-01-02T15:04:05Z07:00
$ uname -s
Darwin
$ TZ=CET ./gotime
2014-07-16T22:55:46+02:00
2006-01-02T15:04:05Z07:00
$ TZ="" ./gotime
2014-07-16T20:55:46Z
2006-01-02T15:04:05Z07:00
Upvotes: 0
Reputation: 1610
2014-07-16T20:55:46Z
and 2014-07-17T05:55:46+09:00
different zone time.
time.RFC3339 is const. https://golang.org/pkg/time/#RFC3339.
const (
// ...
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
// ...
)
go uses Numeric time or Z triggers the ISO 8601.
Numeric time zone offsets format as follows:
-0700 ±hhmm
-07:00 ±hh:mm
-07 ±hh
and Replacing the sign in the format with a Z triggers the ISO 8601 behavior of printing Z instead of an offset for the UTC zone.
Z0700 Z or ±hhmm
Z07:00 Z or ±hh:mm
Z07 Z or ±hh
Upvotes: 1
Reputation: 166915
Don't jump to conclusions. Examine all the evidence. For instance, consider the local time zone.
import "time"
func Unix(sec int64, nsec int64) Time
Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC.
For example,
package main
import (
"fmt"
"runtime"
"strconv"
"time"
)
func main() {
i, err := strconv.ParseInt("1405544146", 10, 64)
if err != nil {
panic(err)
}
t := time.Unix(i, 0)
fmt.Println(t)
fmt.Println(t.Format(time.RFC3339))
fmt.Println(time.RFC3339)
fmt.Println(runtime.GOOS, runtime.GOARCH, runtime.Version())
}
Playground: https://play.golang.org/p/UH6o57YckiV
Output (Playground):
2014-07-16 20:55:46 +0000 UTC
2014-07-16T20:55:46Z
2006-01-02T15:04:05Z07:00
nacl amd64p32 go1.12
Output (Linux):
2014-07-16 16:55:46 -0400 EDT
2014-07-16T16:55:46-04:00
2006-01-02T15:04:05Z07:00
linux amd64 devel +5b68cb65d3 Thu Mar 28 23:49:52 2019 +0000
Different time zones (UTC versus EDT) so different formatted dates and times.
In your examples you have 2014-07-16T20:55:46Z
and 2014-07-17T05:55:46+09:00
, different time zones so different formatted dates and times.
Upvotes: 1