Search⌘ K
AI Features

Time Duration

Explore how C++17's chrono library manages time durations, from measuring differences between time points to using predefined and custom ratios. Learn to work with floating-point durations, understand conversion rules, and apply built-in literals to handle various time units efficiently.

We'll cover the following...

Time duration

Time duration is the difference between the two time-points. Time duration is measured in the number of ticks.

C++
template <class Rep, class Period = ratio<1>> class duration;

If Rep is a floating-point number, the time duration supports fractions of ticks. The most important time durations are predefined in the chrono library:

C++
typedef duration<signed int, nano> nanoseconds;
typedef duration<signed int, micro> microseconds;
typedef duration<signed int, milli> milliseconds;
typedef duration<signed int> seconds;
typedef duration<signed int, ratio< 60>> minutes;
typedef duration<signed int, ratio<3600>> hours;

How long can a time duration be? The C++ standard guarantees that the predefined time durations can store +/- 292 years. ...