| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___CHRONO_MONTHDAY_H |
| 11 | #define _LIBCPP___CHRONO_MONTHDAY_H |
| 12 | |
| 13 | #include <__chrono/calendar.h> |
| 14 | #include <__chrono/day.h> |
| 15 | #include <__chrono/month.h> |
| 16 | #include <__compare/ordering.h> |
| 17 | #include <__config> |
| 18 | #include <__cstddef/size_t.h> |
| 19 | #include <__functional/hash.h> |
| 20 | |
| 21 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 22 | # pragma GCC system_header |
| 23 | #endif |
| 24 | |
| 25 | #if _LIBCPP_STD_VER >= 20 |
| 26 | |
| 27 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 28 | |
| 29 | namespace chrono { |
| 30 | |
| 31 | class month_day { |
| 32 | private: |
| 33 | chrono::month __m_; |
| 34 | chrono::day __d_; |
| 35 | |
| 36 | public: |
| 37 | month_day() = default; |
| 38 | constexpr month_day(const chrono::month& __mval, const chrono::day& __dval) noexcept : __m_{__mval}, __d_{__dval} {} |
| 39 | [[nodiscard]] inline constexpr chrono::month month() const noexcept { return __m_; } |
| 40 | [[nodiscard]] inline constexpr chrono::day day() const noexcept { return __d_; } |
| 41 | [[nodiscard]] constexpr bool ok() const noexcept; |
| 42 | }; |
| 43 | |
| 44 | inline constexpr bool month_day::ok() const noexcept { |
| 45 | if (!__m_.ok()) |
| 46 | return false; |
| 47 | const unsigned __dval = static_cast<unsigned>(__d_); |
| 48 | if (__dval < 1 || __dval > 31) |
| 49 | return false; |
| 50 | if (__dval <= 29) |
| 51 | return true; |
| 52 | // Now we've got either 30 or 31 |
| 53 | const unsigned __mval = static_cast<unsigned>(__m_); |
| 54 | if (__mval == 2) |
| 55 | return false; |
| 56 | if (__mval == 4 || __mval == 6 || __mval == 9 || __mval == 11) |
| 57 | return __dval == 30; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | inline constexpr bool operator==(const month_day& __lhs, const month_day& __rhs) noexcept { |
| 62 | return __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); |
| 63 | } |
| 64 | |
| 65 | inline constexpr strong_ordering operator<=>(const month_day& __lhs, const month_day& __rhs) noexcept { |
| 66 | if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0) |
| 67 | return __c; |
| 68 | return __lhs.day() <=> __rhs.day(); |
| 69 | } |
| 70 | |
| 71 | [[nodiscard]] inline constexpr month_day operator/(const month& __lhs, const day& __rhs) noexcept { |
| 72 | return month_day{__lhs, __rhs}; |
| 73 | } |
| 74 | |
| 75 | [[nodiscard]] inline constexpr month_day operator/(const day& __lhs, const month& __rhs) noexcept { |
| 76 | return __rhs / __lhs; |
| 77 | } |
| 78 | |
| 79 | [[nodiscard]] inline constexpr month_day operator/(const month& __lhs, int __rhs) noexcept { |
| 80 | return __lhs / day(__rhs); |
| 81 | } |
| 82 | |
| 83 | [[nodiscard]] inline constexpr month_day operator/(int __lhs, const day& __rhs) noexcept { |
| 84 | return month(__lhs) / __rhs; |
| 85 | } |
| 86 | |
| 87 | [[nodiscard]] inline constexpr month_day operator/(const day& __lhs, int __rhs) noexcept { |
| 88 | return month(__rhs) / __lhs; |
| 89 | } |
| 90 | |
| 91 | class month_day_last { |
| 92 | private: |
| 93 | chrono::month __m_; |
| 94 | |
| 95 | public: |
| 96 | explicit constexpr month_day_last(const chrono::month& __val) noexcept : __m_{__val} {} |
| 97 | [[nodiscard]] inline constexpr chrono::month month() const noexcept { return __m_; } |
| 98 | [[nodiscard]] inline constexpr bool ok() const noexcept { return __m_.ok(); } |
| 99 | }; |
| 100 | |
| 101 | inline constexpr bool operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept { |
| 102 | return __lhs.month() == __rhs.month(); |
| 103 | } |
| 104 | |
| 105 | inline constexpr strong_ordering operator<=>(const month_day_last& __lhs, const month_day_last& __rhs) noexcept { |
| 106 | return __lhs.month() <=> __rhs.month(); |
| 107 | } |
| 108 | |
| 109 | [[nodiscard]] inline constexpr month_day_last operator/(const month& __lhs, last_spec) noexcept { |
| 110 | return month_day_last{__lhs}; |
| 111 | } |
| 112 | |
| 113 | [[nodiscard]] inline constexpr month_day_last operator/(last_spec, const month& __rhs) noexcept { |
| 114 | return month_day_last{__rhs}; |
| 115 | } |
| 116 | |
| 117 | [[nodiscard]] inline constexpr month_day_last operator/(int __lhs, last_spec) noexcept { |
| 118 | return month_day_last{month(__lhs)}; |
| 119 | } |
| 120 | |
| 121 | [[nodiscard]] inline constexpr month_day_last operator/(last_spec, int __rhs) noexcept { |
| 122 | return month_day_last{month(__rhs)}; |
| 123 | } |
| 124 | |
| 125 | } // namespace chrono |
| 126 | |
| 127 | # if _LIBCPP_STD_VER >= 26 |
| 128 | |
| 129 | template <> |
| 130 | struct hash<chrono::month_day> { |
| 131 | [[nodiscard]] static size_t operator()(const chrono::month_day& __md) noexcept { |
| 132 | return std::__hash_combine(lhs: hash<chrono::month>{}(__md.month()), rhs: hash<chrono::day>{}(__md.day())); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | template <> |
| 137 | struct hash<chrono::month_day_last> { |
| 138 | [[nodiscard]] static size_t operator()(const chrono::month_day_last& __mdl) noexcept { |
| 139 | return hash<chrono::month>{}(__mdl.month()); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | # endif // _LIBCPP_STD_VER >= 26 |
| 144 | |
| 145 | _LIBCPP_END_NAMESPACE_STD |
| 146 | |
| 147 | #endif // _LIBCPP_STD_VER >= 20 |
| 148 | |
| 149 | #endif // _LIBCPP___CHRONO_MONTHDAY_H |
| 150 | |