By now in this series, we’ve spent time looking at the major standard clocks and their behavior. We’ve talked about wall-clock time, monotonic clocks, and the myths around “high resolution”. Today, we are going to talk about a subtle area: how clocks relate to each other, how epochs differ, and what happens when you - need to - convert durations.
It sounds simple, right? A timestamp is a timestamp, and a duration is just a number of seconds - or other time units. But
It sounds simple, right? A timestamp is a timestamp, and a duration is just a number of seconds - or other time units. But when it comes to converting between clocks, the reality is much messier. Different clocks have different epochs, different guarantees, and sometimes completely different purposes.
Once you understand why these conversions are tricky, and why the library forces you to be explicit about them, the design choices in
Let’s jump right into the details.
Clock Epochs: Why “Zero” Isn’t Universal
A time_point in
std::chrono::system_clock uses the Unix epoch (1 January 1970 UTC).
uses the Unix epoch (1 January 1970 UTC). std::chrono::steady_clock uses an unspecified monotonic epoch. Its zero might be system boot time, or something entirely different.
uses an unspecified monotonic epoch. Its zero might be system boot time, or something entirely different. std::chrono::high_resolution_clock is usually just an alias to either system_clock or steady_clock .
... continue reading