Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::operator+,std::chrono::operator-(std::chrono::year)

      From cppreference.com
      <cpp‎ |chrono‎ |year
       
       
      Date and time library
       
       
      constexprstd::chrono::year operator+(conststd::chrono::year& y,
                                             conststd::chrono::years& ys)noexcept;
      (1)(since C++20)
      constexprstd::chrono::year operator+(conststd::chrono::years& ys,
                                             conststd::chrono::year& y)noexcept;
      (2)(since C++20)
      constexprstd::chrono::year operator-(conststd::chrono::year& y,
                                             conststd::chrono::years& ys)noexcept;
      (3)(since C++20)
      constexprstd::chrono::years operator-(conststd::chrono::year& y1,
                                             conststd::chrono::year& y2)noexcept;
      (4)(since C++20)
      1,2) Addsys.count() years toy.
      3) Subtractsys.count() years fromy.
      4) Returns the difference in years betweeny1 andy2.

      Contents

      [edit]Return value

      1,2)std::chrono::year(int(y)+ ys.count())
      3)std::chrono::year(int(y)- ys.count())
      4)std::chrono::years(int(y1)-int(y2))

      [edit]Notes

      If the resulting year value for(1-3) is outside the range[-3276732767], the actual value stored is unspecified.

      The result of subtracting twoyear values is a duration of typestd::chrono::years. This duration unit represents the length of the average Gregorian year, and the resulting duration bears no relationship to the number of days in the particular years represented by the operands. For example, the result of2018y- 2017y isstd::chrono::years(1), which represents 365.2425 days, not 365 days.

      [edit]Example

      Run this code
      #include <cassert>#include <chrono> int main(){std::chrono::year y{2020};     y=std::chrono::years(12)+ y;// overload (2): duration + time pointassert(y==std::chrono::year(2032));     y= y-std::chrono::years(33);// overload (3): time point - durationassert(y==std::chrono::year(1999)); // y = std::chrono::years(33) - y; // not supported: duration - time point usingnamespace std::chrono;constexprstd::chrono::years ys= 2025y- 2020y;// overload (4)    static_assert(ys==std::chrono::years(5));}

      [edit]See also

      increments or decrements the month
      (public member function ofstd::chrono::month)[edit]
      adds or subtracts a number of months
      (public member function ofstd::chrono::month)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/year/operator_arith_2&oldid=157535"

      [8]ページ先頭

      ©2009-2026 Movatter.jp