Share via


<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_point 对象,该对象表示从时间点 Time 偏移了 Dur 时间间隔的时间点。

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) 将减去的持续时间转换为其通用类型后,返回 duration,其计时周期计数等于从 Left 中的计时周期数减去的 Right 中的计时周期数。

2) 返回一个 time_point,它所表示的时间点从 Time 指定的时间点开始,偏移 Dur 表示的时间间隔的求反值。

3) 返回一个 duration 对象,该对象表示 LeftRight 之间的时间间隔。

4) 返回 d-ds.count() 的结果。 如果结果超出范围 [0, 255],则不指定结果。

5) 如果 m.ok() == truems.ok() == true,则返回减去两个月份值或减去月数的结果。 结果将在 [1, 12] 范围内。 如果结果为负,则回绕。 例如,将一月减去一个月 (month m1{1} - months{1};) 得到 12(十二月)。

6) 返回 LeftRight 之间的月份差

7) 如果 Left.ok() == trueRight.ok() == true,则返回 [days{0}, days{6}] 范围内的 weekday

8) 返回两个工作日之间的天数。

9) 返回 year(int(y)-ys.count)())

10) 返回 years(int(y) - int(y2))。 减去两个 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) 两个 duration 对象不表示相同的计时周期数。
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
左侧的 durationtime_point 对象。

Right
右侧的 durationtime_point 对象。

返回值

1) 如果 LeftRight 共有的类型的计时周期数不相等,则返回 true。 否则返回 false
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
一个整数值。

返回值

返回一个 duration 对象,其时间间隔长度为 Dur 的长度乘以 Mult

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
左侧的 durationtime_point 对象。

Right
右侧的 durationtime_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
左侧的 durationtime_point 对象。

Right
右侧的 durationtime_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
左侧的 durationtime_point 对象。

Right
右侧的 durationtime_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
左侧的 durationtime_point 对象。

Right
右侧的 durationtime_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
要从流中读取和存储在字符串中的单个字符的数据类型。 C++ 标准库使用类型为 char 的元素的类型定义 string、类型为 wchar_t 的元素的类型定义 wstring、类型为 char16_t 的元素的类型定义 u16string 以及类型为 char32_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_timeutc_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 值以十进制数的形式输出,如果结果是个位数,则在前面加一个零。 如果 !d.ok(),则将“isn't a valid day”追加到输出中。

2) hh_mm_ss 值以小时:分钟:秒:千分之一秒的形式输出。 例如,"00:00:05.721

3) 输出缩写月份名称(使用与 os 关联的区域设置)。 例如 Jan。 如果 !m.ok(),则将 " is not a valid month" 追加到输出中。

4) 输出缩写月份名称(使用与 os 关联的区域设置),后跟日期,如果结果是个位数,则在前面加一个零。 例如 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) 如果结果少于四位数,则用 0(零)将年份向左填充为四位数。 如果 !y.ok(),则将 " is not a valid year" 追加到输出中。

12) year_month 以 yyyy-mm-dd 的形式输出。 如果 ym.ok 返回 false,则追加 " is not a valid date"

13) year_month_day 以 yyyy-mm-dd 的形式输出。 如果 ymd.ok 返回 false,则追加 " 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 时区格式输出。 例如: 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) 返回一个 duration 对象,其时间间隔长度为 DurDiv 取模的长度。

2) 返回一个值,该值表示 LeftRight 取模的值。

duration,表示集 operator/

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) 一个 duration 对象,其时间间隔长度是 Dur 除以值 Div 的长度。

2) LeftRight 的时间间隔长度之比。

除非 is_convertible<Rep2, common_type<Rep1, Rep2>>“保持为 true”,并且 Rep2 不是 duration 的实例化,否则第一个运算符不参与重载决策。 有关详细信息,请参阅 <type_traits>

operator/(用于日历日期)

提供以下列形式创建日历日期的语法:

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

可以将 day 替换为:

last
weekday[n](表示该月的第 n 天)
weekday[last](表示该月的最后一个 weekday)。

分部日期可按以下形式组成:

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
一个空标记类型,指示 s 序列中的最后一项。 例如,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