std::chrono...::zoned_time(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

okay so welcome to the manual page for std::chrono...::zoned_time(3) from the C++ Standard Library β€” it's giving constructor lore, and yes, the constructor is also called zoned_time. bestie, this is your no-cap map to every overload. πŸ’…

🌸 NAME 🌸

std::chrono::zoned_time::zoned_time - std::chrono::zoned_time::zoned_time β€” that's the whole name, and it's literally the constructor of zoned_time. iconic. ✨

✨ Synopsis ✨

okay so here are all 16 overloads, and every single one is (since C++20). no cap, we're listing every last one:

  1. (1) β€” (since C++20)
zoned_time();
  1. (2) β€” (since C++20)
zoned_time( const std::chrono::sys_time<Duration>& st );
  1. (3) β€” (since C++20)
zoned_time( const zoned_time& other ) = default;
  1. (4) β€” (since C++20)
template< class Duration2 >
zoned_time( const std::chrono::zoned_time<Duration2, TimeZonePtr>& other );
  1. (5) β€” (since C++20)
explicit zoned_time( TimeZonePtr z );
  1. (6) β€” (since C++20)
explicit zoned_time( std::string_view name );
  1. (7) β€” (since C++20)
zoned_time( TimeZonePtr z, const std::chrono::sys_time<Duration>& st );
  1. (8) β€” (since C++20)
zoned_time( std::string_view name, const std::chrono::sys_time<Duration>& st );
  1. (9) β€” (since C++20)
zoned_time( TimeZonePtr z, const std::chrono::local_time<Duration>& tp );
  1. (10) β€” (since C++20)
zoned_time( std::string_view name, const std::chrono::local_time<Duration>& tp );
  1. (11) β€” (since C++20)
zoned_time( TimeZonePtr z, const std::chrono::local_time<Duration>& tp, std::chrono::choose c );
  1. (12) β€” (since C++20)
zoned_time( std::string_view name, const std::chrono::local_time<Duration>& tp, std::chrono::choose c );
  1. (13) β€” (since C++20)
template< class Duration2, class TimeZonePtr2 >
zoned_time( TimeZonePtr z, const std::chrono::zoned_time<Duration2, TimeZonePtr2>& zt );
  1. (14) β€” (since C++20)
template< class Duration2, class TimeZonePtr2 >
zoned_time( TimeZonePtr z, const std::chrono::zoned_time<Duration2, TimeZonePtr2>& zt, std::chrono::choose );
  1. (15) β€” (since C++20)
template< class Duration2, class TimeZonePtr2 >
zoned_time( std::string_view name, const std::chrono::zoned_time<Duration2, TimeZonePtr2>& zt );
  1. (16) β€” (since C++20)
template< class Duration2, class TimeZonePtr2 >
zoned_time( std::string_view name, const std::chrono::zoned_time<Duration2, TimeZonePtr2>& zt, std::chrono::choose );

Constructs a zoned_time object, initializing the stored time zone pointer and time point according to the following table, where traits is std::chrono::zoned_traits<TimeZonePtr>:

the columns are overload, time zone pointer (denoted zone), time point (a std::chrono::sys_time<duration>), and notes. here's that table translated into bullet form so it's readable on your screen:

  • (1) β€” zone: traits::default_zone(); time point: default constructed; notes: (a).
  • (2) β€” zone: traits::default_zone(); time point: st; notes: (a).
  • (3) β€” zone: other.get_time_zone(); time point: other.get_sys_time(); notes: (b).
  • (4) β€” zone: other.get_time_zone(); time point: other.get_sys_time(); notes: (e).
  • (5) β€” zone: std::move(z); time point: default constructed.
  • (6) β€” zone: traits::locate_zone(name); time point: default constructed; notes: (c).
  • (7) β€” zone: std::move(z); time point: st.
  • (8) β€” zone: traits::locate_zone(name); time point: st; notes: (c).
  • (9) β€” zone: std::move(z); time point: zone->to_sys(tp); notes: (d).
  • (10) β€” zone: traits::locate_zone(name); time point: zone->to_sys(tp); notes: (c,d).
  • (11) β€” zone: std::move(z); time point: zone->to_sys(tp, c); notes: (d).
  • (12) β€” zone: traits::locate_zone(name); time point: zone->to_sys(tp, c); notes: (c,d).
  • (13,14) β€” zone: std::move(z); time point: zt.get_sys_time(); notes: (e).
  • (15,16) β€” zone: traits::locate_zone(name); time point: zt.get_sys_time(); notes: (c,e).
  • a) Constructors specified to call traits::default_zone() (1,2) do not participate in overload resolution if that expression is not well-formed.
  • b) The defaulted copy constructor (3) is defined as deleted if std::is_copy_constructible_v<TimeZonePtr> is false.
  • c) Constructors with a std::string_view parameter name (6,8,10,12,15,16) do not participate in overload resolution if traits::locate_zone(name) is not well-formed or if that expression is not convertible to TimeZonePtr.
  • d) Constructors specified to call zone->to_sys (9-12) do not participate in overload resolution if that call expression is not well-formed or if the result is not convertible to std::chrono::sys_time<duration>.
  • e) Constructors with a template parameter Duration2 (4,13-16) do not participate in overload resolution if Duration2 is not convertible to Duration.
⚠️ The behavior is undefined if the time zone pointer (initialized as described above) does not refer to a time zone. not me being dramatic, but that's literally undefined behavior, so respect it. πŸ’€

πŸŽ€ Notes πŸŽ€

zoned_time does not have a move constructor and attempting to move one will perform a copy instead using the defaulted copy constructor (3). Thus, when TimeZonePtr is a move-only type, zoned_time is immovable: it can be neither moved nor copied.

The constructors (14,16) accept a std::chrono::choose parameter, but that parameter has no effect. it's giving decorative parameter energy πŸ™ˆ.

πŸ¦‹ Example πŸ¦‹

Run this code, bestie:

// Run this code

#include <chrono>
#include <iostream>
#include <string_view>

int main()
{
    using std::chrono_literals::operator""y;
    using std::operator""sv;

    std::cout << std::chrono::zoned_time{} << " : default\n";

    constexpr std::string_view location1{"America/Phoenix"sv};
    std::cout << std::chrono::zoned_time{location1} << " : " << location1 << '\n';

    const std::chrono::time_zone* timeZonePtr = std::chrono::locate_zone("UTC");
    std::cout << std::chrono::zoned_time{timeZonePtr} << " : UTC time zone\n";

    constexpr auto location2{"Europe/Rome"sv};
    std::cout << std::chrono::zoned_time{location2, std::chrono::local_days{2021y/12/31}}
              << " : " << location2 << '\n';

    constexpr auto location3{"Europe/Rome"sv};
    constexpr auto some_date = std::chrono::sys_time<std::chrono::days>{2021y/12/31};
    std::cout << std::chrono::zoned_time{location3, some_date}
              << " : " << location3 << '\n';

    const auto now =
        std::chrono::floor<std::chrono::minutes>(std::chrono::system_clock::now());
    constexpr auto location4{"Europe/Rome"sv};
    std::cout << std::chrono::zoned_time{location4, now} << " : " << location4 << '\n';

    constexpr auto NewYork{"America/New_York"sv};
    constexpr auto Tokyo{"Asia/Tokyo"sv};
    const std::chrono::zoned_time tz_Tokyo{Tokyo, now};
    const std::chrono::zoned_time tz_NewYork{NewYork, now};
    std::cout << std::chrono::zoned_time{Tokyo, tz_NewYork} << " : " << Tokyo << '\n';
    std::cout << std::chrono::zoned_time{NewYork, tz_Tokyo} << " : " << NewYork << '\n';
}

πŸ”₯ Possible output πŸ”₯

1970-01-01 00:00:00 UTC : default
1969-12-31 17:00:00 MST : America/Phoenix
1970-01-01 00:00:00 UTC : UTC time zone
2021-12-31 00:00:00 CET : Europe/Rome
2021-12-31 01:00:00 CET : Europe/Rome
2021-09-20 23:04:00 CEST : Europe/Rome
2021-09-21 06:04:00 JST : Asia/Tokyo
2021-09-20 17:04:00 EDT : America/New_York

mwah πŸ’‹ β€” and for the cute sign-off, this page's footer info is: source/version http://cppreference.com, date 2024.06.10, and the manual name is std::chrono...::zoned_time(3). that's the tea, bestie. 🫢

πŸ“¦ where this page comes from