Modern C++ (C++11 and later) provides a powerful library for handling time measurement, duration calculations, timestamps, and date operations: std::chrono. This library is used for high-precision timing, performance profiling, scheduling, and working with real calendar dates.

In this article, we will explore std::chrono in detail, including different clock types, durations, time points, conversions, and the C++20 date API with practical examples.


1) Basic std::chrono Types

Category Description
duration Represents the amount of time between two time points
time_point Represents a specific point in time
clock Mechanism used to measure and track time

2) Working with Durations

Duration objects represent time units such as seconds, milliseconds, microseconds, etc.


using namespace chrono;

seconds s(5);
milliseconds ms(2500);

cout << s.count();   // 5
cout << ms.count();  // 2500

Unit conversion:


auto duration = 3s + 1500ms;
cout << duration.count(); // 4500 (milliseconds)

3) High-precision Timing (high_resolution_clock)

Commonly used for benchmarking and performance profiling.


using namespace chrono;

auto start = high_resolution_clock::now();

// Code to measure
long total = 0;
for (int i = 0; i < 1000000; i++)
    total += i;

auto end = high_resolution_clock::now();

auto elapsed = duration_cast<milliseconds>(end - start);

cout << "Elapsed: " << elapsed.count() << " ms";

4) system_clock — Real-world Time

system_clock represents the current wall-clock time.


using namespace chrono;

auto now = system_clock::now();

time_t t = system_clock::to_time_t(now);
cout << "Now: " << ctime(&t);

5) steady_clock — Monotonic Clock

steady_clock never goes backward and is unaffected by system time adjustments. It is the most reliable clock type for time measurement.


using namespace chrono;

auto t1 = steady_clock::now();
auto t2 = steady_clock::now();

auto diff = duration_cast<microseconds>(t2 - t1);
cout << diff.count();

6) C++20 Date API: year_month_day, sys_days

With C++20, calendar date support was officially added to std::chrono.


using namespace chrono;

year_month_day date = 2024y / February / 15;

cout << (int)date.year() << endl;
cout << (unsigned)date.month() << endl;
cout << (unsigned)date.day() << endl;

Converting a date to system_clock:


using namespace chrono;

sys_days day(2024y / March / 1);
auto tp = system_clock::time_point(day);

time_t t = system_clock::to_time_t(tp);
cout << ctime(&t);

7) Calculating Differences Between Dates


using namespace chrono;

sys_days d1 = 2024y / January / 1;
sys_days d2 = 2024y / February / 1;

auto diff = d2 - d1;

cout << diff.count() << " days";

8) Timing Operations (sleep_for, sleep_until)

sleep_for:


this_thread::sleep_for(2s);

sleep_until:


auto target = steady_clock::now() + 3s;
this_thread::sleep_until(target);

9) Using duration_cast with Any Clock


auto now = system_clock::now();
auto nanos = duration_cast<nanoseconds>(now.time_since_epoch());

cout << nanos.count();

10) TL;DR

  • chrono provides duration, time_point, and clock utilities
  • high_resolution_clock → used for benchmarking
  • system_clock → current real-world time
  • steady_clock → monotonic, never goes backward
  • C++20 introduced built-in calendar date types
  • duration_cast → converts between time units
  • Compatible with Visual Studio 2022, GCC, and Clang