Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::is_am,std::chrono::is_pm,std::chrono::make12,std::chrono::make24

      From cppreference.com
      <cpp‎ |chrono
       
       
      Date and time library
      Time point
      (C++11)
      (C++20)
      Duration
      (C++11)
      Clocks
      (C++20)
      (C++20)
      (C++20)
      (C++20)
      (C++20)
      (C++20)
      Time of day
      is_amis_pm
      (C++20)(C++20)
      make12make24
      (C++20)(C++20)
      (C++20)
       
      Defined in header<chrono>
      constexprbool is_am(conststd::chrono::hours& h)noexcept;
      (1)(since C++20)
      constexprbool is_pm(conststd::chrono::hours& h)noexcept;
      (2)(since C++20)
      constexprstd::chrono::hours make12(conststd::chrono::hours& h)noexcept;
      (3)(since C++20)
      constexprstd::chrono::hours make24(conststd::chrono::hours& h,
                                           bool is_pm)noexcept;
      (4)(since C++20)

      These functions aid in translating between a 12-hour format time of day, and a 24-hour format time of day.

      1) Detects whether the 24-hour format time is a.m. (ante meridiem, before midday).
      2) Detects whether the 24-hour format time is p.m. (post meridiem, after midday).
      3) Returns the 12-hour equivalent of a 24-hour format time.
      4) Returns the 24-hour equivalent of a 12-hour format timeh, whereis_pm determines whether the time is p.m.

      [edit]Parameters

      h - 12-hour or 24-hour format time to detect
      is_pm - whether the 12-hour format time is p.m.

      [edit]Return value

      1)0h<= h&& h<= 11h
      2)12h<= h&& h<= 23h
      3) Ifh is in range[0h23h], returns the 12-hour equivalent in range[1h12h]. Otherwise, the return value is unspecified.
      4) Ifh is in range[1h12h], returns the 24-hour equivalent in range[0h11h] ifis_pm isfalse, or in range[12h23h] otherwise. Otherwise, the return value is unspecified.

      [edit]Example

      Run this code
      #include <chrono>#include <iomanip>#include <iostream>#include <utility> int main(){usingnamespace std::chrono;     static_assert(        is_am(10h)&&  is_am(11h)&&!is_am(12h)&&!is_am(23h)&&!is_pm(10h)&&!is_pm(11h)&&  is_pm(12h)&&  is_pm(23h)); std::cout<<"make12():\n"; for(const hours hh:{0h, 1h, 11h, 12h, 13h, 23h}){const hours am{make12(hh)};std::cout<<std::setw(2)<< hh.count()<<"h == "<<std::setw(2)<< am.count()<<(is_am(hh)?"h a.m.\n":"h p.m.\n");} std::cout<<"\nmake24():\n"; using p=std::pair<hours,bool>; for(constauto&[hh, pm]:{p{1h,0}, p{12h,0}, p{1h,1}, p{12h,1}}){std::cout<<std::setw(2)<< hh.count()<<(pm?"h p.m.":"h a.m.")<<" == "<<std::setw(2)<< make24(hh, pm).count()<<"h\n";}}

      Output:

      make12(): 0h == 12h a.m. 1h ==  1h a.m.11h == 11h a.m.12h == 12h p.m.13h ==  1h p.m.23h == 11h p.m. make24(): 1h a.m. ==  1h12h a.m. ==  0h 1h p.m. == 13h12h p.m. == 12h
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/hour_fun&oldid=159664"

      [8]ページ先頭

      ©2009-2025 Movatter.jp