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

When 'zero' isn't zero: conversions spark chaos and comedy

TLDR: C++ time says each clock has its own 'zero,' and C++20’s clock_cast only converts related clocks. Comments split between safety lovers and frustrated realists, with tales of broken logs, drifted offsets, and jokes that time depends on the moon—important because bad conversions cause real bugs.

C++’s latest time talk says clocks don’t share the same “zero,” and the internet responded with confessions, rants, and memes. The article explains why the language’s time library <chrono> blocks casual clock-mixing and only allows safe conversions between related clocks via clock_cast. The crowd? Split right down the middle.

On one side, folks cheer the strict rules: fewer bugs, less mystery, more reliable tests. On the other, engineers are screaming that real-world systems don’t care about textbook purity. barishnamazov drops a war story about “manually correlating” a strict timer with wall-clock time—worked in testing, then drifted into chaos during long runs. loeg calls out a design gap: you can’t make a clock tied to the CPU’s cycle counter because conversion requires compile-time math, not runtime smarts. cyberax sums up the mood with a spicy “C++ chrono is weird,” accusing it of being both too abstract and not abstract enough.

Meanwhile, jokes fly: “Time depends on the phase of the moon,” “friendship ended with steady_clock,” and “time is a flat circle” make the rounds. Whether you love the type safety or hate the handcuffs, the message is clear: mixing clocks isn’t just tricky—it’s drama, and your tests are the collateral damage.

Key Points

  • Different std::chrono clocks use distinct epochs, making cross-clock time_point comparisons meaningless.
  • std::chrono::system_clock uses the Unix epoch; steady_clock has an unspecified monotonic epoch; high_resolution_clock is usually an alias.
  • C++20 introduced std::chrono::clock_cast and clock_time_conversion to enable defined inter-clock conversions.
  • Standard-supported conversions exist between system_clock, utc_clock, tai_clock, gps_clock, and file_clock due to known offsets.
  • For clocks without a defined relationship (e.g., system_clock vs. steady_clock), only manual correlation is possible and results are approximate.

Hottest takes

"I fell into the exact trap of manually correlating steady_clock and system_clock" — barishnamazov
"you can't implement a std::chrono clock where the count() unit is the native CPU's cycle counter" — loeg
"C++ chrono is weird. It's both over-abstracted and yet has some thoughtless features" — cyberax
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.