Reputation: 147
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
Upvotes: 7
Views: 4437
Reputation: 1
Use time formatting
fmt.Println(time.Now().Format("2006-01-02 15:04:05.999999999 -0700 MST"))
Upvotes: 0
Reputation: 166616
i need output like this => 2009-11-10 23:00:00 +0000 UTC
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 (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 (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
Upvotes: 15
Reputation: 1947
All you need is:
time.Now().Truncate(0)
According to doc on time.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.
And time.Truncate() godoc says:
Truncate returns the result of rounding t down to a multiple of d (since the zero time). If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.
Upvotes: 6