Home » Time in C++20: Introduction to Chrono Terminology

Time in C++20: Introduction to Chrono Terminology

by admin
Time in C++20: Introduction to Chrono Terminology

Time in C++20: Introduction to Chrono Terminology

Essentially, the time zone functionality (C++20) is based on the calendar functionality (C++20), which in turn is based on the chrono functionality (C++11). Therefore, this basic chrono terminology begins with the three C++11 components of time, duration, and clock.


Rainer Grimm has been working as a software architect, team and training manager for many years. He enjoys writing articles on the programming languages ​​C++, Python and Haskell, but also enjoys speaking frequently at specialist conferences. On his blog Modern C++ he deals intensively with his passion C++.

A time has a starting point, called the epoch, and an additional period of time since the epoch.A Duration is the difference between two points in time. The number of ticks of the clock defines the length of time.One Clock has a starting point (epoch) and a tick to calculate the current point in time.

Subtracting times results in a time period. You get a new point in time when you add a period of time to a point in time. One year is the typical accuracy of the clock and the measure of the length of time.

I will use the three concepts to introduce the lifetime of the father of the C programming language: Dennis Ritchie, who died in 2011. For simplicity’s sake, I’m only interested in the years.

Dennis Ritchie was 70 years old. The birth of Christ is the epoch. If I combine the epoch with the time period, I get the times 1941 and 2011. Subtracting the time 1941 from 2011 gives the time period in annual precision.

See also  C++20: More details on module support from the big three compilers

C++11 has three clocks std::chrono::system_clock, std::chrono::steady_clock and std::chrono::high_resolution_clock. The time duration can be positive and negative. Each of the three clocks has a member function now that returns the current time.

More about the time, duration and clock can be found in my previous articles:

C++20 adds new components to the Chrono library:

Die Time of day is the length of time since midnight, divided into hours, minutes, seconds and fractions of a second.calendar represents various calendar dates such as year, month, day of the week or the nth day of a week.One Time zone represents a specific time in a specific geographical area.A zoned time combines a point in time with a time zone. Time is a mystery

Honestly, time is a mystery to me. On the one hand, each of us has an intuitive idea of ​​time, but on the other hand, it is extremely difficult to define it formally. For example, the three components of time, duration and clock depend on each other.

First, I would like to introduce the basic data types and literals.

For the sake of completeness, this section contains the basic data types and literals of the previous C++ standards.


In addition to the wall clock std::chrono::system_clock, the monotonic clock std::chrono::steady_clock and the most precise clock std::chrono::high_resolution_clock in C++11, C++20 knows five other clocks. The following table shows the properties of all C++ clocks and their epoch.

The std::chrono::steady_clock and std::chrono::file_clock clocks have an implementation-specific epoch. The epochs of std::chrono::system_clock, std::chrono::gps_clock, std::chrono::tai_clock and std::chrono::utc_clock start at 00:00:00. std::chrono::file_clock is the clock for file system entries.

See also  More details on formatting custom data types in C++20

Additionally, C++11 supports the std::chrono::high_resolution_clock. This clock is not implemented on all implementations and is simply an alias for std::chrono::steady_clock or std::chrono::high_resolution_clock.

A point in time can be converted between the clocks.

Conversion of times between clocks

The std::chrono::clock_cast function converts times between clocks that have an epoch. These are the clocks std::chrono::system_clock, std::chrono::utc_clock, std::chrono::gps_clock and std::chrono::tai_clock. Also, std::chrono::file_clock supports conversion.

The following program converts the time 2021-8-5 17:00:00 between the different clocks.

// convertClocks.cpp


int main() {

std::istringstream{“2021-8-5 17:00:00”}
>> std::chrono::parse(“%F %T”s, timePoint);

auto timePointUTC
= std::chrono::clock_cast<:chrono::utc_clock>(timePoint);
std::cout (timePoint);
std::cout (timePoint);
std::cout (timePoint);
std::cout (timePoint);
std::cout The std::chrono::parse function parses the Chrono object from the stream. In the following lines, std::chrono::clock_cast converts the timePoint to the specified clock. The following line displays the time, specifying the date (%F), local time representation (%X), and time zone abbreviation (%Z). One of my next articles will go deeper into the format string.

The output will surprise some: GPS time is 18 seconds ahead of UTC time. TAI time is 37 seconds ahead of UTC time and 19 seconds ahead of GPS time.

In my next article I will continue my journey through data types with durations and points in time.


To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy