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