4 ms·
This article includes a Go code example: start := time.Now() // do something spent := time.Now().Sub(start) It's worth noting that the Go time lib
by makeworld 4y ago
This article includes a Go code example:
start := time.Now()
// do something
spent := time.Now().Sub(start)
It's worth noting that the Go time library is specifically designed so that computer clocks running backwards won't cause `spent` to be a negative duration. A monotonic clock that only ticks forward is used for time comparison and subtractions.
Source: https://pkg.go.dev/time#hdr-Monotonic_Clocks https://pkg.go.dev/time#hdr-Monotonic_Clocks
- klabb3 4y agoHehe, story time: I was using exactly this logic to detect hibernation/sleep, especially for laptops. I was surprised when it never triggered, and printed the time, which indeed showed that a long time had passed. So why didn't it trigger? Because IFF both time stamps have monotonic component (internal, not visible on print) then the monotonic stamp is used. Confused me a lot. One man's bug is another man's feature.