11---
22description : " Learn more about: <chrono> operators"
33title : " <chrono> operators"
4- ms.date : " 6/24 /2021"
4+ ms.date : " 6/30 /2021"
55f1_keywords: ["chrono/std::operator modulo", "chrono/std::operator+", "chrono/std::chrono::day::operator+", "chrono/std::chrono::duration::operator+", "chrono/std::chrono::month::operator+", "chrono/std::chrono::time_point::operator+", "chrono/std::chrono::weekday::operator+", "chrono/std::chrono::year_month::operator+", "chrono/std::chrono::year::operator+", "chrono/std::chrono::year_month_day::operator+", "chrono/std::chrono::year_month_day_last::operator+", "chrono/std::chrono::year_month_weekday::operator+", "chrono/std::chrono::year_month_weekday_last::operator+", "chrono/std::operator!=", "chrono/std::operator*", "chrono/std::operator/", "chrono/std::operator-", "chrono/std::operator<", "chrono/std::operator<=", "chrono/std::operator<", "chrono/std::operator==", "chrono/std::operator>>", "chrono/std::operator<=>", "chrono/std::chrono::day::operator<=>", "chrono/std::chrono::month::operator<=>", "chrono/std::chrono::year::operator<=>", "chrono/std::chrono::year_month::operator<=>", "chrono/std::chrono::time_point::operator<=>", "chrono/std::chrono::duration::operator<=>", "chrono/std::chrono::month_day_last::operator<=>", "chrono/std::chrono::year_month_day_last::operator<=>", "chrono/std::operator==", "chrono/std::chrono::year::operator<==>", "chrono/std::chrono::day::operator==", "chrono/std::chrono::duration::operator==", "chrono/std::chrono::month::operator==", "chrono/std::chrono::month_day::operator==", "chrono/std::chrono::month_day_last::operator==", "chrono/std::chrono::month_weekday::operator==", "chrono/std::chrono::month_weekday_last::operator==", "chrono/std::chrono::time_point::operator==", "chrono/std::chrono::weekday::operator==", "chrono/std::chrono::year_month::operator==", "chrono/std::chrono::year::operator==", "chrono/std::chrono::year_month_day::operator==", "chrono/std::chrono::year_month_day_last::operator==", "chrono/std::chrono::year_month_weekday_last::operator==", "chrono/std::chrono::year_month_weekday::operator==", "chrono/std::chrono::month_weekday_last::operator==", "chrono/std::chrono::weekday::operator==", "chrono/std::chrono::weekday_last::operator==", "chrono/std::chrono::year_month_weekday_indexed::operator==", "chrono/std::chrono::year_month_weekday_last::operator==", "chrono/std::chrono::year::operator==", "chrono/std::chrono::year_month::operator==", "chrono/std::chrono::year_month_day::operator==", "chrono/std::chrono::year_month_day_last::operator==", "chrono/std::operator-", "chrono/std::chrono::day::operator-", "chrono/std::chrono::duration::operator-", "chrono/std::chrono::month::operator-", "chrono/std::chrono::time_point::operator-", "chrono/std::chrono::weekday::operator-", "chrono/std::chrono::year_month::operator-", "chrono/std::chrono::year::operator-", "chrono/std::chrono::year_month_day::operator-", "chrono/std::chrono::year_month_day_last::operator-", "chrono/std::chrono::year_month_weekday::operator-", "chrono/std::chrono::year_month_weekday_last::operator-", "chrono/std::chrono::day::operator<<", "chrono/std::chrono::hh_mm_ss::operator<<", "chrono/std::chrono::month_day::operator<<", "chrono/std::chrono::month_day_last::operator<<", "chrono/std::chrono::month_weekday::operator<<", "chrono/std::chrono::month_weekday_last::operator<<", "chrono/std::chrono::weekday::operator<<", "chrono/std::chrono::weekday_indexed::operator<<", "chrono/std::chrono::weekday_last::operator<<", "chrono/std::chrono::year::operator<<", "chrono/std::chrono::year_month_day::operator<<", "chrono/std::chrono::year_month_day_last::operator<<", "chrono/std::chrono::year_month_weekday::operator<<"]
66---
77# ` <chrono> ` operators
@@ -80,6 +80,7 @@ constexpr year_month_day_last operator+(const months& dm, const year_month_day_l
8080
818112 )
8282constexpr year_month_day_last operator+(const year_month_day_last& ymdl, const years& dy) noexcept; // C++ 20
83+ constexpr year_month_day_last operator+(const years& dy, const year_month_day_last& ymdl) noexcept; // C++ 20
8384
848513 )
8586constexpr year_month_weekday operator+(const year_month_weekday& ymwd, const months& dm) noexcept; // C++ 20
@@ -173,8 +174,6 @@ int main()
173174}
174175```
175176
176- Output:
177-
178177``` output
17917803
180179Feb
@@ -363,7 +362,7 @@ int main()
363362
364363 // year_month_day_last
365364 year_month_day_last ymdl = June / last / 2021;
366- std::cout << ymdl + years{1} + months{1} << '\n'; // 2022/Jul/last
365+ std::cout << ymdl - years{1} - months{1} << '\n'; // 2022/Jul/last
367366
368367 // year_month_weekday
369368 year_month_weekday ymw{ year(1997) / January / Wednesday[1] };
3873862020
3883872021/Apr
3893882019/Jun
390- 2022/Jul /last
389+ 2020/May /last
3913901996/Dec/Wed[1]
3923911996/Jan/Wed[1]
3933921996/Dec/Wed[last]
396395
397396## <a name =" op_unary_negate " ></a > Unary ` operator- `
398397
399- Negates the following types:
400-
401- - [ ` duration ` ] ( duration-class.md )
398+ Negates a [ ` duration ` ] ( duration-class.md ) .
402399
403400``` cpp
404401constexpr common_type_t <duration> operator -() const ;
@@ -425,15 +422,13 @@ int main()
425422}
426423```
427424
428- Output:
429-
430425``` output
431426-120ms
432427```
433428
434429## <a name =" op_neq " ></a > ` operator!= `
435430
436- Inequality operator for [ ` duration ` ] ( ../standard-library/ duration-class.md) or [ ` time_point ` ] ( ../standard-library/ time-point-class.md) objects.
431+ Inequality operator for [ ` duration ` ] ( duration-class.md ) or [ ` time_point ` ] ( time-point-class.md ) objects.
437432
438433``` cpp
439434template <class Rep1 , class Period1, class Rep2, class Period2>
@@ -555,6 +550,7 @@ Each function returns `!(Right < Left)`.
555550## <a name =" op_eq_eq " ></a > ` operator== `
556551
557552Determines whether:
553+
5585541\) [ ` duration ` ] ( duration-class.md ) objects represent time intervals that have the same length.\
5595552\) [ ` time_point ` ] ( time-point-class.md ) objects represent the same point in time.\
5605563\) [ ` day ` ] ( day-class.md ) objects represent the same day.\
@@ -568,9 +564,10 @@ Determines whether:
56856411\) [ ` weekday_indexed ` ] ( weekdayindexed-class.md ) represent the same weekday index.\
56956512\) [ ` year ` ] ( year-class.md ) represent the same year.\
57056613\) [ ` year_month ` ] ( year-month-class.md ) represent the same year and month.\
571- 14\) [ ` year_month_day ` ] ( year-month-day-class.md ) represent the same year, month, and day.
572- 15\) [ ` year_month_day_last ` ] ( year-month-day-last-class.md ) represent the same last day of the year and month.
573- 16\) [ ` year_month_weekday ` ] ( year-month-weekday-class.md ) represent the same weekday, year, and month.
567+ 14\) [ ` year_month_day ` ] ( year-month-day-class.md ) represent the same year, month, and day.\
568+ 15\) [ ` year_month_day_last ` ] ( year-month-day-last-class.md ) represent the same last day of the year and month.\
569+ 16\) [ ` year_month_weekday ` ] ( year-month-weekday-class.md ) represent the same weekday, year, and month.\
570+ 17\) [ ` year_month_weekday_last ` ] ( year-month-weekday-last-class.md ) represent the same last weekday of the month, year, and month.
574571
575572``` cpp
576573// 1) duration<Rep, Period>
@@ -626,6 +623,9 @@ constexpr bool operator==(const year_month_day_last& Left, const year_month_day_
626623
627624// 16) year_month_weekday
628625constexpr bool operator==(const year_month_weekday& Left, const year_month_weekday& Right) noexcept; // C++ 20
626+
627+ // 17) year_month_weekday_last
628+ constexpr bool operator==(const year_month_weekday_last& Left, const year_month_weekday_last& Right) noexcept; // C++ 20
629629```
630630
631631### Parameters
@@ -640,7 +640,7 @@ The right object to compare.
640640
6416411\) Returns **`true`** if *`Left`* and *`Right`* represent time intervals that have the same length. Otherwise, the function returns **`false`**.\
6426422\) Returns **`true`** if *`Left`* and *`Right`* represent the same point in time. Otherwise, returns **`false`**.\
643- 3-15 \) Returns **`true`** if *`Left`* and *`Right`* have the same value. Otherwise, returns **`false`**.
643+ 3-17 \) Returns **`true`** if *`Left`* and *`Right`* have the same value. Otherwise, returns **`false`**.
644644
645645## <a name="op_gt"></a> `operator>`
646646
@@ -715,12 +715,18 @@ The spaceship operator, in conjunction with `operator==`, synthesizes operators
715715```cpp
716716// 1)
717717constexpr bool operator<=>(const day& Left, const day& Right) noexcept; // C++ 20
718+
718719constexpr std::strong_ordering operator<=>(const month& Left, const month& Right) noexcept; // C++ 20
720+
719721constexpr strong_ordering operator<=>(const month_day& Left, const month_day& Right) noexcept; // C++ 20
722+
720723constexpr std::strong_ordering operator<=>(const year& Left, const year& Right ) noexcept; // C++ 20
724+
721725constexpr strong_ordering operator<=>(const year_month& Left, const year_month& Right) noexcept; // C++ 20
726+
722727template<class Clock, class Duration1, three_way_comparable_with<Duration1> Duration2>
723728 constexpr auto operator<=>(const time_point<Clock, Duration1>& Left, const time_point<Clock, Duration2>& Right); // C++ 20
729+
724730template<class Rep1, class Period1, class Rep2, class Period2>
725731 requires three_way_comparable<typename CT::rep>
726732 constexpr auto operator<=>(const duration<Rep1, Period1>& Left, const duration<Rep2, Period2>& Right);
@@ -739,15 +745,15 @@ The [`day`](day-class.md), [`duration`](duration-class.md), [`month`](month-clas
739745
740746### Return value
741747
742- 1\)
748+ 1\) \
743749` 0 ` if ` Left == Right ` \
744750` < 0 ` if ` Left < Right ` \
745751` > 0 ` if ` Left > Right `
746752
747- 2\)
753+ 2\) \
748754Equivalent to: ` Left.month() <=> Right.month() `
749755
750- 3\)
756+ 3\) \
751757Equivalent to:
752758
753759``` cpp
@@ -788,8 +794,6 @@ int main()
788794}
789795```
790796
791- Output:
792-
793797``` output
794798d1 < d2
795799true true false
@@ -1374,8 +1378,8 @@ int main()
13741378 month_day_last mdl{ January / last }; // Jan/last
13751379 month_weekday mw{ 11 / Monday[1] }; // Nov/Mon[1]
13761380 month_weekday_last mwl{ January / Monday[last] }; // Jan/Mon[last]
1377- weekday wd{ Monday }; // Monday
1378- weekday_indexed wdi{ Monday, 1}; // Monday [1]
1381+ weekday wd{ Monday }; // Mon
1382+ weekday_indexed wdi{ Monday, 1 }; // Mon [1]
13791383 year_month ym{ 2021y / April }; // 2021/Apr
13801384 year_month_day ymd{ January / 1d / 2021y }; // 2021-01-01
13811385 year_month_day ymd2{ 2021y / 5 / 7 }; // 2021-05-07
@@ -1385,12 +1389,13 @@ int main()
13851389 int yearValue{ 2021 / 4 / 4 }; // 126
13861390
13871391 std::cout << m << '\n' << md << '\n' << md2 << '\n' << mdl << '\n' << mw
1388- << '\n' << mwl << '\n' << wd << '\n' << wdi << '\n'
1389- << ym << '\n' << ymd2 << '\n' << ymdl
1390- << '\n' << ymw << '\n' << ymwl << '\n' << yearValue;
1392+ << '\n' << mwl << '\n' << wd << '\n' << wdi << '\n'
1393+ << ym << '\n' << ymd << '\n' << ymd2 << '\n' << ymdl
1394+ << '\n' << ymw << '\n' << ymwl << '\n' << yearValue;
13911395
13921396 return 0;
13931397}
1398+ ```
13941399
13951400``` output
13961401Jul
@@ -1402,6 +1407,7 @@ Jan/Mon[last]
14021407Mon
14031408Mon[1]
140414092021/Apr
1410+ 2021-01-01
140514112021-05-07
140614121975/Apr/last
140714131997/Jan/Wed[1]
0 commit comments