Clocks

The clock types

The C++ Standard library provides 3 types of clock, each having their own characteristics.

system_clock

steady_clock

High resolution clock

Both the clock system_clock and high_resolution_clock have a static member of type bool, called is_steady, representing whether they are updated at steady rate or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream> #include <chrono> int main() { std::cout << "The clock 'high_resolution_clock' is " << (std::chrono::high_resolution_clock::is_steady ? "" : "not ") << "updated at a steady rate.\n\n"; std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); for(unsigned c = 0; c < 1000 * 1000; c++) {} std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); std::chrono::nanoseconds elapsedNanoSec = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start); std::cout << elapsedNanoSec.count() << " nanoseconds elapsed.\n"; return 0; }

By accessing the static members rep and period of the typedef time_point defined under the namespace of a clock, we can retrieve the type and period it uses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream> #include <chrono> int main() { std::chrono::high_resolution_clock::time_point timePoint = std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::time_point::rep nbPeriodsElapsed = timePoint.time_since_epoch().count(); std::cout << nbPeriodsElapsed << " periods of " << std::chrono::high_resolution_clock::time_point::period::num << '/' << std::chrono::high_resolution_clock::time_point::period::den << " seconds\nelapsed since the epoch used by the clock high_resolution_clock.\n"; return 0; }

Retrieving the date information about a time point

The C++ Standard Library does not directly provide tools to retrieve the current date. However, the class system_clock, and only that clock, has a method, which header is time_t to_time_t(const time_point & timePoint), that allows to convert a time point (With a time_point type having system_clock as first template argument) to a value of type time_t. We can then retrieve information about the date by using the functions localtime or gmtime of the C Standard Library to convert it to a structure of type tm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> #include <chrono> #include <time.h> // localtime int main() { // Retrieves the current time point. std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); // Converts the time_point object to a value of type time_t. time_t nbSecSinceEpoch = std::chrono::system_clock::to_time_t(now); // Retrieves a structure of type 'tm' corresponding to the time point. tm dateInfo = *localtime(&nbSecSinceEpoch); std::cout << "The current date is " << dateInfo.tm_mday << '/' << dateInfo.tm_mon+1 << '/' << dateInfo.tm_year + 1900 << ".\n"; return 0; }

Note that we can convert a value of type time_t to a std::chrono::system_clock::time_point object by using the static method time_point std::chrono::system_clock::from_time_t(time_t t).