donglian4770
2018-12-23 19:06
浏览 845
已采纳

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

I'm trying to understand what the difference is between time.Now() and time.Now().Local(). I started by printing them out on my laptop (running Ubuntu 18.04):

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

which gives me

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

I'm not sure what the m=+0.000583834 is. Maybe the difference between my machine and the NTP servers?

I then checked out the docs on .Now() and .Local(), which read:

Now returns the current local time.

and

Local returns t with the location set to local time.

Both of them return local time, so I'm still unsure what the difference is. I tried searching around, but I couldn't really find definite answers.

Could anyone shed some light on this?

图片转代码服务由CSDN问答提供 功能建议

我正试图了解 time.Now()与 <代码> time.Now()。本地()。 我首先在笔记本电脑(运行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服务器之间有区别?

然后我在 上签出了文档。现在 () .Local() ,其内容为:

现在返回当前本地时间。

Local返回t,并将位置设置为本地时间。

两者都返回本地时间 ,所以我仍然不确定有什么区别。 我尝试四处搜寻,但找不到真正的答案。

有人可以对此有所了解吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhu3548 2018-12-23 19:22
    已采纳

    time.Now().Local() sets the time's Location to local time. time.Now() is already set to local time, so there's no net effect except that m bit.

    The m portion is the 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.

    A monotonic clock is basically a simple count since the program started. m=+0.000583834 says that time is 0.000583834 seconds after the program started.

    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).

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题