Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::zoned_time

      From cppreference.com
      <cpp‎ |chrono
       
       
      Date and time library
       
       
      Defined in header<chrono>
      template<

         class Duration,
         class TimeZonePtr=conststd::chrono::time_zone*

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

      The classzoned_time represents a logical pairing of a time zone and astd::chrono::time_point whose resolution isDuration.

      An invariant ofzoned_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 azoned_time will perform a copy.

      The program is ill-formed ifDuration is not a specialization ofstd::chrono::duration.

      The template parameterTimeZonePtr allows users to supply their own time zone pointer types and further customize the behavior ofzoned_time viastd::chrono::zoned_traits. Custom time zone types need not support all the operations supported bystd::chrono::time_zone, only those used by the functions actually called on thezoned_time.

      TimeZonePtr must beMoveConstructible. Move-onlyTimeZonePtrs are allowed but difficult to use, as thezoned_time will be immovable and it is not possible to access the storedTimeZonePtr.

      Contents

      [edit]Member types

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

      [edit]Member functions

      constructs azoned_time
      (public member function)[edit]
      assigns value to azoned_time
      (public member function)[edit]
      obtains a copy of the time zone pointer
      (public member function)[edit]
      obtains the stored time point as alocal_time
      (public member function)[edit]
      obtains the stored time point as asys_time
      (public member function)[edit]
      obtain information about the time zone at the stored time point
      (public member function)[edit]

      [edit]Non-member functions

      (C++20)
      compares twozoned_time values
      (function template)[edit]
      (C++20)
      outputs azoned_time into a stream
      (function template)[edit]

      [edit]Helper classes

      formatting support forzoned_time
      (class template specialization)[edit]
      hash support forstd::chrono::zoned_time
      (class template specialization)

      [edit]Helper specializations

      template<class Duration>

      constexprbool enable_nonlocking_formatter_optimization

       <chrono::zoned_time<Duration,const chrono::time_zone*>>=true;
      (since C++23)

      This specialization ofstd::enable_nonlocking_formatter_optimization enables efficient implementation ofstd::print andstd::println for printing achrono::zoned_time object.

      [edit]Deduction guides

      [edit]Example

      Run this code
      #include <algorithm>#include <chrono>#include <iomanip>#include <iostream>#include <stdexcept>#include <string_view> int main(){constexprstd::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",}; constexprauto width= std::ranges::max_element(locations,{},[](constauto& s){return s.length();})->length(); for(constauto location: locations)try{// may throw if 'location' is not in the time zone databaseconst 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 +01America/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 EDTError: 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 BSTError: 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

      [edit]See also

      (C++20)
      represents a time zone
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/zoned_time&oldid=173866"

      [8]ページ先頭

      ©2009-2025 Movatter.jp