Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:25

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___CHRONO_YEAR_MONTH_DAY_H
0011 #define _LIBCPP___CXX03___CHRONO_YEAR_MONTH_DAY_H
0012 
0013 #include <__cxx03/__chrono/calendar.h>
0014 #include <__cxx03/__chrono/day.h>
0015 #include <__cxx03/__chrono/duration.h>
0016 #include <__cxx03/__chrono/month.h>
0017 #include <__cxx03/__chrono/monthday.h>
0018 #include <__cxx03/__chrono/system_clock.h>
0019 #include <__cxx03/__chrono/time_point.h>
0020 #include <__cxx03/__chrono/year.h>
0021 #include <__cxx03/__chrono/year_month.h>
0022 #include <__cxx03/__config>
0023 #include <__cxx03/compare>
0024 #include <__cxx03/limits>
0025 
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 #  pragma GCC system_header
0028 #endif
0029 
0030 #if _LIBCPP_STD_VER >= 20
0031 
0032 _LIBCPP_BEGIN_NAMESPACE_STD
0033 
0034 namespace chrono {
0035 
0036 class year_month_day_last;
0037 
0038 class year_month_day {
0039 private:
0040   chrono::year __y_;
0041   chrono::month __m_;
0042   chrono::day __d_;
0043 
0044 public:
0045   year_month_day() = default;
0046   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(
0047       const chrono::year& __yval, const chrono::month& __mval, const chrono::day& __dval) noexcept
0048       : __y_{__yval}, __m_{__mval}, __d_{__dval} {}
0049   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day(const year_month_day_last& __ymdl) noexcept;
0050   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(const sys_days& __sysd) noexcept
0051       : year_month_day(__from_days(__sysd.time_since_epoch())) {}
0052   _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_day(const local_days& __locd) noexcept
0053       : year_month_day(__from_days(__locd.time_since_epoch())) {}
0054 
0055   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const months& __dm) noexcept;
0056   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const months& __dm) noexcept;
0057   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const years& __dy) noexcept;
0058   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const years& __dy) noexcept;
0059 
0060   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
0061   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
0062   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
0063   _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }
0064   _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
0065     return local_days{__to_days()};
0066   }
0067 
0068   _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
0069 
0070   _LIBCPP_HIDE_FROM_ABI static constexpr year_month_day __from_days(days __d) noexcept;
0071   _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
0072 };
0073 
0074 // https://howardhinnant.github.io/date_algorithms.html#civil_from_days
0075 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day year_month_day::__from_days(days __d) noexcept {
0076   static_assert(numeric_limits<unsigned>::digits >= 18, "");
0077   static_assert(numeric_limits<int>::digits >= 20, "");
0078   const int __z        = __d.count() + 719468;
0079   const int __era      = (__z >= 0 ? __z : __z - 146096) / 146097;
0080   const unsigned __doe = static_cast<unsigned>(__z - __era * 146097);                   // [0, 146096]
0081   const unsigned __yoe = (__doe - __doe / 1460 + __doe / 36524 - __doe / 146096) / 365; // [0, 399]
0082   const int __yr       = static_cast<int>(__yoe) + __era * 400;
0083   const unsigned __doy = __doe - (365 * __yoe + __yoe / 4 - __yoe / 100); // [0, 365]
0084   const unsigned __mp  = (5 * __doy + 2) / 153;                           // [0, 11]
0085   const unsigned __dy  = __doy - (153 * __mp + 2) / 5 + 1;                // [1, 31]
0086   const unsigned __mth = __mp + (__mp < 10 ? 3 : -9);                     // [1, 12]
0087   return year_month_day{chrono::year{__yr + (__mth <= 2)}, chrono::month{__mth}, chrono::day{__dy}};
0088 }
0089 
0090 // https://howardhinnant.github.io/date_algorithms.html#days_from_civil
0091 _LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_day::__to_days() const noexcept {
0092   static_assert(numeric_limits<unsigned>::digits >= 18, "");
0093   static_assert(numeric_limits<int>::digits >= 20, "");
0094 
0095   const int __yr       = static_cast<int>(__y_) - (__m_ <= February);
0096   const unsigned __mth = static_cast<unsigned>(__m_);
0097   const unsigned __dy  = static_cast<unsigned>(__d_);
0098 
0099   const int __era      = (__yr >= 0 ? __yr : __yr - 399) / 400;
0100   const unsigned __yoe = static_cast<unsigned>(__yr - __era * 400);                 // [0, 399]
0101   const unsigned __doy = (153 * (__mth + (__mth > 2 ? -3 : 9)) + 2) / 5 + __dy - 1; // [0, 365]
0102   const unsigned __doe = __yoe * 365 + __yoe / 4 - __yoe / 100 + __doy;             // [0, 146096]
0103   return days{__era * 146097 + static_cast<int>(__doe) - 719468};
0104 }
0105 
0106 _LIBCPP_HIDE_FROM_ABI inline constexpr bool
0107 operator==(const year_month_day& __lhs, const year_month_day& __rhs) noexcept {
0108   return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day();
0109 }
0110 
0111 _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
0112 operator<=>(const year_month_day& __lhs, const year_month_day& __rhs) noexcept {
0113   if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
0114     return __c;
0115   if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
0116     return __c;
0117   return __lhs.day() <=> __rhs.day();
0118 }
0119 
0120 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, const day& __rhs) noexcept {
0121   return year_month_day{__lhs.year(), __lhs.month(), __rhs};
0122 }
0123 
0124 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, int __rhs) noexcept {
0125   return __lhs / day(__rhs);
0126 }
0127 
0128 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year& __lhs, const month_day& __rhs) noexcept {
0129   return __lhs / __rhs.month() / __rhs.day();
0130 }
0131 
0132 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(int __lhs, const month_day& __rhs) noexcept {
0133   return year(__lhs) / __rhs;
0134 }
0135 
0136 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, const year& __rhs) noexcept {
0137   return __rhs / __lhs;
0138 }
0139 
0140 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, int __rhs) noexcept {
0141   return year(__rhs) / __lhs;
0142 }
0143 
0144 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
0145 operator+(const year_month_day& __lhs, const months& __rhs) noexcept {
0146   return (__lhs.year() / __lhs.month() + __rhs) / __lhs.day();
0147 }
0148 
0149 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
0150 operator+(const months& __lhs, const year_month_day& __rhs) noexcept {
0151   return __rhs + __lhs;
0152 }
0153 
0154 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
0155 operator-(const year_month_day& __lhs, const months& __rhs) noexcept {
0156   return __lhs + -__rhs;
0157 }
0158 
0159 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
0160 operator+(const year_month_day& __lhs, const years& __rhs) noexcept {
0161   return (__lhs.year() + __rhs) / __lhs.month() / __lhs.day();
0162 }
0163 
0164 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
0165 operator+(const years& __lhs, const year_month_day& __rhs) noexcept {
0166   return __rhs + __lhs;
0167 }
0168 
0169 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
0170 operator-(const year_month_day& __lhs, const years& __rhs) noexcept {
0171   return __lhs + -__rhs;
0172 }
0173 
0174 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const months& __dm) noexcept {
0175   *this = *this + __dm;
0176   return *this;
0177 }
0178 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const months& __dm) noexcept {
0179   *this = *this - __dm;
0180   return *this;
0181 }
0182 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const years& __dy) noexcept {
0183   *this = *this + __dy;
0184   return *this;
0185 }
0186 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const years& __dy) noexcept {
0187   *this = *this - __dy;
0188   return *this;
0189 }
0190 
0191 class year_month_day_last {
0192 private:
0193   chrono::year __y_;
0194   chrono::month_day_last __mdl_;
0195 
0196 public:
0197   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last(const year& __yval, const month_day_last& __mdlval) noexcept
0198       : __y_{__yval}, __mdl_{__mdlval} {}
0199 
0200   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const months& __m) noexcept;
0201   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const months& __m) noexcept;
0202   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const years& __y) noexcept;
0203   _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const years& __y) noexcept;
0204 
0205   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
0206   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __mdl_.month(); }
0207   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month_day_last month_day_last() const noexcept { return __mdl_; }
0208   _LIBCPP_HIDE_FROM_ABI constexpr chrono::day day() const noexcept;
0209   _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept {
0210     return sys_days{year() / month() / day()};
0211   }
0212   _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
0213     return local_days{year() / month() / day()};
0214   }
0215   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __mdl_.ok(); }
0216 };
0217 
0218 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day year_month_day_last::day() const noexcept {
0219   constexpr chrono::day __d[] = {
0220       chrono::day(31),
0221       chrono::day(28),
0222       chrono::day(31),
0223       chrono::day(30),
0224       chrono::day(31),
0225       chrono::day(30),
0226       chrono::day(31),
0227       chrono::day(31),
0228       chrono::day(30),
0229       chrono::day(31),
0230       chrono::day(30),
0231       chrono::day(31)};
0232   return (month() != February || !__y_.is_leap()) && month().ok()
0233            ? __d[static_cast<unsigned>(month()) - 1]
0234            : chrono::day{29};
0235 }
0236 
0237 _LIBCPP_HIDE_FROM_ABI inline constexpr bool
0238 operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
0239   return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last();
0240 }
0241 
0242 _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
0243 operator<=>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
0244   if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
0245     return __c;
0246   return __lhs.month_day_last() <=> __rhs.month_day_last();
0247 }
0248 
0249 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept {
0250   return year_month_day_last{__lhs.year(), month_day_last{__lhs.month()}};
0251 }
0252 
0253 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0254 operator/(const year& __lhs, const month_day_last& __rhs) noexcept {
0255   return year_month_day_last{__lhs, __rhs};
0256 }
0257 
0258 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(int __lhs, const month_day_last& __rhs) noexcept {
0259   return year_month_day_last{year{__lhs}, __rhs};
0260 }
0261 
0262 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0263 operator/(const month_day_last& __lhs, const year& __rhs) noexcept {
0264   return __rhs / __lhs;
0265 }
0266 
0267 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const month_day_last& __lhs, int __rhs) noexcept {
0268   return year{__rhs} / __lhs;
0269 }
0270 
0271 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0272 operator+(const year_month_day_last& __lhs, const months& __rhs) noexcept {
0273   return (__lhs.year() / __lhs.month() + __rhs) / last;
0274 }
0275 
0276 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0277 operator+(const months& __lhs, const year_month_day_last& __rhs) noexcept {
0278   return __rhs + __lhs;
0279 }
0280 
0281 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0282 operator-(const year_month_day_last& __lhs, const months& __rhs) noexcept {
0283   return __lhs + (-__rhs);
0284 }
0285 
0286 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0287 operator+(const year_month_day_last& __lhs, const years& __rhs) noexcept {
0288   return year_month_day_last{__lhs.year() + __rhs, __lhs.month_day_last()};
0289 }
0290 
0291 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0292 operator+(const years& __lhs, const year_month_day_last& __rhs) noexcept {
0293   return __rhs + __lhs;
0294 }
0295 
0296 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
0297 operator-(const year_month_day_last& __lhs, const years& __rhs) noexcept {
0298   return __lhs + (-__rhs);
0299 }
0300 
0301 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
0302 year_month_day_last::operator+=(const months& __dm) noexcept {
0303   *this = *this + __dm;
0304   return *this;
0305 }
0306 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
0307 year_month_day_last::operator-=(const months& __dm) noexcept {
0308   *this = *this - __dm;
0309   return *this;
0310 }
0311 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
0312 year_month_day_last::operator+=(const years& __dy) noexcept {
0313   *this = *this + __dy;
0314   return *this;
0315 }
0316 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
0317 year_month_day_last::operator-=(const years& __dy) noexcept {
0318   *this = *this - __dy;
0319   return *this;
0320 }
0321 
0322 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
0323     : __y_{__ymdl.year()}, __m_{__ymdl.month()}, __d_{__ymdl.day()} {}
0324 
0325 _LIBCPP_HIDE_FROM_ABI inline constexpr bool year_month_day::ok() const noexcept {
0326   if (!__y_.ok() || !__m_.ok())
0327     return false;
0328   return chrono::day{1} <= __d_ && __d_ <= (__y_ / __m_ / last).day();
0329 }
0330 
0331 } // namespace chrono
0332 
0333 _LIBCPP_END_NAMESPACE_STD
0334 
0335 #endif // _LIBCPP_STD_VER >= 20
0336 
0337 #endif // _LIBCPP___CXX03___CHRONO_YEAR_MONTH_DAY_H