liveBook · Manning

Monotonic clock

As explained in 100 Go Mistakes and How to Avoid Them

A monotonic clock is a time source that advances steadily for measuring elapsed durations without being adjusted backward by wall-clock changes.

A monotonic clock advances steadily for measuring elapsed durations. Unlike a wall clock, it is not moved backward or forward by time jumps such as NTP corrections, although its frequency may be adjusted. section 10.3.2

Go's time.Time can hold both wall time and monotonic time. time.Now() supplies both, but JSON unmarshaling restores only wall time, so comparing the original and decoded values with == can unexpectedly return false. section 10.3.2

A misleadingly unequal timestamp

The original value prints a monotonic suffix such as m=+0.000338660, while the unmarshaled value does not. Their wall times match, but their monotonic components differ; == therefore prints false.

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Defines both clock types, traces monotonic data loss through JSON, and contrasts ==, Equal, and Truncate(0).
Examples
Uses an Event JSON round trip, printed clock components, Equal, and Truncate(0) to expose and correct comparison behavior.
What you can do after
Diagnose/debug with it by distinguishing wall-clock changes from monotonic data and selecting Equal or Truncate(0) appropriately.
How it connects
Connects monotonic clocks to wall clocks, time.Time, JSON marshaling, JSON unmarshaling, and time.Duration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.