liveBook · Manning

System_clock

As explained in Learn C++ by Example

The system clock is a clock representing system-wide calendar time that can move backward when the system time is adjusted.

std::chrono::system_clock represents system-wide calendar time. Calling now() returns a time_point based on the operating system’s UTC time, which makes it useful for dates and real-world events. section 4.1

The clock is not steady: an administrator or system adjustment can make a later now() appear earlier. Use steady_clock for measuring intervals; system_clock is better when calendar meaning matters. It also maps easily to C’s time_t. section 4.1 section 4.5

Counting down to New Year’s Eve

Capture the current time with std::chrono::system_clock::now(), create a calendar date with year_month_day, convert it using sys_days, and subtract the two time points to obtain a duration. Formatting can print the remaining duration and event date. Because system_clock uses UTC, local countdowns need a time zone to account for daylight saving time. section 4.1 section 4.3.2

std::chrono::time_point now = std::chrono::system_clock::now();

Start with the minimal call that obtains the current system time. section 4.1

How Learn C++ by Example covers it

How deep it goes
Explains now(), UTC behavior, clock instability, time points, and interval subtraction, while deferring steady interval timing.
Examples
Uses a New Year’s Eve countdown, year_month_day conversion, duration subtraction, and formatted date output.
What you can do after
Apply it via std::chrono to capture current calendar time and calculate durations until dated events.
How it connects
Connects time_point, duration, UTC, time zones, steady_clock, high_resolution_clock, file_clock, and time_t.

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.