英文:
How to get Timestamp of UTC time with Golang?
问题
我想将UTC时间字符串转换为Unix时间戳。
我这样做:
fmt.Printf("%s %d\n", time.Now().String(), time.Now().Unix())
fmt.Printf("%s %s\n", time.Now().UTC().String(), time.Now().UTC().Unix())
但是我得到了相同的Unix时间戳 1499018765
> 2017-07-02 20:06:05.5582802 +0200 CEST 1499018765
>
> 2017-07-02 18:06:05.791337 +0000 UTC 1499018765
英文:
I want to convert UTC time string to unix timestamp.
I do this
fmt.Printf("%s %d\n", time.Now().String(), time.Now().Unix())
fmt.Printf("%s %s\n", time.Now().UTC().String(), time.Now().UTC().Unix())
But I got same unix timestamp 1499018765
> 2017-07-02 20:06:05.5582802 +0200 CEST 1499018765
>
> 2017-07-02 18:06:05.791337 +0000 UTC 1499018765
答案1
得分: 93
Unix()函数始终返回自1970年1月1日UTC以来经过的秒数。因此,无论你给它time.Now()还是time.Now().UTC(),它都是相同的UTC时间,只是在地球上的不同位置。你得到的结果是正确的。
英文:
Unix() always returns the number of seconds elapsed since January 1, 1970 UTC. So it does not matter whether you give it time.Now() or time.Now().UTC(), it is the same UTC time, just in different places on Earth. What you get as the result is correct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论