Tech News
← Back to articles

Time in C++: Inter-Clock Conversions, Epochs, and Durations

read original related products more articles

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 is designed to be type-safe, and it enforces some rules that prevent accidental misuse. Once you understand why those rules exist, time handling in C++ starts to feel much safer — and your tests get more reliable too. And

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. doesn’t stop you from doing conversions just to be pedantic — it discourages them because many of those conversions simply don’t make sense or can silently introduce subtle bugs.

Once you understand why these conversions are tricky, and why the library forces you to be explicit about them, the design choices in click into place. And more importantly, you start avoiding whole classes of timing bugs — both in production and in tests.

Let’s jump right into the details.

Clock Epochs: Why “Zero” Isn’t Universal

A time_point in is always measured relative to some epoch. But here’s the catch: each clock defines its own epoch.

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