go - time.Now() 和 time.Now().Local() 有什么区别?

标签 go time timezone

我试图了解 time.Now()time.Now().Local() 之间的区别。我首先在我的笔记本电脑上打印它们(运行 Ubuntu 18.04):

fmt.Println(time.Now())
fmt.Println(time.Now().Local())

这给了我

2018-12-23 19:57:08.606595466 +0100 CET m=+0.000583834
2018-12-23 19:57:08.606667843 +0100 CET

我不确定 m=+0.000583834 是什么。也许我的机器和 NTP 服务器之间的区别?

然后我查看了 .Now() 上的文档和 .Local() ,内容如下:

Now returns the current local time.

Local returns t with the location set to local time.

他们都返回本地时间,所以我仍然不确定有什么区别。我尝试四处搜索,但我无法真正找到明确的答案。

任何人都可以阐明这一点吗?

最佳答案

time.Now().Local() 设置时间的 Location到本地时间。 time.Now() 已设置为本地时间,因此除了 m 位外没有任何实际影响。

m 部分是 Monotonic Clock .

Operating systems provide both a “wall clock,” which is subject to changes for clock synchronization, and a “monotonic clock,” which is not. The general rule is that the wall clock is for telling time and the monotonic clock is for measuring time.

单调时钟基本上是自程序启动以来的一个简单计数。 m=+0.000583834 表示时间是程序启动后的 0.000583834 秒。

time.Now().Local() explicitly strips the monotonic clock ...

Because t.In, t.Local, and t.UTC are used for their effect on the interpretation of the wall time, they also strip any monotonic clock reading from their results. The canonical way to strip a monotonic clock reading is to use t = t.Round(0).

关于go - time.Now() 和 time.Now().Local() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53906412/

相关文章:

javascript - 有没有办法在 Javascript `Date` 对象中表示 2012 年 10 月 21 日?

apache - 如何使用 Apache 将 Go 应用程序与 MediaWiki 一起部署?

go - 管理硬编码导入路径

java - 移动应用程序中什么最重要?内存优化或快速执行

java - 为什么它总是给出相同的时间?

php - CURTIME() 获取服务器时间而不是 php.ini 中指定的时区

javascript - 如何在 Javascript 中获取时区名称(PDT、EST 等)?

go - 无法识别的导入路径

git - 致命的 : could not read Username for 'https://gitlab.com' : terminal prompts disabled

python - 比较列表理解和显式循环(3 个数组生成器比 1 个 for 循环更快)