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 <__config> 23 #include <compare> 24 #include <limits> 25 26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27 # pragma GCC system_header 28 #endif 29 30 #if _LIBCPP_STD_VER >= 20 31 32 _LIBCPP_BEGIN_NAMESPACE_STD 33 34 namespace chrono 35 { 36 37 class year_month_day_last; 38 39 class year_month_day { 40 private: 41 chrono::year __y_; 42 chrono::month __m_; 43 chrono::day __d_; 44 public: 45 year_month_day() = default; 46 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day( 47 const chrono::year& __yval, const chrono::month& __mval, const chrono::day& __dval) noexcept 48 : __y_{__yval}, __m_{__mval}, __d_{__dval} {} 49 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day(const year_month_day_last& __ymdl) noexcept; 50 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(const sys_days& __sysd) noexcept 51 : year_month_day(__from_days(__sysd.time_since_epoch())) {} 52 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_day(const local_days& __locd) noexcept 53 : year_month_day(__from_days(__locd.time_since_epoch())) {} 54 55 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const months& __dm) noexcept; 56 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const months& __dm) noexcept; 57 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const years& __dy) noexcept; 58 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const years& __dy) noexcept; 59 60 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; } 61 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; } 62 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; } 63 _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; } 64 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept { return local_days{__to_days()}; } 65 66 _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept; 67 68 _LIBCPP_HIDE_FROM_ABI static constexpr year_month_day __from_days(days __d) noexcept; 69 _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept; 70 }; 71 72 73 // https://howardhinnant.github.io/date_algorithms.html#civil_from_days 74 _LIBCPP_HIDE_FROM_ABI inline constexpr 75 year_month_day year_month_day::__from_days(days __d) noexcept 76 { 77 static_assert(numeric_limits<unsigned>::digits >= 18, ""); 78 static_assert(numeric_limits<int>::digits >= 20 , ""); 79 const int __z = __d.count() + 719468; 80 const int __era = (__z >= 0 ? __z : __z - 146096) / 146097; 81 const unsigned __doe = static_cast<unsigned>(__z - __era * 146097); // [0, 146096] 82 const unsigned __yoe = (__doe - __doe/1460 + __doe/36524 - __doe/146096) / 365; // [0, 399] 83 const int __yr = static_cast<int>(__yoe) + __era * 400; 84 const unsigned __doy = __doe - (365 * __yoe + __yoe/4 - __yoe/100); // [0, 365] 85 const unsigned __mp = (5 * __doy + 2)/153; // [0, 11] 86 const unsigned __dy = __doy - (153 * __mp + 2)/5 + 1; // [1, 31] 87 const unsigned __mth = __mp + (__mp < 10 ? 3 : -9); // [1, 12] 88 return year_month_day{chrono::year{__yr + (__mth <= 2)}, chrono::month{__mth}, chrono::day{__dy}}; 89 } 90 91 // https://howardhinnant.github.io/date_algorithms.html#days_from_civil 92 _LIBCPP_HIDE_FROM_ABI inline constexpr 93 days year_month_day::__to_days() const noexcept 94 { 95 static_assert(numeric_limits<unsigned>::digits >= 18, ""); 96 static_assert(numeric_limits<int>::digits >= 20 , ""); 97 98 const int __yr = static_cast<int>(__y_) - (__m_ <= February); 99 const unsigned __mth = static_cast<unsigned>(__m_); 100 const unsigned __dy = static_cast<unsigned>(__d_); 101 102 const int __era = (__yr >= 0 ? __yr : __yr - 399) / 400; 103 const unsigned __yoe = static_cast<unsigned>(__yr - __era * 400); // [0, 399] 104 const unsigned __doy = (153 * (__mth + (__mth > 2 ? -3 : 9)) + 2) / 5 + __dy-1; // [0, 365] 105 const unsigned __doe = __yoe * 365 + __yoe/4 - __yoe/100 + __doy; // [0, 146096] 106 return days{__era * 146097 + static_cast<int>(__doe) - 719468}; 107 } 108 109 _LIBCPP_HIDE_FROM_ABI inline constexpr 110 bool operator==(const year_month_day& __lhs, const year_month_day& __rhs) noexcept 111 { return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); } 112 113 _LIBCPP_HIDE_FROM_ABI 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 _LIBCPP_HIDE_FROM_ABI inline constexpr 123 year_month_day operator/(const year_month& __lhs, const day& __rhs) noexcept 124 { return year_month_day{__lhs.year(), __lhs.month(), __rhs}; } 125 126 _LIBCPP_HIDE_FROM_ABI inline constexpr 127 year_month_day operator/(const year_month& __lhs, int __rhs) noexcept 128 { return __lhs / day(__rhs); } 129 130 _LIBCPP_HIDE_FROM_ABI inline constexpr 131 year_month_day operator/(const year& __lhs, const month_day& __rhs) noexcept 132 { return __lhs / __rhs.month() / __rhs.day(); } 133 134 _LIBCPP_HIDE_FROM_ABI inline constexpr 135 year_month_day operator/(int __lhs, const month_day& __rhs) noexcept 136 { return year(__lhs) / __rhs; } 137 138 _LIBCPP_HIDE_FROM_ABI inline constexpr 139 year_month_day operator/(const month_day& __lhs, const year& __rhs) noexcept 140 { return __rhs / __lhs; } 141 142 _LIBCPP_HIDE_FROM_ABI inline constexpr 143 year_month_day operator/(const month_day& __lhs, int __rhs) noexcept 144 { return year(__rhs) / __lhs; } 145 146 147 _LIBCPP_HIDE_FROM_ABI inline constexpr 148 year_month_day operator+(const year_month_day& __lhs, const months& __rhs) noexcept 149 { return (__lhs.year()/__lhs.month() + __rhs)/__lhs.day(); } 150 151 _LIBCPP_HIDE_FROM_ABI inline constexpr 152 year_month_day operator+(const months& __lhs, const year_month_day& __rhs) noexcept 153 { return __rhs + __lhs; } 154 155 _LIBCPP_HIDE_FROM_ABI inline constexpr 156 year_month_day operator-(const year_month_day& __lhs, const months& __rhs) noexcept 157 { return __lhs + -__rhs; } 158 159 _LIBCPP_HIDE_FROM_ABI inline constexpr 160 year_month_day operator+(const year_month_day& __lhs, const years& __rhs) noexcept 161 { return (__lhs.year() + __rhs) / __lhs.month() / __lhs.day(); } 162 163 _LIBCPP_HIDE_FROM_ABI inline constexpr 164 year_month_day operator+(const years& __lhs, const year_month_day& __rhs) noexcept 165 { return __rhs + __lhs; } 166 167 _LIBCPP_HIDE_FROM_ABI inline constexpr 168 year_month_day operator-(const year_month_day& __lhs, const years& __rhs) noexcept 169 { return __lhs + -__rhs; } 170 171 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const months& __dm) noexcept { *this = *this + __dm; return *this; } 172 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const months& __dm) noexcept { *this = *this - __dm; return *this; } 173 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const years& __dy) noexcept { *this = *this + __dy; return *this; } 174 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const years& __dy) noexcept { *this = *this - __dy; return *this; } 175 176 class year_month_day_last { 177 private: 178 chrono::year __y_; 179 chrono::month_day_last __mdl_; 180 public: 181 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last(const year& __yval, const month_day_last& __mdlval) noexcept 182 : __y_{__yval}, __mdl_{__mdlval} {} 183 184 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const months& __m) noexcept; 185 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const months& __m) noexcept; 186 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const years& __y) noexcept; 187 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const years& __y) noexcept; 188 189 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; } 190 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __mdl_.month(); } 191 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month_day_last month_day_last() const noexcept { return __mdl_; } 192 _LIBCPP_HIDE_FROM_ABI constexpr chrono::day day() const noexcept; 193 _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{year()/month()/day()}; } 194 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept { return local_days{year()/month()/day()}; } 195 _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __mdl_.ok(); } 196 }; 197 198 _LIBCPP_HIDE_FROM_ABI inline constexpr 199 chrono::day year_month_day_last::day() const noexcept 200 { 201 constexpr chrono::day __d[] = 202 { 203 chrono::day(31), chrono::day(28), chrono::day(31), 204 chrono::day(30), chrono::day(31), chrono::day(30), 205 chrono::day(31), chrono::day(31), chrono::day(30), 206 chrono::day(31), chrono::day(30), chrono::day(31) 207 }; 208 return (month() != February || !__y_.is_leap()) && month().ok() ? 209 __d[static_cast<unsigned>(month()) - 1] : chrono::day{29}; 210 } 211 212 _LIBCPP_HIDE_FROM_ABI inline constexpr 213 bool operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept 214 { return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last(); } 215 216 _LIBCPP_HIDE_FROM_ABI inline constexpr 217 bool operator!=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept 218 { return !(__lhs == __rhs); } 219 220 _LIBCPP_HIDE_FROM_ABI inline constexpr 221 bool operator< (const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept 222 { 223 if (__lhs.year() < __rhs.year()) return true; 224 if (__lhs.year() > __rhs.year()) return false; 225 return __lhs.month_day_last() < __rhs.month_day_last(); 226 } 227 228 _LIBCPP_HIDE_FROM_ABI inline constexpr 229 bool operator> (const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept 230 { return __rhs < __lhs; } 231 232 _LIBCPP_HIDE_FROM_ABI inline constexpr 233 bool operator<=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept 234 { return !(__rhs < __lhs);} 235 236 _LIBCPP_HIDE_FROM_ABI inline constexpr 237 bool operator>=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept 238 { return !(__lhs < __rhs); } 239 240 _LIBCPP_HIDE_FROM_ABI inline constexpr 241 year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept 242 { return year_month_day_last{__lhs.year(), month_day_last{__lhs.month()}}; } 243 244 _LIBCPP_HIDE_FROM_ABI inline constexpr 245 year_month_day_last operator/(const year& __lhs, const month_day_last& __rhs) noexcept 246 { return year_month_day_last{__lhs, __rhs}; } 247 248 _LIBCPP_HIDE_FROM_ABI inline constexpr 249 year_month_day_last operator/(int __lhs, const month_day_last& __rhs) noexcept 250 { return year_month_day_last{year{__lhs}, __rhs}; } 251 252 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last 253 operator/(const month_day_last& __lhs, const year& __rhs) noexcept 254 { return __rhs / __lhs; } 255 256 _LIBCPP_HIDE_FROM_ABI inline constexpr 257 year_month_day_last operator/(const month_day_last& __lhs, int __rhs) noexcept 258 { return year{__rhs} / __lhs; } 259 260 261 _LIBCPP_HIDE_FROM_ABI inline constexpr 262 year_month_day_last operator+(const year_month_day_last& __lhs, const months& __rhs) noexcept 263 { return (__lhs.year() / __lhs.month() + __rhs) / last; } 264 265 _LIBCPP_HIDE_FROM_ABI inline constexpr 266 year_month_day_last operator+(const months& __lhs, const year_month_day_last& __rhs) noexcept 267 { return __rhs + __lhs; } 268 269 _LIBCPP_HIDE_FROM_ABI inline constexpr 270 year_month_day_last operator-(const year_month_day_last& __lhs, const months& __rhs) noexcept 271 { return __lhs + (-__rhs); } 272 273 _LIBCPP_HIDE_FROM_ABI inline constexpr 274 year_month_day_last operator+(const year_month_day_last& __lhs, const years& __rhs) noexcept 275 { return year_month_day_last{__lhs.year() + __rhs, __lhs.month_day_last()}; } 276 277 _LIBCPP_HIDE_FROM_ABI inline constexpr 278 year_month_day_last operator+(const years& __lhs, const year_month_day_last& __rhs) noexcept 279 { return __rhs + __lhs; } 280 281 _LIBCPP_HIDE_FROM_ABI inline constexpr 282 year_month_day_last operator-(const year_month_day_last& __lhs, const years& __rhs) noexcept 283 { return __lhs + (-__rhs); } 284 285 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator+=(const months& __dm) noexcept { *this = *this + __dm; return *this; } 286 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator-=(const months& __dm) noexcept { *this = *this - __dm; return *this; } 287 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator+=(const years& __dy) noexcept { *this = *this + __dy; return *this; } 288 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator-=(const years& __dy) noexcept { *this = *this - __dy; return *this; } 289 290 _LIBCPP_HIDE_FROM_ABI inline constexpr 291 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept 292 : __y_{__ymdl.year()}, __m_{__ymdl.month()}, __d_{__ymdl.day()} {} 293 294 _LIBCPP_HIDE_FROM_ABI inline constexpr 295 bool year_month_day::ok() const noexcept 296 { 297 if (!__y_.ok() || !__m_.ok()) return false; 298 return chrono::day{1} <= __d_ && __d_ <= (__y_ / __m_ / last).day(); 299 } 300 301 } // namespace chrono 302 303 _LIBCPP_END_NAMESPACE_STD 304 305 #endif // _LIBCPP_STD_VER >= 20 306 307 #endif // _LIBCPP___CHRONO_YEAR_MONTH_DAY_H 308