| 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_YEAR_MONTH_DAY_H |
| 11 | #define _LIBCPP___CHRONO_YEAR_MONTH_DAY_H |
| 12 | |
| 13 | #include <__chrono/calendar.h> |
| 14 | #include <__chrono/day.h> |
| 15 | #include <__chrono/duration.h> |
| 16 | #include <__chrono/month.h> |
| 17 | #include <__chrono/monthday.h> |
| 18 | #include <__chrono/system_clock.h> |
| 19 | #include <__chrono/time_point.h> |
| 20 | #include <__chrono/year.h> |
| 21 | #include <__chrono/year_month.h> |
| 22 | #include <__compare/ordering.h> |
| 23 | #include <__config> |
| 24 | #include <__cstddef/size_t.h> |
| 25 | #include <__functional/hash.h> |
| 26 | #include <limits> |
| 27 | |
| 28 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 29 | # pragma GCC system_header |
| 30 | #endif |
| 31 | |
| 32 | #if _LIBCPP_STD_VER >= 20 |
| 33 | |
| 34 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 35 | |
| 36 | namespace chrono { |
| 37 | |
| 38 | class year_month_day_last; |
| 39 | |
| 40 | class year_month_day { |
| 41 | private: |
| 42 | chrono::year __y_; |
| 43 | chrono::month __m_; |
| 44 | chrono::day __d_; |
| 45 | |
| 46 | public: |
| 47 | year_month_day() = default; |
| 48 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day( |
| 49 | const chrono::year& __yval, const chrono::month& __mval, const chrono::day& __dval) noexcept |
| 50 | : __y_{__yval}, __m_{__mval}, __d_{__dval} {} |
| 51 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day(const year_month_day_last& __ymdl) noexcept; |
| 52 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(const sys_days& __sysd) noexcept |
| 53 | : year_month_day(__from_days(d: __sysd.time_since_epoch())) {} |
| 54 | _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_day(const local_days& __locd) noexcept |
| 55 | : year_month_day(__from_days(d: __locd.time_since_epoch())) {} |
| 56 | |
| 57 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const months& __dm) noexcept; |
| 58 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const months& __dm) noexcept; |
| 59 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const years& __dy) noexcept; |
| 60 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const years& __dy) noexcept; |
| 61 | |
| 62 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; } |
| 63 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; } |
| 64 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; } |
| 65 | _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; } |
| 66 | _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept { |
| 67 | return local_days{__to_days()}; |
| 68 | } |
| 69 | |
| 70 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept; |
| 71 | |
| 72 | _LIBCPP_HIDE_FROM_ABI static constexpr year_month_day __from_days(days __d) noexcept; |
| 73 | _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept; |
| 74 | }; |
| 75 | |
| 76 | // https://howardhinnant.github.io/date_algorithms.html#civil_from_days |
| 77 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day year_month_day::__from_days(days __d) noexcept { |
| 78 | static_assert(numeric_limits<unsigned>::digits >= 18, "" ); |
| 79 | static_assert(numeric_limits<int>::digits >= 20, "" ); |
| 80 | const int __z = __d.count() + 719468; |
| 81 | const int __era = (__z >= 0 ? __z : __z - 146096) / 146097; |
| 82 | const unsigned __doe = static_cast<unsigned>(__z - __era * 146097); // [0, 146096] |
| 83 | const unsigned __yoe = (__doe - __doe / 1460 + __doe / 36524 - __doe / 146096) / 365; // [0, 399] |
| 84 | const int __yr = static_cast<int>(__yoe) + __era * 400; |
| 85 | const unsigned __doy = __doe - (365 * __yoe + __yoe / 4 - __yoe / 100); // [0, 365] |
| 86 | const unsigned __mp = (5 * __doy + 2) / 153; // [0, 11] |
| 87 | const unsigned __dy = __doy - (153 * __mp + 2) / 5 + 1; // [1, 31] |
| 88 | const unsigned __mth = __mp + (__mp < 10 ? 3 : -9); // [1, 12] |
| 89 | return year_month_day{chrono::year{__yr + (__mth <= 2)}, chrono::month{__mth}, chrono::day{__dy}}; |
| 90 | } |
| 91 | |
| 92 | // https://howardhinnant.github.io/date_algorithms.html#days_from_civil |
| 93 | _LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_day::__to_days() const noexcept { |
| 94 | static_assert(numeric_limits<unsigned>::digits >= 18, "" ); |
| 95 | static_assert(numeric_limits<int>::digits >= 20, "" ); |
| 96 | |
| 97 | const int __yr = static_cast<int>(__y_) - (__m_ <= February); |
| 98 | const unsigned __mth = static_cast<unsigned>(__m_); |
| 99 | const unsigned __dy = static_cast<unsigned>(__d_); |
| 100 | |
| 101 | const int __era = (__yr >= 0 ? __yr : __yr - 399) / 400; |
| 102 | const unsigned __yoe = static_cast<unsigned>(__yr - __era * 400); // [0, 399] |
| 103 | const unsigned __doy = (153 * (__mth + (__mth > 2 ? -3 : 9)) + 2) / 5 + __dy - 1; // [0, 365] |
| 104 | const unsigned __doe = __yoe * 365 + __yoe / 4 - __yoe / 100 + __doy; // [0, 146096] |
| 105 | return days{__era * 146097 + static_cast<int>(__doe) - 719468}; |
| 106 | } |
| 107 | |
| 108 | _LIBCPP_HIDE_FROM_ABI inline constexpr bool |
| 109 | operator==(const year_month_day& __lhs, const year_month_day& __rhs) noexcept { |
| 110 | return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); |
| 111 | } |
| 112 | |
| 113 | _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering |
| 114 | operator<=>(const year_month_day& __lhs, const year_month_day& __rhs) noexcept { |
| 115 | if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0) |
| 116 | return __c; |
| 117 | if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0) |
| 118 | return __c; |
| 119 | return __lhs.day() <=> __rhs.day(); |
| 120 | } |
| 121 | |
| 122 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 123 | operator/(const year_month& __lhs, const day& __rhs) noexcept { |
| 124 | return year_month_day{__lhs.year(), __lhs.month(), __rhs}; |
| 125 | } |
| 126 | |
| 127 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 128 | operator/(const year_month& __lhs, int __rhs) noexcept { |
| 129 | return __lhs / day(__rhs); |
| 130 | } |
| 131 | |
| 132 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 133 | operator/(const year& __lhs, const month_day& __rhs) noexcept { |
| 134 | return __lhs / __rhs.month() / __rhs.day(); |
| 135 | } |
| 136 | |
| 137 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 138 | operator/(int __lhs, const month_day& __rhs) noexcept { |
| 139 | return year(__lhs) / __rhs; |
| 140 | } |
| 141 | |
| 142 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 143 | operator/(const month_day& __lhs, const year& __rhs) noexcept { |
| 144 | return __rhs / __lhs; |
| 145 | } |
| 146 | |
| 147 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 148 | operator/(const month_day& __lhs, int __rhs) noexcept { |
| 149 | return year(__rhs) / __lhs; |
| 150 | } |
| 151 | |
| 152 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 153 | operator+(const year_month_day& __lhs, const months& __rhs) noexcept { |
| 154 | return (__lhs.year() / __lhs.month() + __rhs) / __lhs.day(); |
| 155 | } |
| 156 | |
| 157 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 158 | operator+(const months& __lhs, const year_month_day& __rhs) noexcept { |
| 159 | return __rhs + __lhs; |
| 160 | } |
| 161 | |
| 162 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 163 | operator-(const year_month_day& __lhs, const months& __rhs) noexcept { |
| 164 | return __lhs + -__rhs; |
| 165 | } |
| 166 | |
| 167 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 168 | operator+(const year_month_day& __lhs, const years& __rhs) noexcept { |
| 169 | return (__lhs.year() + __rhs) / __lhs.month() / __lhs.day(); |
| 170 | } |
| 171 | |
| 172 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 173 | operator+(const years& __lhs, const year_month_day& __rhs) noexcept { |
| 174 | return __rhs + __lhs; |
| 175 | } |
| 176 | |
| 177 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
| 178 | operator-(const year_month_day& __lhs, const years& __rhs) noexcept { |
| 179 | return __lhs + -__rhs; |
| 180 | } |
| 181 | |
| 182 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const months& __dm) noexcept { |
| 183 | *this = *this + __dm; |
| 184 | return *this; |
| 185 | } |
| 186 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const months& __dm) noexcept { |
| 187 | *this = *this - __dm; |
| 188 | return *this; |
| 189 | } |
| 190 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const years& __dy) noexcept { |
| 191 | *this = *this + __dy; |
| 192 | return *this; |
| 193 | } |
| 194 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const years& __dy) noexcept { |
| 195 | *this = *this - __dy; |
| 196 | return *this; |
| 197 | } |
| 198 | |
| 199 | class year_month_day_last { |
| 200 | private: |
| 201 | chrono::year __y_; |
| 202 | chrono::month_day_last __mdl_; |
| 203 | |
| 204 | public: |
| 205 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last(const year& __yval, const month_day_last& __mdlval) noexcept |
| 206 | : __y_{__yval}, __mdl_{__mdlval} {} |
| 207 | |
| 208 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const months& __m) noexcept; |
| 209 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const months& __m) noexcept; |
| 210 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const years& __y) noexcept; |
| 211 | _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const years& __y) noexcept; |
| 212 | |
| 213 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; } |
| 214 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __mdl_.month(); } |
| 215 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month_day_last month_day_last() const noexcept { |
| 216 | return __mdl_; |
| 217 | } |
| 218 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr chrono::day day() const noexcept; |
| 219 | _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { |
| 220 | return sys_days{year() / month() / day()}; |
| 221 | } |
| 222 | _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept { |
| 223 | return local_days{year() / month() / day()}; |
| 224 | } |
| 225 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __mdl_.ok(); } |
| 226 | }; |
| 227 | |
| 228 | _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day year_month_day_last::day() const noexcept { |
| 229 | constexpr chrono::day __d[] = { |
| 230 | chrono::day(31), |
| 231 | chrono::day(28), |
| 232 | chrono::day(31), |
| 233 | chrono::day(30), |
| 234 | chrono::day(31), |
| 235 | chrono::day(30), |
| 236 | chrono::day(31), |
| 237 | chrono::day(31), |
| 238 | chrono::day(30), |
| 239 | chrono::day(31), |
| 240 | chrono::day(30), |
| 241 | chrono::day(31)}; |
| 242 | return (month() != February || !__y_.is_leap()) && month().ok() |
| 243 | ? __d[static_cast<unsigned>(month()) - 1] |
| 244 | : chrono::day{29}; |
| 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 | return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last(); |
| 250 | } |
| 251 | |
| 252 | _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering |
| 253 | operator<=>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { |
| 254 | if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0) |
| 255 | return __c; |
| 256 | return __lhs.month_day_last() <=> __rhs.month_day_last(); |
| 257 | } |
| 258 | |
| 259 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 260 | operator/(const year_month& __lhs, last_spec) noexcept { |
| 261 | return year_month_day_last{__lhs.year(), month_day_last{__lhs.month()}}; |
| 262 | } |
| 263 | |
| 264 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 265 | operator/(const year& __lhs, const month_day_last& __rhs) noexcept { |
| 266 | return year_month_day_last{__lhs, __rhs}; |
| 267 | } |
| 268 | |
| 269 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 270 | operator/(int __lhs, const month_day_last& __rhs) noexcept { |
| 271 | return year_month_day_last{year{__lhs}, __rhs}; |
| 272 | } |
| 273 | |
| 274 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 275 | operator/(const month_day_last& __lhs, const year& __rhs) noexcept { |
| 276 | return __rhs / __lhs; |
| 277 | } |
| 278 | |
| 279 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 280 | operator/(const month_day_last& __lhs, int __rhs) noexcept { |
| 281 | return year{__rhs} / __lhs; |
| 282 | } |
| 283 | |
| 284 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 285 | operator+(const year_month_day_last& __lhs, const months& __rhs) noexcept { |
| 286 | return (__lhs.year() / __lhs.month() + __rhs) / last; |
| 287 | } |
| 288 | |
| 289 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 290 | operator+(const months& __lhs, const year_month_day_last& __rhs) noexcept { |
| 291 | return __rhs + __lhs; |
| 292 | } |
| 293 | |
| 294 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 295 | operator-(const year_month_day_last& __lhs, const months& __rhs) noexcept { |
| 296 | return __lhs + (-__rhs); |
| 297 | } |
| 298 | |
| 299 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 300 | operator+(const year_month_day_last& __lhs, const years& __rhs) noexcept { |
| 301 | return year_month_day_last{__lhs.year() + __rhs, __lhs.month_day_last()}; |
| 302 | } |
| 303 | |
| 304 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 305 | operator+(const years& __lhs, const year_month_day_last& __rhs) noexcept { |
| 306 | return __rhs + __lhs; |
| 307 | } |
| 308 | |
| 309 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
| 310 | operator-(const year_month_day_last& __lhs, const years& __rhs) noexcept { |
| 311 | return __lhs + (-__rhs); |
| 312 | } |
| 313 | |
| 314 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
| 315 | year_month_day_last::operator+=(const months& __dm) noexcept { |
| 316 | *this = *this + __dm; |
| 317 | return *this; |
| 318 | } |
| 319 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
| 320 | year_month_day_last::operator-=(const months& __dm) noexcept { |
| 321 | *this = *this - __dm; |
| 322 | return *this; |
| 323 | } |
| 324 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
| 325 | year_month_day_last::operator+=(const years& __dy) noexcept { |
| 326 | *this = *this + __dy; |
| 327 | return *this; |
| 328 | } |
| 329 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
| 330 | year_month_day_last::operator-=(const years& __dy) noexcept { |
| 331 | *this = *this - __dy; |
| 332 | return *this; |
| 333 | } |
| 334 | |
| 335 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept |
| 336 | : __y_{__ymdl.year()}, __m_{__ymdl.month()}, __d_{__ymdl.day()} {} |
| 337 | |
| 338 | _LIBCPP_HIDE_FROM_ABI inline constexpr bool year_month_day::ok() const noexcept { |
| 339 | if (!__y_.ok() || !__m_.ok()) |
| 340 | return false; |
| 341 | return chrono::day{1} <= __d_ && __d_ <= (__y_ / __m_ / last).day(); |
| 342 | } |
| 343 | |
| 344 | } // namespace chrono |
| 345 | |
| 346 | # if _LIBCPP_STD_VER >= 26 |
| 347 | |
| 348 | template <> |
| 349 | struct hash<chrono::year_month_day> { |
| 350 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::year_month_day& __ymd) noexcept { |
| 351 | return std::__hash_combine( |
| 352 | hash<chrono::year>{}(__ymd.year()), |
| 353 | std::__hash_combine(hash<chrono::month>{}(__ymd.month()), hash<chrono::day>{}(__ymd.day()))); |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | template <> |
| 358 | struct hash<chrono::year_month_day_last> { |
| 359 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::year_month_day_last& __ymdl) noexcept { |
| 360 | return std::__hash_combine( |
| 361 | hash<chrono::year>{}(__ymdl.year()), hash<chrono::month_day_last>{}(__ymdl.month_day_last())); |
| 362 | } |
| 363 | }; |
| 364 | |
| 365 | # endif // _LIBCPP_STD_VER >= 26 |
| 366 | |
| 367 | _LIBCPP_END_NAMESPACE_STD |
| 368 | |
| 369 | #endif // _LIBCPP_STD_VER >= 20 |
| 370 | |
| 371 | #endif // _LIBCPP___CHRONO_YEAR_MONTH_DAY_H |
| 372 | |