Skip to content

Commit a05724a

Browse files
authored
[libc++][chrono] Adds year_month_day_last&::operator<=>. (#98169)
41f7bb9 claimed it implemented this change but the code was not adjusted. The other spaceship operators in the calendar code have been validated too. Implements parts of - P1614R2 The Mothership has Landed
1 parent afbdd6f commit a05724a

File tree

3 files changed

+65
-95
lines changed

3 files changed

+65
-95
lines changed

libcxx/include/__chrono/year_month_day.h

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -239,33 +239,11 @@ operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) n
239239
return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last();
240240
}
241241

242-
_LIBCPP_HIDE_FROM_ABI inline constexpr bool
243-
operator!=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
244-
return !(__lhs == __rhs);
245-
}
246-
247-
_LIBCPP_HIDE_FROM_ABI inline constexpr bool
248-
operator<(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
249-
if (__lhs.year() < __rhs.year())
250-
return true;
251-
if (__lhs.year() > __rhs.year())
252-
return false;
253-
return __lhs.month_day_last() < __rhs.month_day_last();
254-
}
255-
256-
_LIBCPP_HIDE_FROM_ABI inline constexpr bool
257-
operator>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
258-
return __rhs < __lhs;
259-
}
260-
261-
_LIBCPP_HIDE_FROM_ABI inline constexpr bool
262-
operator<=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
263-
return !(__rhs < __lhs);
264-
}
265-
266-
_LIBCPP_HIDE_FROM_ABI inline constexpr bool
267-
operator>=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
268-
return !(__lhs < __rhs);
242+
_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
243+
operator<=>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
244+
if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
245+
return __c;
246+
return __lhs.month_day_last() <=> __rhs.month_day_last();
269247
}
270248

271249
_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept {

libcxx/modules/std/chrono.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export namespace std {
3737

3838
// [time.duration.comparisons], duration comparisons
3939
using std::chrono::operator==;
40-
using std::chrono::operator!=;
4140
using std::chrono::operator<;
4241
using std::chrono::operator>;
4342
using std::chrono::operator<=;

libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
// UNSUPPORTED: c++03, c++11, c++14, c++17
8+
9+
// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
910

1011
// <chrono>
1112
// class year_month_day_last;
1213

1314
// constexpr bool operator==(const year_month_day_last& x, const year_month_day_last& y) noexcept;
14-
// Returns: x.year() == y.year() && x.month_day_last() == y.month_day_last().
15-
//
16-
// constexpr bool operator< (const year_month_day_last& x, const year_month_day_last& y) noexcept;
17-
// Returns:
18-
// If x.year() < y.year(), returns true.
19-
// Otherwise, if x.year() > y.year(), returns false.
20-
// Otherwise, returns x.month_day_last() < y.month_day_last()
15+
// constexpr bool operator<=>(const year_month_day_last& x, const year_month_day_last& y) noexcept;
2116

2217
#include <chrono>
2318
#include <type_traits>
@@ -26,63 +21,61 @@
2621
#include "test_macros.h"
2722
#include "test_comparisons.h"
2823

29-
int main(int, char**)
30-
{
31-
using year = std::chrono::year;
32-
using month = std::chrono::month;
33-
using month_day_last = std::chrono::month_day_last;
34-
using year_month_day_last = std::chrono::year_month_day_last;
35-
36-
AssertComparisonsAreNoexcept<year_month_day_last>();
37-
AssertComparisonsReturnBool<year_month_day_last>();
38-
39-
constexpr month January = std::chrono::January;
40-
constexpr month February = std::chrono::February;
41-
42-
static_assert( testComparisons(
43-
year_month_day_last{year{1234}, month_day_last{January}},
44-
year_month_day_last{year{1234}, month_day_last{January}},
45-
true, false), "");
46-
47-
// different month
48-
static_assert( testComparisons(
49-
year_month_day_last{year{1234}, month_day_last{January}},
50-
year_month_day_last{year{1234}, month_day_last{February}},
51-
false, true), "");
52-
53-
// different year
54-
static_assert( testComparisons(
55-
year_month_day_last{year{1234}, month_day_last{January}},
56-
year_month_day_last{year{1235}, month_day_last{January}},
57-
false, true), "");
58-
59-
// different month
60-
static_assert( testComparisons(
61-
year_month_day_last{year{1234}, month_day_last{January}},
62-
year_month_day_last{year{1234}, month_day_last{February}},
63-
false, true), "");
64-
65-
// different year and month
66-
static_assert( testComparisons(
67-
year_month_day_last{year{1234}, month_day_last{February}},
68-
year_month_day_last{year{1235}, month_day_last{January}},
69-
false, true), "");
70-
71-
// same year, different months
72-
for (unsigned i = 1; i < 12; ++i)
73-
for (unsigned j = 1; j < 12; ++j)
74-
assert((testComparisons(
75-
year_month_day_last{year{1234}, month_day_last{month{i}}},
76-
year_month_day_last{year{1234}, month_day_last{month{j}}},
77-
i == j, i < j )));
78-
79-
// same month, different years
80-
for (int i = 1000; i < 2000; ++i)
81-
for (int j = 1000; j < 2000; ++j)
82-
assert((testComparisons(
83-
year_month_day_last{year{i}, month_day_last{January}},
84-
year_month_day_last{year{j}, month_day_last{January}},
85-
i == j, i < j )));
86-
87-
return 0;
24+
constexpr bool test() {
25+
using year = std::chrono::year;
26+
using month = std::chrono::month;
27+
using month_day_last = std::chrono::month_day_last;
28+
using year_month_day_last = std::chrono::year_month_day_last;
29+
30+
constexpr month January = std::chrono::January;
31+
constexpr month February = std::chrono::February;
32+
33+
assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}},
34+
year_month_day_last{year{1234}, month_day_last{January}},
35+
std::strong_ordering::equal));
36+
37+
// different month
38+
assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}},
39+
year_month_day_last{year{1234}, month_day_last{February}},
40+
std::strong_ordering::less));
41+
42+
// different year
43+
assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}},
44+
year_month_day_last{year{1235}, month_day_last{January}},
45+
std::strong_ordering::less));
46+
47+
// different year and month
48+
assert(testOrder(year_month_day_last{year{1234}, month_day_last{February}},
49+
year_month_day_last{year{1235}, month_day_last{January}},
50+
std::strong_ordering::less));
51+
52+
// same year, different months
53+
for (unsigned i = 1; i < 12; ++i)
54+
for (unsigned j = 1; j < 12; ++j)
55+
assert((testOrder(year_month_day_last{year{1234}, month_day_last{month{i}}},
56+
year_month_day_last{year{1234}, month_day_last{month{j}}},
57+
i == j ? std::strong_ordering::equal
58+
: i < j ? std::strong_ordering::less
59+
: std::strong_ordering::greater)));
60+
61+
// same month, different years
62+
for (int i = 1000; i < 20; ++i)
63+
for (int j = 1000; j < 20; ++j)
64+
assert((testOrder(year_month_day_last{year{i}, month_day_last{January}},
65+
year_month_day_last{year{j}, month_day_last{January}},
66+
i == j ? std::strong_ordering::equal
67+
: i < j ? std::strong_ordering::less
68+
: std::strong_ordering::greater)));
69+
return true;
70+
}
71+
72+
int main(int, char**) {
73+
using year_month_day_last = std::chrono::year_month_day_last;
74+
AssertOrderAreNoexcept<year_month_day_last>();
75+
AssertOrderReturn<std::strong_ordering, year_month_day_last>();
76+
77+
test();
78+
static_assert(test());
79+
80+
return 0;
8881
}

0 commit comments

Comments
 (0)