liveBook · Manning

Sys_time

As explained in Learn C++ by Example

sys_time is a chrono time representation based on system time and suitable for measuring physical elapsed time across civil-time changes.

sys_time is a chrono time-point alias based on system_clock. Give it a duration, such as days, to represent an event and subtract it from now to obtain a duration. section 4.1 section 4.1

Countdown to an event

Create New Year's Eve with sys_days, then subtract the current system time: `auto event = std::chrono::sys_days(new_years_eve);` followed by `std::chrono::duration dur = event - now;`. The result can be streamed with C++20 chrono output or inspected with `dur.count()`. section 4.1 section 4.1

Use sys_time when physical elapsed time matters across civil-time changes. Convert local dates through zoned_time and compare their system times; local_time arithmetic intentionally preserves local clock time instead. section 4.4 section 4.4

Listing 4.15 Countdown in local time
std::chrono::system_clock::duration
countdown_in_local_time(std::chrono::system_clock::time_point now,
    std::chrono::year_month_day date) 
{
    using namespace std::chrono;
    auto sys_event = zoned_time(current_zone(),
                      local_days{ date }).get_sys_time();    #1
    return sys_event - now;                                  #2
}

A complete countdown function converts a local event through the current time zone, obtains system time, and subtracts now. section 4.4

How Learn C++ by Example covers it

How deep it goes
Explains aliases, date conversion, subtraction, output, and time-zone behavior, but only scratches the surface of chrono.
Examples
Uses a New Year's Eve countdown and a daylight-saving-aware local meeting countdown.
What it assumes
Builds from chrono dates and time points while using system_clock, durations, literals, and formatted output.
What you can do after
Apply sys_time to build countdowns, subtract time points, and account for daylight-saving transitions.
How it connects
Connects sys_time with system_clock, time_point, duration, sys_days, local_time, zoned_time, and time zones.

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.