std::chrono::zoned_time(3)
π std::chrono::zoned_time(3) π
okay bestie, this page comes from the C++ Standard Libary manual (yep, that's the spelling β we're keeping it). we're doing std::chrono::zoned_time, and it's lowkey iconic: time zone + time point, paired up. π«Ά
πΈ NAME πΈ
std::chrono::zoned_time β std::chrono::zoned_time
β¨ Synopsis β¨
Defined in header <chrono>
template<
class Duration,
class TimeZonePtr = const std::chrono::time_zone*
> class zoned_time;
using zoned_seconds = std::chrono::zoned_time<std::chrono::seconds>;
The template is (since C++20), and zoned_seconds is also (since C++20). periodt.
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. so if you try to move it, it just copies β no cap. π
π¨ ill-formed alert: the program is ill-formed ifDurationis not a specialization ofstd::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. iykyk β¨
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. bestie, read that again: immovable and you can't even reach the stored pointer. it's giving locked box energy. π
π Member types π
durationβstd::common_type_t<Duration, std::chrono::seconds>
π Member functions π
constructorβ constructs azoned_time(public member function)operator=β assigns value to azoned_time(public member function)get_time_zoneβ obtains a copy of the time zone pointer (public member function)operator local_time,get_local_timeβ obtains the stored time point as alocal_time(public member function)operator sys_time,get_sys_timeβ obtains the stored time point as asys_time(public member function)get_infoβ obtain information about the time zone at the stored time point (public member function)
π¦ Non-member functions π¦
operator==β compares twozoned_timevalues(C++20)(function template)operator<<β outputs azoned_timeinto a stream(C++20)(function template)
π Helper classes π
std::formatter<std::chrono::zoned_time>β formatting support forzoned_time(C++20)(class template specialization)std::hash<std::chrono::zoned_time>β hash support forstd::chrono::zoned_time(C++26)(class template specialization)
π§ Deduction guides π§
the original page just drops this heading here with no extra text, so we're keeping it that way, bestie. π«
π₯ Example π₯
// Run this code
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#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::runtime_error& ex)
{
std::cout << "Error: " << ex.what() << '\n';
}
}
π Possible output π
Africa/Casablanca - Zoned Time: 2023-06-29
20:58:34.697449319 +01
America/Argentina/Buenos_Aires - Zoned Time: 2023-06-29
16:58:34.709957354 -03
America/Barbados - Zoned Time: 2023-06-29
15:58:34.709977888 AST
America/Indiana/Petersburg - Zoned Time: 2023-06-29
15:58:34.709998072 EDT
Error: tzdb: cannot locate zone: America/Tarasco_Bar
Antarctica/Casey - Zoned Time: 2023-06-30
06:58:34.710093685 +11
Antarctica/Vostok - Zoned Time: 2023-06-30
01:58:34.710107932 +06
Asia/Magadan - Zoned Time: 2023-06-30
06:58:34.710121831 +11
Asia/Manila - Zoned Time: 2023-06-30
03:58:34.710134751 PST
Asia/Shanghai - Zoned Time: 2023-06-30
03:58:34.710153259 CST
Asia/Tokyo - Zoned Time: 2023-06-30
04:58:34.710172815 JST
Atlantic/Bermuda - Zoned Time: 2023-06-29
16:58:34.710191043 ADT
Australia/Darwin - Zoned Time: 2023-06-30
05:28:34.710236720 ACST
Europe/Isle_of_Man - Zoned Time: 2023-06-29
20:58:34.710256834 BST
Error: tzdb: cannot locate zone: Europe/Laputa
Indian/Christmas - Zoned Time: 2023-06-30
02:58:34.710360409 +07
Indian/Cocos - Zoned Time: 2023-06-30
02:28:34.710377520 +0630
Pacific/Galapagos - Zoned Time: 2023-06-29
13:58:34.710389952 -06
π See also π
time_zoneβ represents a time zone(C++20)(class)
π that's the whole page, bestie. source: http://cppreference.com, version/date: 2024.06.10, and the page is std::chrono::zoned_time(3). bye, go make time zones cute π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms