std::chrono::zoned_time

From cppreference.com
< cpp‎ | chrono
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
Date and time utilities
Time point
(C++11)
(C++20)
Duration
(C++11)
Clocks
(C++11)      
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time of day
(C++20)(C++20)
(C++20)(C++20)
(C++20)

Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
Time zones
(C++20)
(C++20)
(C++20)
zoned_time
(C++20)

chrono I/O
(C++20)
C-style date and time
 
 
Defined in header <chrono>
template <

    class Duration,
    class TimeZonePtr = const std::chrono::time_zone*

> class zoned_time;
(since C++20)
using zoned_seconds = std::chrono::zoned_time<std::chrono::seconds>;
(since C++20)

The class zoned_time represents a logical pairing of a time zone and a std::chrono::time_point whose resolution is Duration.

An invariant of zoned_time is that it always refers to a valid time zone and represents an existing and unambiguous time point in that time zone. Consistent with this invariant, zoned_time has no move constructor or move assignment operator; attempts to move a zoned_time will perform a copy.

The program is ill-formed if Duration is not a specialization of std::chrono::duration.

The template parameter TimeZonePtr allows users to supply their own time zone pointer types and further customize the behavior of zoned_time via std::chrono::zoned_traits. Custom time zone types need not support all the operations supported by std::chrono::time_zone, only those used by the functions actually called on the zoned_time.

TimeZonePtr must be MoveConstructible. Move-only TimeZonePtrs are allowed but difficult to use, as the zoned_time will be immovable and it is not possible to access the stored TimeZonePtr.

Member types

Member type Definition
duration std::common_type_t<Duration, std::chrono::seconds>

Member functions

constructs a zoned_time
(public member function)
assigns value to a zoned_time
(public member function)
obtains a copy of the time zone pointer
(public member function)
obtains the stored time point as a local_time
(public member function)
obtains the stored time point as a sys_time
(public member function)
obtain information about the time zone at the stored time point
(public member function)

Non-member functions

compares two zoned_time values
(function template)
outputs a zoned_time into a stream
(function template)

Helper classes

specialization of std::formatter that formats a zoned_time according to the provided format
(class template specialization)

Deduction guides

Example

#include <chrono>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <string_view>
 
int main()
{
    constexpr std::string_view locations[] = {
        "Africa/Casablanca",   "America/Argentina/Buenos_Aires",
        "America/Barbados",    "America/Indiana/Petersburg",
        "America/Tarasco_Bar", "Antarctica/Casey",
        "Antarctica/Vostok",   "Asia/Magadan",
        "Asia/Manila",         "Asia/Shanghai",
        "Asia/Tokyo",          "Atlantic/Bermuda",
        "Australia/Darwin",    "Europe/Isle_of_Man",
        "Europe/Laputa",       "Indian/Christmas",
        "Indian/Cocos",        "Pacific/Galapagos",
    };
    constexpr auto width = std::ranges::max_element(locations, {},
        [](const auto& s) { return s.length(); })->length();
 
    for (const auto location : locations) {
        try {
            // may throw if `location` is not in the time zone database
            const std::chrono::zoned_time zt{location, std::chrono::system_clock::now()};
            std::cout << std::setw(width) << location << " - Zoned Time: " << zt << '\n';
        } catch (std::chrono::nonexistent_local_time& ex) {
            std::cout << "Error: " << ex.what() << '\n';
        }
    }
}

Possible output:

             Africa/Casablanca - Zoned Time: 2021-09-16 10:26:54.837665555 +01
America/Argentina/Buenos_Aires - Zoned Time: 2021-09-16 06:26:55.090150136 -03
              America/Barbados - Zoned Time: 2021-09-16 05:26:55.090419331 AST
    America/Indiana/Petersburg - Zoned Time: 2021-09-16 05:26:55.090465623 EDT
Error: America/Tarasco_Bar not found in timezone database
              Antarctica/Casey - Zoned Time: 2021-09-16 20:26:55.123070973 +11
             Antarctica/Vostok - Zoned Time: 2021-09-16 15:26:55.123151218 +06
                  Asia/Magadan - Zoned Time: 2021-09-16 20:26:55.123208852 +11
                   Asia/Manila - Zoned Time: 2021-09-16 17:26:55.123434512 PST
                 Asia/Shanghai - Zoned Time: 2021-09-16 17:26:55.123520538 CST
                    Asia/Tokyo - Zoned Time: 2021-09-16 18:26:55.123626199 JST
              Atlantic/Bermuda - Zoned Time: 2021-09-16 06:26:55.123713854 ADT
              Australia/Darwin - Zoned Time: 2021-09-16 18:56:55.155857464 ACST
            Europe/Isle_of_Man - Zoned Time: 2021-09-16 10:26:55.155909304 BST
Error: Europe/Laputa not found in timezone database
              Indian/Christmas - Zoned Time: 2021-09-16 16:26:55.215065303 +07
                  Indian/Cocos - Zoned Time: 2021-09-16 15:56:55.215137548 +0630
             Pacific/Galapagos - Zoned Time: 2021-09-16 03:26:55.215201447 -06