Question

What's the correct way to get the Unix timestamp in Go? I thought it would be time.Now().Unix() but it's clearly not.

enter image description here

http://play.golang.org/p/KoJADUDxOS

enter image description here

So time.Now().Unix() tells me the unix timestamp is 1257894000 whereas my browser tells me it is 1398351437. Also www.unixtimestamp.de tells me it is 1398351704 which is what I would expect. Why is the one coming from Go so far off? I get the same results on my local machine btw.

What am I missing?

UPDATE

Ok, turns out it was really only an issue on play.golang.org. I thought I tested it locally as well but I had another look now and yes on my local machine everything is correct.

Was it helpful?

Solution

Unix() does produce a Unix timestap: The number of seconds since the epoch. I think the documentation http://golang.org/pkg/time/#Time.Unix is pretty clear here.

Maybe you are not interested in a Unix timestamp but some human readable time? In that case use Format(): http://golang.org/pkg/time/#Time.Format

And: The playground has only synthetic time. Absolute values on the playground do not correspond to UTC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top