<chrono> 演算子

operator+

次の型の加算演算子:

1) 
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
   operator+(
      const duration<Rep1, Period1>& Left,
      const duration<Rep2, Period2>& Right);

2)
template <class Clock, class Duration1, class Rep2, class Period2>
constexpr time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
   operator+(
      const time_point<Clock, Duration1>& Time,
      const duration<Rep2, Period2>& Dur);

3)
template <class Rep1, class Period1, class Clock, class Duration2>
time_point<Clock, constexpr typename common_type<duration<Rep1, Period1>, Duration2>::type>
   operator+(
      const duration<Rep1, Period1>& Dur,
      const time_point<Clock, Duration2>& Time);

4)
constexpr day operator+(const day& d, const days& ds) noexcept; // C++20
constexpr day operator+(const days& ds, const day&  d) noexcept; // C++20

5)
constexpr month operator+(const month& m, const months& ms) noexcept; // C++20
constexpr month operator+(const months& ms, const month& m) noexcept; // C++20

6)
constexpr weekday operator+(const weekday& wd, const days& wds) noexcept // C++20
constexpr weekday operator+(const days& ds, const weekday& wd) noexcept; // C++20

7)
constexpr year operator+(const year& y, const years& ys) noexcept; // C++20
constexpr year operator+(const years& ys, const year& y) noexcept; // C++20

8)
constexpr year_month operator+(const year_month& ym, const months& dm) noexcept; // C++20
constexpr year_month operator+(const months& dm, const year_month& ym) noexcept; // C++20
constexpr year_month operator+(const year_month& ym, const years& dy) noexcept; // C++20
constexpr year_month operator+(const years& dy, const year_month& ym) noexcept; // C++20

9)
constexpr year_month_day operator+(const year_month_day& ymd, const months& dm) noexcept; // C++20
constexpr year_month_day operator+(const months& dm, const year_month_day& ymd) noexcept; // C++20
constexpr year_month_day operator+(const year_month_day& ymd, const years& dy) noexcept; // C++20
constexpr year_month_day operator+(const years& dy, const year_month_day& ymd) noexcept; // C++20

10)
constexpr year_month_day_last operator+(const year_month_day_last& ymdl, const months& dm) noexcept; // C++20

11)
constexpr year_month_day_last operator+(const months& dm, const year_month_day_last& ymdl) noexcept; // C++20

12)
constexpr year_month_day_last operator+(const year_month_day_last& ymdl, const years& dy) noexcept; // C++20
constexpr year_month_day_last operator+(const years& dy, const year_month_day_last& ymdl) noexcept; // C++20

13)
constexpr year_month_weekday operator+(const year_month_weekday& ymwd, const months& dm) noexcept; // C++20
constexpr year_month_weekday operator+(const months& dm, const year_month_weekday& ymwd) noexcept; // C++20

14)
constexpr year_month_weekday operator+(const year_month_weekday& ymwd, const years& dy) noexcept; // C++20

15)
constexpr year_month_weekday operator+(const years& dy, const year_month_weekday& ymwd) noexcept; // C++20

16)
constexpr year_month_weekday_last operator+(const year_month_weekday_last& ymwdl, const months& dm) noexcept; // C++20
constexpr year_month_weekday_last operator+(const months& dm, const year_month_weekday_last& ymwdl) noexcept; // C++20

17)
constexpr year_month_weekday_last operator+(const year_month_weekday_last& ymwdl, const years& dy) noexcept; // C++20
constexpr year_month_weekday_last operator+(const years& dy, const year_month_weekday_last& ymwdl) noexcept; // C++20

戻り値

1) LeftRight をそれらの共通型に変換した後で、変換されたティック数の加算結果と等しいティック数を持つ duration を返します。

2-3) Time 時点から、間隔 Dur によって変位される時点を表す time_point オブジェクトを返します。

4) d+ds.count() の結果を返します。 結果が [0, 255] の範囲外の場合、結果は指定されません。

5) m+ms.count() の結果を返します。 結果が [1, 12] の範囲外の場合は、12 を法として減算され、次に + 1 が加算されます。

6) weekday に日数と曜日を加算した結果を返します。 結果は 7 を法とするため、常に [0, 6] の範囲内です。

7) 指定された年数に年を加算した結果を返します。

8) 指定された月と年に月数と年数を加算した結果を返します。

9) year_month_day に月または年を加算した結果を返します。 ymd.month()February であり、ymd.day() が [1d, 28d] の範囲内でない場合、ok() は加算の結果として false を返すことがあります。

10) (ymdl.year() / ymdl.month() + dm) / last を返します。 注: ここで使用されている / は、除算演算子ではありません。 日付演算子です。

11) ymdl + dm を返します。

12) {ymdl.year()+dy, ymdl.month_day_last()} を返します。

13) ymwd + dm.count() を返します。

14-15) {ymwd.year()+dy, ymwd.month(), ymwd.weekday_indexed()} を返します。

16) (ymwdl.year() / ymwdl.month() + dm) / ymwdl.weekday_last() を返します。 注: ここで使用されている / は、除算演算子ではなく、日付演算子です。

17) ymwdl + dy を返します。

例: operator+

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    // day
    day d{1};
    std::cout << d + days(2) << '\n'; // 03

    // month
    month m{11};
    std::cout << m + months(3)<< '\n'; // Feb

    // weekday
    weekday wd = Thursday;
    std::cout << wd + days(1) << '\n'; // Fri

    // year_month_day_last
    year_month_day_last ymdl{June / last / 2021};
    std::cout << ymdl + years{1} + months{1} << '\n'; // 2022/Jul/last

    // year_month_weekday
    year_month_weekday ymw{ year(1997) / January / Wednesday[1] };
    std::cout << ymw + months{1} << '\n'; // 1997/Feb/Wed[1]
    std::cout << ymw + years{1} << '\n'; // 1998/Jan/Wed[1] 

    // year_month_weekday_last
    year_month_weekday_last ymwl{ year(1997) / January / Wednesday[last] };
    std::cout << ymwl + months{ 1 } << '\n'; // 1997/Feb/Wed[last]
    std::cout << ymwl + years{ 1 } << '\n'; // 1998/Jan/Wed[last] 

    return 0;
}
03
Feb
Fri
2022/Jul/last
1997/Feb/Wed[1]
1998/Jan/Wed[1]
1997/Feb/Wed[last]
1998/Jan/Wed[last]

単項 operator+

単項プラスを次の型に適用します。

// duration
constexpr common_type_t<duration> operator+() const // C++20

戻り値

*this を返します。

operator-

次の型の減算演算子:

1)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
   operator-(
       const duration<Rep1, Period1>& Left,
       const duration<Rep2, Period2>& Right);
2)
template <class Clock, class Duration1, class Rep2, class Period2>
constexpr time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type
   operator-(
       const time_point<Clock, Duration1>& Time,
       const duration<Rep2, Period2>& Dur);
3)
template <class Clock, class Duration1, class Duration2>
constexpr typename common_type<Duration1, Duration2>::type
   operator-(
       const time_point<Clock, Duration1>& Left,
       const time_point<Clock, Duration2>& Right);
4)
constexpr day operator-(const day& d,  days& ds) noexcept; // C++20
constexpr day operator-(const day& d, const day& d) noexcept; // C++20

5)
constexpr month operator-(const month& m, const months& ms) noexcept; // C++20
constexpr month operator-(const month& m, const month& ms) noexcept; // C++20

6)
constexpr months operator-(const year_month& Left, const year_month& Right) noexcept; // C++20

7)
constexpr weekday operator-(const weekday& Left, const days& Right) noexcept; // C++20

8)
constexpr days operator-(const weekday& Left, const weekday& Right) noexcept; // C++20

9)
constexpr year operator-(const year& y, const years& ys) noexcept; // C++20

10)
constexpr years operator-(const year& y, const year& y2) noexcept; // C++20

11)
constexpr year_month operator-(const year_month& ym, const months& dm) noexcept; // C++20
constexpr year_month operator-(const year_month& ym, const years& dy) noexcept; // C++20

12)
constexpr year_month_day operator-(const year_month_day& ymd, const months& dm) noexcept; // C++20
constexpr year_month_day operator-(const year_month_day& ymd, const years& dy) noexcept; // C++20

13)
constexpr year_month_day_last operator-(const year_month_day_last& ymdl, const months& dm) noexcept;  // C++20

14)
constexpr year_month_day_last operator-(const year_month_day_last& ymdl, const years& dy) noexcept;  // C++20

15)
constexpr year_month_weekday operator-(const year_month_weekday& ymwd, const months& dm) noexcept; // C++20

16)
constexpr year_month_weekday operator-(const year_month_weekday& ymwd, const years& dy) noexcept; // C++20

17)
constexpr year_month_weekday_last operator-(const year_month_weekday_last& ymwdl, const months& dm) noexcept; // C++20

18)
constexpr year_month_weekday_last operator-(const year_month_weekday_last& ymwdl, const years& dy) noexcept; // C++20

戻り値

1) 減算される期間をそれらの共通型に変換した後で、Left 内のティック数から Right 内のティック数を減算した値と等しいティック数を持つ duration を返します。

2) Time で指定された時点から、Dur によって表される時間間隔を減算することによって変位される時点を表す time_point を返します。

3) LeftRight の間の時間間隔を表す duration オブジェクトを返します。

4) d-ds.count() の結果を返します。 結果が [0, 255] の範囲外の場合、結果は指定されません。

5) m.ok() == truems.ok() == true の場合は、2 つの月の値を減算した結果、または月数を減算した結果を返します。 結果は [1, 12] の範囲内になります。 結果が負の場合は、ラップアラウンドします。 たとえば、1 月から 1 つの月を減算すると (month m1{1} - months{1};)、結果は 12 (12 月) になります。

6) LeftRight の差を月単位で返します。

7) Left.ok() == trueRight.ok() == true の場合は、weekday を [days{0}, days{6}] の範囲内で返します。

8) 2 つの曜日の間の日数を返します。

9) year(int(y)-ys.count)()) を返します。

10) years(int(y) - int(y2)) を返します。 2 つの year 値を減算すると、結果は std::chrono::years になります。これは、yy2 の差を年単位で表します。 たとえば、2021y-2000y を使用すると、years(21) が生成されます。

11) year_month 値から月または年を減算した結果を返します。

12) year_month_day 値から月または年を減算した結果を返します。

13) year_month_day_last 値から月数を減算した結果を返します。 つまり、ymdl-dm です。

14) year_month_day_last 値から年数を減算した結果を返します。 つまり、ymdl-dy です。

15) year_month_weekday 値から月数を減算した結果を返します。 つまり、ymwd-dm です。

16) year_month_weekday 値から年数を減算した結果を返します。 つまり、ymwd-dy です。

17) year_month_weekday_last 値から月数を減算した結果を返します。 つまり、ymwdl-dm です。

18) year_month_weekday_last 値から年数を減算した結果を返します。 つまり、ymwdl-dy です。

例: operator-

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    // day
    day d{10};
    d = d - days(5);  
    std::cout << d << '\n'; // 05

    // month 
    month m{2};
    m = m - months{1};
    std::cout << m << '\n'; // Jan
    m = m - months{1};
    std::cout << m << '\n'; // Dec

    // year
    auto diff1 = 2021y-2000y;
    auto diff2 = 2021y-years{1};
    std::cout << diff1.count() << '\n'; // 21
    std::cout << diff2 << '\n'; // 2020

    // year_month
    const year theYear{ 2021 };
    year_month ym1{theYear, June};
    year_month ym2 = ym1 - months{2};
    std::cout << ym2 << '\n'; // 2021/Apr
    year_month ym3 = ym1 - years{2};
    std::cout << ym3 << '\n'; // 2019/Jun

    // year_month_day_last
    year_month_day_last ymdl = June / last / 2021;
    std::cout << ymdl - years{1} - months{1} << '\n'; // 2022/Jul/last

    // year_month_weekday
    year_month_weekday ymw{ year(1997) / January / Wednesday[1] };
    std::cout << ymw - months{1} << '\n'; // 1996/Dec/Wed[1]
    std::cout << ymw - years{1} << '\n'; // 1996/Jan/Wed[1] 

    // year_month_weekday_last
    year_month_weekday_last ymwl{ year(1997) / January / Wednesday[last] };
    std::cout << ymwl - months{ 1 } << '\n'; // 1996/Dec/Wed[last]
    std::cout << ymwl - years{ 1 } << '\n'; // 1996/Jan/Wed[last]
    
    return 0;
}
05
Jan
Dec
21
2020
2021/Apr
2019/Jun
2020/May/last
1996/Dec/Wed[1]
1996/Jan/Wed[1]
1996/Dec/Wed[last]
1996/Jan/Wed[last]

単項 operator-

duration を減算します。

constexpr common_type_t<duration> operator-() const;

戻り値

*this の負のコピーを返します。

例: 単項 operator-

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
   duration<int, std::milli> milliseconds(120);
   std::cout << -milliseconds << '\n';
   return 0;
}
-120ms

operator!=

次に該当するかどうかを判断します。

1) 2 つの duration オブジェクトは、同じティック数を表していません。
2) 2 つの time_point オブジェクトが同じ時点を表していません。

1)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator!=(
    const duration<Rep1, Period1>& Left,
    const duration<Rep2, Period2>& Right);

2)
template <class Clock, class Duration1, class Duration2>
constexpr bool operator!=(
    const time_point<Clock, Duration1>& Left,
    const time_point<Clock, Duration2>& Right);

パラメーター

Left
左側の duration オブジェクトまたは time_point オブジェクト。

Right
右側の duration オブジェクトまたは time_point オブジェクト。

戻り値

1) LeftRight に共通の型のティック数が等しくない場合は、true を返します。 それ以外の場合、false を返します。
2) 2 つの time_point オブジェクトが同じ時点を表していない場合は、true を返します。 それ以外の場合、false を返します。

operator*

duration オブジェクトの乗算演算子。 乗算される duration をそれらの共通型に変換した後で、変換されたティック数の乗算結果と等しいティック数を持つ duration を返します。

1)
template <class Rep1, class Period1, class Rep2>
constexpr duration<typename common_type<Rep1, Rep2>::type, Period1>
   operator*(
      const duration<Rep1, Period1>& Dur,
      const Rep2& Mult);

2)
template <class Rep1, class Rep2, class Period2>
constexpr duration<typename common_type<Rep1, Rep2>::type, Period2>
   operator*(
       const Rep1& Mult,
       const duration<Rep2,
       Period2>& Dur);

パラメーター

Dur
duration オブジェクト。

Mult
整数値。

戻り値

間隔の長さが Dur の長さと Mult の乗算結果である duration オブジェクトを返します。

1) is_convertible<Rep2, common_type<Rep1, Rep2>>true にならない限り、この関数はオーバーロードの解決に関与しません。 詳細については、「<type_traits>」を参照してください。

2) is_convertible<Rep1, common_type<Rep1, Rep2>>true にならない限り、この関数はオーバーロードの解決に関与しません。 詳細については、「<type_traits>」を参照してください。

operator<

1) 比較される duration をそれらの共通型に変換した後で、Left のティック数が Right より小さいかどうかを判断します。

2) Lefttime_point のエポック以降の経過時間が Righttime_point のエポック以降の経過時間より小さいかどうかを判断します。

1)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator<(
    const duration<Rep1, Period1>& Left,
    const duration<Rep2, Period2>& Right);

2)
template <class Clock, class Duration1, class Duration2>
constexpr bool operator<(
    const time_point<Clock, Duration1>& Left,
    const time_point<Clock, Duration2>& Right);

パラメーター

Left
左側の duration オブジェクトまたは time_point オブジェクト。

Right
右側の duration オブジェクトまたは time_point オブジェクト。

戻り値

1) Left のティック数が Right のティック数より小さい場合は、true を返します。 それ以外の場合、関数は false を返します。

2) LeftRight より前である場合は、true を返します。 それ以外の場合、false を返します。

operator<=

1) 比較される duration をそれらの共通型に変換した後で、Left のティック数が Right 以下であるかどうかを判断します。

2) Lefttime_point のエポック以降の経過時間が Righttime_point のエポック以降の経過時間以下であるかどうかを判断します。

1)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator<=(
    const duration<Rep1, Period1>& Left,
    const duration<Rep2, Period2>& Right);

2)
template <class Clock, class Duration1, class Duration2>
constexpr bool operator<=(
    const time_point<Clock, Duration1>& Left,
    const time_point<Clock, Duration2>& Right);

パラメーター

Left
左側の duration オブジェクトまたは time_point オブジェクト。

Right
右側の duration オブジェクトまたは time_point オブジェクト。

戻り値

1) Left のティック数が Right のティック数以下である場合は、true を返します。 それ以外の場合、関数は false を返します。

2) LeftRight より前であるか、等しい場合は、true を返します。 それ以外の場合、false を返します。

operator==

次に該当するかどうかを判断します。

1) duration オブジェクトは、同じ長さの時間間隔を表しています。
2) time_point オブジェクトは、同じ時点を表しています。
3) day オブジェクトは、同じ日を表しています。
4) month オブジェクトは、同じ月を表しています。
5) month_day オブジェクトは、同じ月と日を表しています。
6) month_day_last オブジェクトは、同じ月を表しています。
7) month_weekday オブジェクトは、同じ月と n 番目の曜日を表しています。
8) month_weekday_last オブジェクトは、同じ月と最後の曜日を表しています。
9) weekday オブジェクトは、同じ曜日を表しています。
10) weekday_last オブジェクトは、その月の同じ最後の曜日を表しています。
11) weekday_indexed は、同じ曜日インデックスを表しています。
12) year は、同じ年を表しています。
13) year_month は、同じ年と月を表しています。
14) year_month_day は、同じ年、月、日を表しています。
15) year_month_day_last は、年と月の同じ最後の日を表しています。
16) year_month_weekday は、同じ曜日、年、月を表しています。
17) year_month_weekday_last は、その月の同じ最後の曜日、年、月を表しています。
18) time_zone_link は、同じ name を持っています。 target 名は考慮されません。
19) zoned_time は、同じ時刻とタイム ゾーンを表しています。

// 1) duration<Rep, Period>
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator==(
    const duration<Rep1, Period1>& Left,
    const duration<Rep2, Period2>& Right);

// 2) time_point
template <class Clock, class Duration1, class Duration2>
constexpr bool operator==(
    const time_point<Clock, Duration1>& Left,
    const time_point<Clock, Duration2>& Right);

// 3) day
constexpr bool operator==(const day& Left, const day& Right) noexcept; // C++20

// 4) month
constexpr bool operator==(const month& Left, const month& Right) noexcept; // C++20

// 5) month_day
constexpr bool operator==(const month_day& Left, const month_day& Right) noexcept; // C++20

// 6) month_day_last
constexpr bool operator==(const month_day_last& Left, const month_day_last& Right) noexcept; // C++20

// 7) month_weekday
constexpr bool operator==(const month_weekday& Left, const month_weekday& Right) noexcept; // C++20

// 8) month_weekday_last
constexpr bool operator==(const month_weekday_last& Left, const month_weekday_last& Right) noexcept; // C++20

// 9) weekday
constexpr bool operator==(const weekday& Left, const weekday& Right) noexcept; // C++20

// 10) weekday_last
constexpr bool operator==(const weekday_last& Left, const weekday_last& Right) noexcept; // C++20

// 11) weekday_indexed
constexpr bool operator==(const weekday_indexed& x, const weekday_indexed& y) noexcept; // C++20

// 12) year
constexpr bool operator==(const year& Left, const year& y ) noexcept; // C++20

// 13) year_month
constexpr bool operator==(const year_month& Left, const year_month& Right) noexcept; // C++20

// 14) year_month_day
constexpr bool operator==(const year_month_day& Left, const year_month_day& Right) noexcept; // C++20

// 15) year_month_day_last
constexpr bool operator==(const year_month_day_last& Left, const year_month_day_last& Right) noexcept; // C++20

// 16) year_month_weekday
constexpr bool operator==(const year_month_weekday& Left, const year_month_weekday& Right) noexcept; // C++20

// 17)  year_month_weekday_last
constexpr bool operator==(const year_month_weekday_last& Left, const year_month_weekday_last& Right) noexcept; // C++20

// 18) time_zone_link
bool operator==(const time_zone_link& Left, const time_zone_link& Right) noexcept; // C++20

// 19) zoned_time
template <class Duration1, class Duration2, class TimeZonePtr>
bool operator==(const zoned_time<Duration1, TimeZonePtr>& Left, const zoned_time<Duration2, TimeZonePtr>& Right); // C++20

パラメーター

Left
比較する左のオブジェクト。例: Left == Right

Right
比較する右のオブジェクト。

戻り値

1) LeftRight に共通の型のティック数が等しい場合は、true を返します。 それ以外の場合、false を返します。
2) LeftRight が同じ時点を表す場合は、true を返します。 それ以外の場合、false を返します。
3-17) LeftRight が同じ値を持っている場合は、true を返します。 それ以外の場合、false を返します。
18) Left.name() == Right.name() の場合は、true を返します。 それ以外の場合、*false* を返します。
19) Left.get_time_zone() == _Right.get_time_zone() && Left.get_sys_time() == Right.get_sys_time(); の場合は、true を返します。

operator>

1) 比較される duration をそれらの共通型に変換した後で、Left のティック数が Right より大きいかどうかを判断します。

2) Lefttime_point のエポック以降の経過時間が Righttime_point のエポック以降の経過時間より大きいかどうかを判断します。

1) 
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator>(
    const duration<Rep1, Period1>& Left,
    const duration<Rep2, Period2>& Right);

2)
template <class Clock, class Duration1, class Duration2>
constexpr bool operator>(
    const time_point<Clock, Duration1>& Left,
    const time_point<Clock, Duration2>& Right);

パラメーター

Left
左側の duration オブジェクトまたは time_point オブジェクト。

Right
右側の duration オブジェクトまたは time_point オブジェクト。

戻り値

1) Left のティック数が Right のティック数より大きい場合は、true を返します。 それ以外の場合、関数は false を返します。

2) LeftRight より後である場合は、true を返します。 それ以外の場合、false を返します。

operator>=

1) 比較される duration をそれらの共通型に変換した後で、Left のティック数が Right 以上であるかどうかを判断します。

2) Lefttime_point のエポック以降の経過時間が Righttime_point のエポック以降の経過時間以上であるかどうかを判断します。

1)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator>=(
    const duration<Rep1, Period1>& Left,
    const duration<Rep2, Period2>& Right);

2)
template <class Clock, class Duration1, class Duration2>
constexpr bool operator>=(
    const time_point<Clock, Duration1>& Left,
    const time_point<Clock, Duration2>& Right);

パラメーター

Left
左側の duration オブジェクトまたは time_point オブジェクト。

Right
右側の duration オブジェクトまたは time_point オブジェクト。

戻り値

1) Left のティック数が Right のティック数以上である場合は、true を返します。 それ以外の場合、関数は false を返します。

2) LeftRight より後であるか、等しい場合は、true を返します。 それ以外の場合、false を返します。

operator<=>

宇宙船演算子は、operator== と共に、次の型の <<=>>=!= の演算子を合成します。

1)
constexpr bool operator<=>(const day& Left, const day& Right) noexcept; // C++20

constexpr std::strong_ordering operator<=>(const month& Left, const month& Right) noexcept; // C++20

constexpr strong_ordering operator<=>(const month_day& Left, const month_day& Right) noexcept; // C++20

constexpr std::strong_ordering operator<=>(const year& Left, const year& Right ) noexcept; // C++20

constexpr strong_ordering operator<=>(const year_month& Left, const year_month& Right) noexcept; // C++20

template<class Clock, class Duration1, three_­way_­comparable_­with<Duration1> Duration2>
    constexpr auto operator<=>(const time_point<Clock, Duration1>& Left, const time_point<Clock, Duration2>& Right); // C++20

template<class Rep1, class Period1, class Rep2, class Period2>
  requires three_­way_­comparable<typename CT::rep>
    constexpr auto operator<=>(const duration<Rep1, Period1>& Left, const duration<Rep2, Period2>& Right);

2)
constexpr strong_ordering operator<=>(const month_day_last& Left, const month_day_last& Right) noexcept;

3)
constexpr strong_ordering operator<=>(const year_month_day_last& Left, const year_month_day_last& Right) noexcept;

4)
strong_ordering operator<=>(const time_zone_link& Left, const time_zone_link& Right) noexcept;

パラメーター

Left, Right
比較対象の daydurationmonthmonth_daymonth_day_lasttime_pointtime_zone_linkyearyear_monthyear_month_dayyear_month_day_last

戻り値

1)
Left == Right の場合は 0
Left < Right の場合は < 0
Left > Right の場合は > 0

2)
次と同等: Left.month() <=> Right.month()

3)
次と同等:

if (auto c = Left.year() <=> Right.year(); c != 0) return c;
return Left.month_day_last() <=> Right.month_day_last();

4)
次と同等:

Left.name() <=> Right.name()

例: operator<=>

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono; // for day and 'd' literals

int main()
{
    day d1{3};
    day d2{2};

    if ((d1 <=> d2) == 0)
    {
        std::cout << "equal\n";
    }
    else if ((d1 <=> d2) < 0)
    {
        std::cout << "d1 < d2\n";
    }
    else if ((d1 <=> d2) > 0)
    {
        std::cout << "d1 > d2\n";
    }

    std::cout << std::boolalpha << (1d <= 1d) << ' ' << (1d != 2d) << ' ' << (2d > 3d);

    return 0;
}
d1 < d2
true true false 

operator<<

次の型をストリームに出力します。

// 1) day
template <class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const day& d); // C++20

// 2) hh_mm_ss
template<class CharT, class traits, class Duration>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const hh_mm_ss<Duration>& hms); // C++20

// 3) month
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const month& m); // C++20

// 4) month_day
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const month_day& md); // C++20

// 5) month_day_last
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const month_day_last& mdl); // C++20

// 6) month_weekday
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const month_weekday& mwd); // C++20

// 7) month_weekday_last
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const month_weekday_last& mwdl); // C++20

// 8) weekday
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const weekday& wd); // C++20

// 9) weekday_indexed
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const weekday_indexed& wdi); // C++20

// 10) weekday_last
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const weekday_last& wdl); // C++20

// 11) year
template <class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const year& y); // C++20

// 12) year_month
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const year_month& ym); // C++20

// 13) year_month_day
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const year_month_day& ymd); // C++20

// 14) year_month_day_last
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const year_month_day_last& ymdl); // C++20

// 15) year_month_weekday
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const year_month_weekday& ymwd); // C++20

// 16) year_month_weekday_last
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const year_month_weekday_last& ymwdl); // C++20

// 17) tai_time
template<class CharT, class Traits, class Duration>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const tai_time<Duration>& t); // C++20

// 18) utc_time
template<class CharT, class Traits, class Duration>
basic_ostream<CharT, traits>&
operator<<(basic_ostream<CharT, Traits>& os, const utc_time<Duration>& t); // C++20

// 19) gps_time
template<class CharT, class Traits, class Duration>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const gps_time<Duration>& t); // C++20

// 20) local_time
template<class CharT, class Traits, class Duration>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const local_time<Duration>& t); // C++20

// 21) sys_info
template<class CharT, class Traits>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const sys_info& si);

// 22) local_info
template<class CharT, class Traits>
basic_ostream<charT, traits>&
operator<<(basic_ostream<CharT, Traits>& os, const local_info& li);

// 23) zoned_time
template<class CharT, class Traits, class Duration, class TimeZonePtr>
basic_ostream<CharT, Traits>&
operator<<(basic_ostream<CharT, Traits>& os, const zoned_time<Duration, TimeZonePtr>& zt);

パラメーター

CharT
ストリームから読み取られる 1 文字のデータ型。文字列に格納されます。 C++ 標準ライブラリはこのクラス テンプレートの特殊化を提供し、型 char の要素の場合は型定義 stringwchar_t の場合は wstringchar16_t の場合は u16stringchar32_t の場合は u32string です。

Traits
basic_stringbasic_istream の特殊化の CharT 属性を記述します。

os
day 値の出力先の出力ストリーム。

d
出力する day

hms
出力する hh_mm_ss

li
出力する local_info

m
出力する month

md
出力する month_day

mdl
出力する month_day_last

mwd
出力する month_weekday

mwdl
出力する month_weekday_last

si
出力する sys_info

t
出力する local_timegps_timetai_time、または utc_time

TimeZonePtr
zoned_time に格納されている time_zone へのポインター。

wd
出力する weekday

wdi
出力する weekday_indexed

wdl
出力する weekday_last

y
出力する year

ym
出力する year_month

ymd
出力する year_month_day

ymdl
出力する year_month_day_last

ymwd
出力する year_month_weekday

ymwdl
出力する year_month_weekday_last

zt
出力する zoned_time

戻り値

渡された出力ストリーム、os

解説

1) day 値は、10 進数として出力され、結果が 1 桁の場合は先頭に 0 が付きます。 !d.ok() の場合は、" isn't a valid day" が出力に追加されます。

2) hh_mm_ss 値は、時間:分:秒:1/1000 秒として出力されます。 たとえば、"00:00:05.721" です。

3) os に関連付けられたロケールを使用した、省略形の月の名前が出力されます。 たとえば、Jan のようにします。 !m.ok() の場合は、" is not a valid month" が出力に追加されます。

4) os に関連付けられたロケールを使用した、省略形の月の名前が出力されます。その後に日付が続き、結果が 1 桁の場合は先頭に 0 が付きます。 たとえば、Jan/05 のようにします。 !md.ok() の場合は、" is not a valid month" が月の出力に追加されることがあり、"is not a valid day" が日の出力に追加されることがあります。 たとえば、204 is not a valid month/204 is not a valid day のようにします。

5) os に関連付けられたロケールを使用した、省略形の月の名前。その後に /last が続きます。 たとえば、Jan/last のようにします。

6) os に関連付けられたロケールを使用した、省略形の曜日の名前。その後にその月の n 番目の曜日であるかが続きます。これは角かっこで囲まれて表されます。 たとえば、Mon[1] のようにします。

7) os に関連付けられたロケールを使用した、省略形の曜日の名前。その後にその月の最後の曜日であるかが続きます。これは角かっこで囲まれて表されます。 たとえば、Jan/Mon[last] のようにします。

8) os に関連付けられたロケールを使用した、省略形の曜日の名前が出力されます。 !wd.ok() の場合は、" is not a valid weekday" が出力に追加されます。

9) os に関連付けられたロケールを使用した、省略形の曜日の名前が出力されます。その後にその月の曜日が角かっこで囲まれて続きます。 たとえば、Mon[3] のようにします。 !wd.ok() の場合は、" is not a valid weekday" が曜日の出力に追加されることがあり、"is not a valid index" が曜日インデックスの出力の出力に追加されることがあります。

10) os に関連付けられたロケールを使用した、月の最後の曜日が出力されます。その後に [last] が続き、その後に日付が続きます。 たとえば、Tue[last] 2019-10-29 のようにします。 !wd.ok() の場合は、" is not a valid weekday" が曜日の出力に追加されることがあり、"is not a valid index" が曜日インデックスの出力の出力に追加されることがあります。

11) 結果が 4 桁より短い場合は、年の左側に 0 (ゼロ) が埋め込まれます。 !y.ok() の場合は、" is not a valid year" が出力に追加されます。

12) year_month は yyy-mm-dd の形式で出力されます。 ym.okfalse を返した場合は、" is not a valid date" が追加されます。

13) year_month_day は yyyy-mm-dd の形式で出力されます。 ymd.okfalse を返した場合は、" is not a valid date" が追加されます。

14) year_month_day_last は yyyy/month/last の形式で出力されます。 たとえば、2020/May/last のようにします。

15) year_month_weekday は yyyy/month/weekday[index] の形式で出力されます。 たとえば、1996/Jan/Wed[1] のように指定します。

16) year_month_weekday_last は yyyy/month/weekday[last] の形式で出力されます。 たとえば、1996/Jan/Wed[last] のように指定します。

17) tai_time は yyyy-mm-dd hh:mm:ss.sssssss の形式で出力されます。 たとえば、2021-08-13 23:23:08.4358666 のように指定します。

18) utc_time は yyyy-mm-dd hh:mm:ss.sssssss の形式で出力されます。 たとえば、2021-08-13 23:23:08.4358666 のように指定します。

19) gps_time は yyyy-mm-dd hh:mm:ss.sssssss の形式で出力されます。 たとえば、2021-08-13 23:23:08.4358666 のように指定します。

20) local_time はクロックのエポック以降の秒数として出力されます。 これは os << std::chrono::sys_time<Duration>(some_local_time.time_since_epoch()); による出力と同様です。 たとえば、some_local_time が 2021 年 8 月 18 日の午後 3: 13 である場合、出力は 1597792380 です。

21) Microsoft の実装では、sys_info はその beginendoffsetsaveabbrev の各フィールドとして出力されます。 例: begin: 2021-03-14 10:00:00, end: 2021-11-07 09:00:00, offset: -25200s, save: 60min, abbrev: PDT

22) Microsoft の実装では、local_info は yyyy-mm-dd hh:mm::ss.ssssss として出力されます。 たとえば、2021-09-17 13:55:59.6590120 のように指定します。

23) zoned_time でのローカル時刻 (zt.get_local_time() として取得されます) は、yyyy-mm-dd hh:mm:ss timezone の形式を使用して出力されます。 たとえば、2021-09-15 10:45:00 GMT-6 のように指定します。

例: operator<<

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    std::cout << utc_clock::now() << '\n';

    year_month ym{ 2021y / April };
    std::cout << ym;
    return 0;
}
2021-08-16 20:47:05.6299822
2021/Apr

operator modulo

duration に対する剰余演算の演算子。

1)
template <class Rep1, class Period1, class Rep2>
constexpr duration<Rep1, Period1, Rep2>::type
   operator%(
      const duration<Rep1, Period1>& Dur,
      const Rep2& Div);

2)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr typename common_type<duration<Rep1, _Period1>, duration<Rep2, Period2>>::type
   operator%(
     const duration<Rep1, Period1>& Left,
     const duration<Rep2, Period2>& Right);

パラメーター

Dur
duration オブジェクト。

Div
整数値。

Left
被除数。 剰余は、被除数を除数で除算した後の余りです。

Right
右側の duration オブジェクト、除数。

戻り値

1) 間隔の長さが DurDiv で除算した剰余となっている duration オブジェクトを返します。

2) LeftRight で除算した剰余を表す値を返します。

durationoperator/

duration オブジェクトの除算演算子。

1)
template <class Rep1, class Period1, class Rep2>
constexpr duration<typename common_type<Rep1, Rep2>::type, Period1>
   operator/(
     const duration<Rep1, Period1>& Dur,
     const Rep2& Div);

2)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr typename common_type<Rep1, Rep2>::type
   operator/(
     const duration<Rep1, Period1>& Left,
     const duration<Rep2, Period2>& Right);

パラメーター

Dur
duration オブジェクト。

Div
整数値。

Left\w 左側の duration オブジェクト。

Right
右側の duration オブジェクト。

戻り値

1) 間隔の長さが Div の値で除算した Dur の長さである duration オブジェクト。

2) LeftRight の間隔の長さの比率。

is_convertible<Rep2, common_type<Rep1, Rep2>>true にならない限り、かつ Rep2duration のインスタンス化ではない限り、最初の演算子はオーバーロードの解決に関与しません。 詳細については、「<type_traits>」を参照してください。

カレンダー日付の operator/

次の形式でカレンダー日付を作成するための構文を提供します。

month/day/year
day/month/year
year/month/day

day は次のように置き換えることができます。

last
その月の n 番目の日を示す weekday[n]
その月の最後の weekday を示す weekday[last]

部分日付は次のように形成できます。

year_month ym = 2015y/April;
month_day md1 = April/4;
month_day md2 = 4d/April;

整数は、解釈があいまいでない限り使用できます。

/////////  returns year_month

// 1
constexpr year_month
operator/(const year& y, const month& m) noexcept; // C++20

// 2
constexpr year_month
operator/(const year& y, int m) noexcept; // C++20
 
/////////  returns month_day

// 3
constexpr month_day
operator/(const month& m, const day& d) noexcept; // C++20

// 4
constexpr month_day
operator/(const month& m, int d) noexcept; // C++20

// 5
constexpr month_day
operator/(int m, const day& d) noexcept; // C++20

// 6
constexpr month_day
operator/(const day& d, const month& m) noexcept; // C++20

// 7
constexpr month_day
operator/(const day& d, int m) noexcept; // C++20

/////////  returns month_day_last

// 8
constexpr month_day_last
operator/(const month& m, last_spec) noexcept; // C++20

// 9
constexpr month_day_last
operator/(int m, last_spec) noexcept; // C++20

// 10
constexpr month_day_last
operator/(last_spec, const month& m) noexcept; // C++20

// 11
constexpr month_day_last
operator/(last_spec, int m) noexcept; // C++20

/////////  returns month_weekday

// 12
constexpr month_weekday
operator/(const month& m, const weekday_indexed& wdi) noexcept; // C++20

// 13
constexpr month_weekday
operator/(int m, const weekday_indexed& wdi) noexcept; // C++20

// 14
constexpr month_weekday
operator/(const weekday_indexed& wdi, const month& m) noexcept; // C++20

// 15
constexpr month_weekday
operator/(const weekday_indexed& wdi, int m) noexcept; // C++20

/////////  returns month_weekday_last

// 16
constexpr month_weekday_last
operator/(const month& m, const weekday_last& wdl) noexcept; // C++20

// 17
constexpr month_weekday_last
operator/(int m, const weekday_last& wdl) noexcept; // C++20

// 18
constexpr month_weekday_last
operator/(const weekday_last& wdl, const month& m) noexcept; // C++20

// 19
constexpr month_weekday_last
operator/(const weekday_last& wdl, int m) noexcept; // C++20

/////////  returns year_month_day

// 20
constexpr year_month_day
operator/(const year_month& ym, const day& d) noexcept; // C++20

// 21
constexpr year_month_day
operator/(const year_month& ym, int d) noexcept; // C++20

// 22
constexpr year_month_day
operator/(const year& y, const month_day& md) noexcept; // C++20

// 23
constexpr year_month_day
operator/(int y, const month_day& md) noexcept; // C++20

// 24
constexpr year_month_day
operator/(const month_day& md, const year& y) noexcept; // C++20

// 25
constexpr year_month_day
operator/(const month_day& md, int y) noexcept; // C++20

/////////  returns year_month_day_last

// 26
constexpr year_month_day_last
operator/(const year_month& ym, last_spec) noexcept; // C++20

// 27
constexpr year_month_day_last
operator/(const year& y, const month_day_last& mdl) noexcept; // C++20

// 28
constexpr year_month_day_last
operator/(int y, const month_day_last& mdl) noexcept; // C++20

// 29
constexpr year_month_day_last
operator/(const month_day_last& mdl, const year& y) noexcept; // C++20

// 30
constexpr year_month_day_last
operator/(const month_day_last& mdl, int y) noexcept; // C++20

/////////  returns year_month_weekday

// 31
constexpr year_month_weekday
operator/(const year_month& ym, const weekday_indexed& wdi) noexcept; // C++20

// 32
constexpr year_month_weekday
operator/(const year& y, const month_weekday& mwd) noexcept; // C++20

// 33
constexpr year_month_weekday
operator/(int y, const month_weekday& mwd) noexcept; // C++20

// 34
constexpr year_month_weekday
operator/(const month_weekday& mwd, const year& y) noexcept; // C++20

// 35
constexpr year_month_weekday
operator/(const month_weekday& mwd, int y) noexcept; // C++20

/////////  returns year_month_weekday_last

// 36
constexpr year_month_weekday_last
operator/(const year_month& ym, const weekday_last& wdl) noexcept; // C++20

// 37
constexpr year_month_weekday_last
operator/(const year& y, const month_weekday_last& mwdl) noexcept; // C++20

// 38
constexpr year_month_weekday_last
operator/(int y, const month_weekday_last& mwdl) noexcept; // C++20

// 39
constexpr year_month_weekday_last
operator/(const month_weekday_last& mwdl, const year& y) noexcept; // C++20

// 40
constexpr year_month_weekday_last
operator/(const month_weekday_last& mwdl, int y) noexcept; // C++20

パラメーター

d
日。 [1, 31] の範囲内の整数として、または day として指定されます。

lastspec
シーケンス内の最後の項目を示す空のタグの種類。 たとえば、2021y/May/last は 2021 年 5 月の最後の日です。

m
月。 [1, 12] の範囲内の整数として、または month として指定されます。

md
月と日。

mdl
指定された月の最後の日。

mwd
指定された月の n 番目の曜日。

mwdl
指定された月の最後の曜日。

wdi
曜日インデックス (weekday_indexed)。 たとえば、weekday_indexed(Monday, 1) は月の最初の月曜日です。

wdl
月の最後の曜日。 たとえば、Monday[last] は月の最後の月曜日です。

y
年。 整数として、または year として指定されます。

ym
年と月。

戻り値

1) year_month(y, m)
2) year_month(y, month(m))
3) month_day(m, d)
4) month_day(m, day(d))
5) month_day(month(m), d)
6) month_day(m, d)
7) month_day(month(m), d)
8) month_day_last(m)
9) month_day_last(month(m))
10) month_day_last(m)
11) month_day_last(month(m))
12) month_weekday(m, wdi)
13) month_weekday(month(m), wdi)
14) month_weekday(m, wdi)
15) month_weekday(month(m), wdi)
16) month_weekday_last(m, wdl)
17) month_weekday_last(month(m), wdl)
18) month_weekday_last(m, wdl)
19) month_weekday_last(month(m), wdl)
20) year_month_day(ym.year(), ym.month(), d)
21) year_month_day(ym.year(), ym.month(), day(d))
22) year_month_day(y, md.month(), md.day())
23) year_month_day(year(y), md.month(), md.day())
24) year_month_day(y, md.month(), md.day())
25) year_month_day(year(y), md.month(), md.day())
26) year_month_day_last(ym.year(), month_day_last(ym.month()))
27) year_month_day_last(y, mdl)
28) year_month_day_last(year(y), mdl)
29) year_month_day_last(y, mdl)
30) year_month_day_last(year(y), mdl)
31) year_month_weekday(ym.year(), ym.month(), wdi)
32) year_month_weekday(y, mwd.month(), mwd.weekday_indexed())
33) year_month_weekday(year(y), mwd.month(), mwd.weekday_indexed())
34) year_month_weekday(y, mwd.month(), mwd.weekday_indexed())
35) year_month_weekday(year(y), mwd.month(), mwd.weekday_indexed())
36) year_month_weekday_last(ym.year(), ym.month(), wdl)
37) year_month_weekday_last(y, mwdl.month(), mwdl.weekday_last())
38) year_month_weekday_last(year(y), mwdl.month(), mwdl.weekday_last())
39) year_month_weekday_last(y, mwdl.month(), mwdl.weekday_last())
40) year_month_weekday_last(year(y), mwdl.month(), mwdl.weekday_last())

例: カレンダー日付の operator/

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    month m{ July }; // Jul
    month_day md{ April / 4 }; // Apr/04
    month_day md2{ 4d / April }; // Apr/04
    month_day_last mdl{ January / last }; // Jan/last
    month_weekday mw{ 11 / Monday[1] }; // Nov/Mon[1]
    month_weekday_last mwl{ January / Monday[last] }; // Jan/Mon[last]
    weekday wd{ Monday }; // Mon
    weekday_indexed wdi{ Monday, 1 }; // Mon[1]
    year_month ym{ 2021y / April }; // 2021/Apr
    year_month_day ymd{ January / 1d / 2021y }; // 2021-01-01
    year_month_day ymd2{ 2021y / 5 / 7 }; // 2021-05-07
    year_month_day_last ymdl{ April / last / 1975 }; // 1975/Apr/last
    year_month_weekday ymw{ 1997y / January / Wednesday[1] }; // 1997/Jan/Wed[1]
    year_month_weekday_last ymwl{ 1997y / January / Wednesday[last] }; // 1997/Jan/Wed[last]
    int yearValue{ 2021 / 4 / 4 }; // 126

    std::cout << m << '\n' << md << '\n' << md2 << '\n' << mdl << '\n' << mw
        << '\n' << mwl << '\n' << wd << '\n' << wdi << '\n'
        << ym << '\n' << ymd << '\n' << ymd2 << '\n' << ymdl
        << '\n' << ymw << '\n' << ymwl << '\n' << yearValue;

    return 0;
}
Jul
Apr/04
Apr/04
Jan/last
Nov/Mon[1]
Jan/Mon[last]
Mon
Mon[1]
2021/Apr
2021-01-01
2021-05-07
1975/Apr/last
1997/Jan/Wed[1]
1997/Jan/Wed[last]
126