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

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?

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置