英文:
What's the correct way to get a Unix timestamp in Go
问题
在Go语言中获取Unix时间戳的正确方法是什么?我原以为应该使用time.Now().Unix()
,但显然不是这样。
所以time.Now().Unix()
告诉我Unix时间戳是1257894000,而我的浏览器告诉我是1398351437。另外,www.unixtimestamp.de告诉我是1398351704,这是我所期望的结果。为什么Go语言返回的结果差距这么大?顺便说一下,我在本地机器上也得到了相同的结果。
我漏掉了什么吗?
更新
好的,事实证明这只是在play.golang.org上的问题。我原以为我也在本地测试过,但现在我再看一下,是的,在我的本地机器上一切都是正确的。
英文:
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.
http://play.golang.org/p/KoJADUDxOS
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.
答案1
得分: 3
Unix()
函数返回一个Unix时间戳,即自纪元以来的秒数。我认为文档http://golang.org/pkg/time/#Time.Unix 在这方面解释得很清楚。
也许你对Unix时间戳不感兴趣,而是想要一些可读的时间格式?在这种情况下,可以使用Format()
函数:http://golang.org/pkg/time/#Time.Format
另外:Playground只有合成时间。Playground上的绝对值与UTC时间不对应。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论