什么是时间戳中的“m”以及如何在没有“m”的情况下获取时间戳?

Arv*_*ndh 3 timestamp go

在 Go 中,对于 time.Now() 获取尾随m=xx.xxxx... 的时间戳,那m是什么意思?

如何在打印时删除它或者有没有其他方法或功能可以在没有m 的情况下获取时间戳

例如:- for time.Now() 获取输出 => 2009-11-10 23:00:00 +0000 UTC m=+0.000000001

但我需要这样的输出 => 2009-11-10 23:00:00 +0000 UTC

pet*_*rSO 7

我需要这样的输出 => 2009-11-10 23:00:00 +0000 UTC


打包时间

导入“时间”

单调时钟

操作系统提供了一个“挂钟”,它受时钟同步变化的影响,以及一个“单调时钟”,它不是。一般规则是挂钟是用来报时的,单调钟是用来测量时间的。不是拆分 API,在这个包中 time.Now 返回的 Time 包含挂钟读数和单调时钟读数;后来的计时操作使用挂钟读数,但后来的时间测量操作,特别是比较和减法,使用单调时钟读数。

去除单调时钟读数的规范方法是使用 t = t.Round(0)。

func (Time) Round 1.1

func (t Time) Round(d Duration) Time
Run Code Online (Sandbox Code Playgroud)

Round 将四舍五入 t 的结果返回到最接近的 d 倍数(从零时间开始)。中间值的舍入行为是向上舍入。如果 d <= 0,Round 返回 t 去除任何单调时钟读数,否则保持不变。

func(时间)字符串

func (t Time) String() string
Run Code Online (Sandbox Code Playgroud)

字符串返回使用格式字符串格式化的时间

如果时间具有单调时钟读数,则返回的字符串包括最后一个字段“m=±”,其中 value 是格式化为十进制秒数的单调时钟读数。


去除单调时钟读数的规范方法是使用 t = t.Round(0).

例如,

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t)
    fmt.Println(t.Round(0))
}
Run Code Online (Sandbox Code Playgroud)

游乐场:https : //play.golang.org/p/nglDbs9IGdU

输出:

2009-11-10 23:00:00 +0000 UTC m=+0.000000001
2009-11-10 23:00:00 +0000 UTC
Run Code Online (Sandbox Code Playgroud)


Sau*_*ash 6

所有你需要的是:

\n\n
    time.Now().Truncate(0)\n
Run Code Online (Sandbox Code Playgroud)\n\n

根据 time.String() 上的文档

\n\n
\n

如果时间具有单调时钟读数,则返回的字符串包括最终字段“m=\xc2\xb1”,其中值是格式化为十进制秒数的单调时钟读数。

\n
\n\n

time.Truncate() godoc 说:

\n\n
\n

截断返回将 t 向下舍入到 d 的倍数(从零时间开始)的结果。如果 d <= 0,则截断返回 t,去除任何单调时钟读数,但其他方面保持不变。

\n
\n