liveBook · Manning

Time_point

As explained in Learn C++ by Example

std::chrono::time_point is a class template representing a point in time relative to a clock's epoch using a duration.

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

How Learn C++ by Example covers it

How deep it goes
Explains template parameters, epochs, clocks, subtraction, conversions, and selected expanded types, while deferring complete chrono internals.
Examples
Demonstrates year-end countdowns, parsed event dates, sys_days conversion, duration output, and readable calendar literals.
What it assumes
Assumes basic C++ while teaching clocks, durations, epochs, calendar types, literals, requirements, and concepts as they arise.
What you can do after
Build countdowns with std::chrono by obtaining clock readings, creating event dates, subtracting time points, and formatting durations.
How it connects
Connects time_point with clocks, durations, calendar dates, duration_cast, time zones, formatting, and C++ concepts.

Also explained in

Compare how 2 books explain time_point →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.