1
1
---
2
2
description : " Learn more about: <chrono> operators"
3
3
title : " <chrono> operators"
4
- ms.date : " 6/24 /2021"
4
+ ms.date : " 6/30 /2021"
5
5
f1_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<<"]
6
6
---
7
7
# ` <chrono> ` operators
@@ -80,6 +80,7 @@ constexpr year_month_day_last operator+(const months& dm, const year_month_day_l
80
80
81
81
12 )
82
82
constexpr 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
83
84
84
85
13 )
85
86
constexpr year_month_weekday operator+(const year_month_weekday& ymwd, const months& dm) noexcept; // C++ 20
@@ -173,8 +174,6 @@ int main()
173
174
}
174
175
```
175
176
176
- Output:
177
-
178
177
``` output
179
178
03
180
179
Feb
@@ -363,7 +362,7 @@ int main()
363
362
364
363
// year_month_day_last
365
364
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
367
366
368
367
// year_month_weekday
369
368
year_month_weekday ymw{ year(1997) / January / Wednesday[1] };
387
386
2020
388
387
2021/Apr
389
388
2019/Jun
390
- 2022/Jul /last
389
+ 2020/May /last
391
390
1996/Dec/Wed[1]
392
391
1996/Jan/Wed[1]
393
392
1996/Dec/Wed[last]
396
395
397
396
## <a name =" op_unary_negate " ></a > Unary ` operator- `
398
397
399
- Negates the following types:
400
-
401
- - [ ` duration ` ] ( duration-class.md )
398
+ Negates a [ ` duration ` ] ( duration-class.md ) .
402
399
403
400
``` cpp
404
401
constexpr common_type_t <duration> operator -() const ;
@@ -425,15 +422,13 @@ int main()
425
422
}
426
423
```
427
424
428
- Output:
429
-
430
425
``` output
431
426
-120ms
432
427
```
433
428
434
429
## <a name =" op_neq " ></a > ` operator!= `
435
430
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.
437
432
438
433
``` cpp
439
434
template <class Rep1 , class Period1, class Rep2, class Period2>
@@ -555,6 +550,7 @@ Each function returns `!(Right < Left)`.
555
550
## <a name =" op_eq_eq " ></a > ` operator== `
556
551
557
552
Determines whether:
553
+
558
554
1\) [ ` duration ` ] ( duration-class.md ) objects represent time intervals that have the same length.\
559
555
2\) [ ` time_point ` ] ( time-point-class.md ) objects represent the same point in time.\
560
556
3\) [ ` day ` ] ( day-class.md ) objects represent the same day.\
@@ -568,9 +564,10 @@ Determines whether:
568
564
11\) [ ` weekday_indexed ` ] ( weekdayindexed-class.md ) represent the same weekday index.\
569
565
12\) [ ` year ` ] ( year-class.md ) represent the same year.\
570
566
13\) [ ` 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.
574
571
575
572
``` cpp
576
573
// 1) duration<Rep, Period>
@@ -626,6 +623,9 @@ constexpr bool operator==(const year_month_day_last& Left, const year_month_day_
626
623
627
624
// 16) year_month_weekday
628
625
constexpr 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
629
629
```
630
630
631
631
### Parameters
@@ -640,7 +640,7 @@ The right object to compare.
640
640
641
641
1\) Returns **`true`** if *`Left`* and *`Right`* represent time intervals that have the same length. Otherwise, the function returns **`false`**.\
642
642
2\) 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`**.
644
644
645
645
## <a name="op_gt"></a> `operator>`
646
646
@@ -715,12 +715,18 @@ The spaceship operator, in conjunction with `operator==`, synthesizes operators
715
715
```cpp
716
716
// 1)
717
717
constexpr bool operator<=>(const day& Left, const day& Right) noexcept; // C++ 20
718
+
718
719
constexpr std::strong_ordering operator<=>(const month& Left, const month& Right) noexcept; // C++ 20
720
+
719
721
constexpr strong_ordering operator<=>(const month_day& Left, const month_day& Right) noexcept; // C++ 20
722
+
720
723
constexpr std::strong_ordering operator<=>(const year& Left, const year& Right ) noexcept; // C++ 20
724
+
721
725
constexpr strong_ordering operator<=>(const year_month& Left, const year_month& Right) noexcept; // C++ 20
726
+
722
727
template<class Clock, class Duration1, three_way_comparable_with<Duration1> Duration2>
723
728
constexpr auto operator<=>(const time_point<Clock, Duration1>& Left, const time_point<Clock, Duration2>& Right); // C++ 20
729
+
724
730
template<class Rep1, class Period1, class Rep2, class Period2>
725
731
requires three_way_comparable<typename CT::rep>
726
732
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
739
745
740
746
### Return value
741
747
742
- 1\)
748
+ 1\) \
743
749
` 0 ` if ` Left == Right ` \
744
750
` < 0 ` if ` Left < Right ` \
745
751
` > 0 ` if ` Left > Right `
746
752
747
- 2\)
753
+ 2\) \
748
754
Equivalent to: ` Left.month() <=> Right.month() `
749
755
750
- 3\)
756
+ 3\) \
751
757
Equivalent to:
752
758
753
759
``` cpp
@@ -788,8 +794,6 @@ int main()
788
794
}
789
795
```
790
796
791
- Output:
792
-
793
797
``` output
794
798
d1 < d2
795
799
true true false
@@ -1374,8 +1378,8 @@ int main()
1374
1378
month_day_last mdl{ January / last }; // Jan/last
1375
1379
month_weekday mw{ 11 / Monday[1] }; // Nov/Mon[1]
1376
1380
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]
1379
1383
year_month ym{ 2021y / April }; // 2021/Apr
1380
1384
year_month_day ymd{ January / 1d / 2021y }; // 2021-01-01
1381
1385
year_month_day ymd2{ 2021y / 5 / 7 }; // 2021-05-07
@@ -1385,12 +1389,13 @@ int main()
1385
1389
int yearValue{ 2021 / 4 / 4 }; // 126
1386
1390
1387
1391
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;
1391
1395
1392
1396
return 0;
1393
1397
}
1398
+ ```
1394
1399
1395
1400
``` output
1396
1401
Jul
@@ -1402,6 +1407,7 @@ Jan/Mon[last]
1402
1407
Mon
1403
1408
Mon[1]
1404
1409
2021/Apr
1410
+ 2021-01-01
1405
1411
2021-05-07
1406
1412
1975/Apr/last
1407
1413
1997/Jan/Wed[1]
0 commit comments