std::chrono::time_point represents a point in time relative to a clock’s epoch. It combines a Clock with a Duration; the duration measures the interval from that clock’s epoch. A clock’s now() function returns a time_point, such as system_clock::now(). section 4.1 section 4.2.4
Use time points with subtraction to measure the duration between two moments. A system_clock supplies wall-clock time and can move backward if the operating system clock changes; a steady_clock is better suited to measuring intervals. Convert the resulting duration when the display needs days, seconds, or another unit. section 4.1 section 4.2.5
Countdown to the end of the year
A countdown obtains the current time point, constructs a calendar date, turns that date into a system time point with sys_days, subtracts now, and casts the result to days. The same pattern can accept a parsed year_month_day and format the result with std::format. section 4.2.5 section 4.3.2
Listing 4.2 Duration between two time points
#include <chrono>
#include <iostream>
void duration_to_end_of_year()
{
std::chrono::time_point now = std::chrono::system_clock::now();
constexpr auto year = 2022; #1
auto new_years_eve = std::chrono::year_month_day(
std::chrono::year(year),
std::chrono::month(12),
std::chrono::day(31)
);
auto event = std::chrono::sys_days(new_years_eve); #2
std::chrono::duration dur = event - now; #3
std::cout << dur << " until event\n"; #4
}
int main()
{
duration_to_end_of_year();
}
Walk through a complete C++ countdown that creates now and an event time point, subtracts them, and prints the resulting duration. section 4.1
The template defaults Duration to the clock’s duration, but its full type can be much more detailed: a system clock plus a duration whose representation and ratio encode the unit. Calendar dates can be written readably with year/month/day syntax before conversion to a time point. section 4.2.4 section 4.2.3

