C++20 changes to std::chrono you might not be aware of: clocks and more
published at 30.03.2026 18:26 by Jens Weller
Save to Instapaper Pocket
I was aware of that C++20 extended std::chrono by adding calendar and time zone support. But I wasn't aware about that it also brought a set of new clock classes and better support for clocks in general.
In its original release, chrono comes with two types of clocks: steady_clock is a stop watch like interface from which you can query time_points, while system_clock represents a clock knowing the time of day. You can measure times with both, but steady_clock will be more accurate. A third clock with C++11 introduced is high_resolution_clock, representing a clock with the highest resolution possible on a given system. Though when you read the entry about this on cppreference, you'll also find a paragraph about how the original author Howard Hinnant would favor deprecating this type. Howard Hinnant also gave a great keynote on chrono at Meeting C++ 2019.
With C++20 there are now 5 new clock types:
- utc_clock
- a clock for using UTC times
- tai_clock
- a clock for internationa atomic time
- gps_clock
- GPS has its own unique time
- file_clock
- how the file system sees time?
- local_t
- a pseudo clock to represent local time
The first three represent certain specified time systems. In common they have a specific but not shared start date and the last two - atomic time and gps time do not have or support leap seconds. While file_clock gives one access on how the file system sees time.
Then C++20 also brings helper functions for writing code for clocks with is_clock and clock_cast. With clock_cast one can cast time_points from one clock time to another clocks time. For this also the clock_time_conversion trait is introduced.
Join the Meeting C++ patreon community!
This and other posts on Meeting C++ are enabled by my supporters on patreon!