Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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



Hehe, 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: