Time Zones

Get detailed knowledge on time zones.

First of all, a time zone is a region, and its full history of the date, such as daylight saving time or leap seconds. The time zone library in C++20 is a complete parser of the IANA timezone database. The following table should give you a first idea of the new functionality.

Type Description
std::chrono::tzdb Describes a copy of the IANA time-zone database
std::chrono::tzdb_list Represents a linked list of the tzdb
std::chrono::get_tzdb
std::chrono::get_tzdb_list
std::chrono::reload_tzdb
std::chrono::remote_version
Accesses and controls the global time-zone database
std::chrono::locate_zone Locates the time zone based on its name
std::chrono::current_zone Returns the current time zone
std::chrono::time_zone Represents a time zone
std::chrono::sys_info Represents information about a time zone at a specific time point
std::chrono::local_info Represents information about a local time to UNIX time conversion
std::chrono::zoned_traits Class for time zone pointers
std::chrono::zoned_time Represents a time zone and a time point
std::chrono::leap_second Contains information about a leap-second insertion
std::chrono::time_zone_link Represents an alternative name for a time zone
std::chrono::nonexistent_local_time Exception which is thrown if a local time does not exist

I use in my examples the function std::chrono::zoned_time, which is essentially a time zone combined with a time point.

Compilation of the examples

Before I show you two examples, I want to make a short remark. To compile a program using the time zone library, you have to compile the tz.cpp file from the date library and link it against the curl library. The curl library is necessary to get the current IANA timezone database. The following command line for g++ should give you the idea:

g++ localTime.cpp -I <Path to data/tz.h> tz.cpp -std=c++17 -lcurl -o localTime

My first example is straightforward. It displays the UTC time and the local time.

UTC time and local time

The UTC time or Coordinated Universal Time is the primary time standard worldwide. A computer uses Unix time which is a very close approximation of UTC. The UNIX time is the number of seconds since the Unix epoch. The Unix epoch is 00:00:00 UTC on 1 January 1970.

std::chrono::system_clock::now() returns in the following program the Unix time.

Get hands-on with 1200+ tech skills courses.