liveBook · Manning

Time zone

As explained in Learn C++ by Example

A time zone is a geographic or rule-defined region whose local civil time is determined by a particular offset and calendar-time rules.

A time zone determines local civil time from a named region, its UTC offset, and calendar-time rules such as daylight saving. It lets countdowns measure the physical time between a current instant and a local event. section 4.4 section 4.5

Use time zones when a countdown or schedule must respect local clock changes:

  • Choose a zone by an IANA name such as Europe/London, or use the computer's current zone. section 4.4
  • Convert a system time to zoned or local time when displaying an event. section 4.4
  • Convert the local event back to system time before subtracting the current instant; local-time arithmetic ignores offset shifts. section 4.4

A daylight-saving countdown

In Britain, the clock change on March 27, 2022 means that a countdown from 3 a.m. on March 27 to the next day is shorter than a full day. A UTC-based calculation reports the wrong local result; a zoned calculation accounts for the missing hour. 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
}

Inspect a compact C++ countdown that constructs a local event in the current zone, obtains its system time, and subtracts the current instant. section 4.4

How Learn C++ by Example covers it

How deep it goes
Explains named-zone conversion and sys_time arithmetic, while stopping short of the full chrono feature set.
Examples
Uses Europe/London, the 2022 British Summer Time transition, and a local countdown implementation.
What it assumes
Assumes time points, durations, system time, and date construction, while introducing local-zone conversion and offset-aware arithmetic.
What you can do after
Apply it via C++20 time-zone facilities or the date library to calculate daylight-saving-aware countdowns.
How it connects
Connects time zones with UTC, system clocks, local time, daylight saving, zoned_time, and the IANA time-zone database.

Also explained in

Compare how 4 books explain time zone →

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.