Clocks

This lesson gives a brief introduction to clocks and their usage in C++ with the help of interactive examples.

We'll cover the following

The fact that there are three different types of clocks begs the question: What are the differences?

  • std::chrono::system_clock: is the system-wide real time clock (wall-clock). The clock has the auxiliary functions to_time_t and from_time_t to convert time points into calendar time

  • std::chrono::steady_clock: is the only clock to provide the guarantee that you can not adjust it. Therefore, std::chrono::steady_clock is the preferred clock to wait for a time duration or until a time point

  • std::chrono::high_resolution_clock: is the clock with the highest accuracy, but it can simply be an alias for the clocks std::chrono::system_clock or std::chrono::steady_clock

No guarantees about accuracy, starting point, and valid time range

The C++ standard provides no guarantee about the accuracy, the starting point, or the valid time range of the clocks. Typically, the starting point of std::chrono:system_clock is the 1.1.1970, the so called UNIX-epoch, while for std::chrono::steady_clock it is typically the boot time of your PC.

Accuracy and Steadiness

It is quite interesting to know which clocks are steady and what accuracy they provide. Steady means that the time points can not decrease. You can get the answers directly from the clocks.

Get hands-on with 1400+ tech skills courses.