doumo0206 2019-02-17 16:10
浏览 88
已采纳

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

In Go , for time.Now() getting timestamp trailing with m=xx.xxxx..., what does that m means?

How to remove it while printing or Is there any other ways or function to get timestamp without m

For Example:- for time.Now() getting output => 2009-11-10 23:00:00 +0000 UTC m=+0.000000001

But i need output like this => 2009-11-10 23:00:00 +0000 UTC

  • 写回答

2条回答 默认 最新

  • duandian4501 2019-02-17 16:24
    关注

    i need output like this => 2009-11-10 23:00:00 +0000 UTC


    Package time

    import "time"

    Monotonic Clocks

    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. Rather than split the API, in this package the Time returned by time.Now contains both a wall clock reading and a monotonic clock reading; later time-telling operations use the wall clock reading, but later time-measuring operations, specifically comparisons and subtractions, use the monotonic clock reading.

    The canonical way to strip a monotonic clock reading is to use t = t.Round(0).

    func (Time) Round 1.1

    func (t Time) Round(d Duration) Time
    

    Round returns the result of rounding t to the nearest multiple of d (since the zero time). The rounding behavior for halfway values is to round up. If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.

    func (Time) String

    func (t Time) String() string
    

    String returns the time formatted using the format string

    If the time has a monotonic clock reading, the returned string includes a final field "m=±", where value is the monotonic clock reading formatted as a decimal number of seconds.


    The canonical way to strip a monotonic clock reading is to use t = t.Round(0).

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t := time.Now()
        fmt.Println(t)
        fmt.Println(t.Round(0))
    }
    

    Playground: https://play.golang.org/p/nglDbs9IGdU

    Output:

    2009-11-10 23:00:00 +0000 UTC m=+0.000000001
    2009-11-10 23:00:00 +0000 UTC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?