File indexing completed on 2026-05-03 08:13:16
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H
0011 #define _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H
0012
0013 #include <__chrono/calendar.h>
0014 #include <__chrono/day.h>
0015 #include <__chrono/duration.h>
0016 #include <__chrono/month.h>
0017 #include <__chrono/month_weekday.h>
0018 #include <__chrono/system_clock.h>
0019 #include <__chrono/time_point.h>
0020 #include <__chrono/weekday.h>
0021 #include <__chrono/year.h>
0022 #include <__chrono/year_month.h>
0023 #include <__chrono/year_month_day.h>
0024 #include <__config>
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_weekday {
0037 chrono::year __y_;
0038 chrono::month __m_;
0039 chrono::weekday_indexed __wdi_;
0040
0041 public:
0042 year_month_weekday() = default;
0043 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday(
0044 const chrono::year& __yval, const chrono::month& __mval, const chrono::weekday_indexed& __wdival) noexcept
0045 : __y_{__yval}, __m_{__mval}, __wdi_{__wdival} {}
0046 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday(const sys_days& __sysd) noexcept
0047 : year_month_weekday(__from_days(__sysd.time_since_epoch())) {}
0048 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_weekday(const local_days& __locd) noexcept
0049 : year_month_weekday(__from_days(__locd.time_since_epoch())) {}
0050 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator+=(const months&) noexcept;
0051 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator-=(const months&) noexcept;
0052 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator+=(const years&) noexcept;
0053 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator-=(const years&) noexcept;
0054
0055 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
0056 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
0057 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept { return __wdi_.weekday(); }
0058 _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned index() const noexcept { return __wdi_.index(); }
0059 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday_indexed weekday_indexed() const noexcept { return __wdi_; }
0060
0061 _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }
0062 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
0063 return local_days{__to_days()};
0064 }
0065 _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept {
0066 if (!__y_.ok() || !__m_.ok() || !__wdi_.ok())
0067 return false;
0068 if (__wdi_.index() <= 4)
0069 return true;
0070 auto __nth_weekday_day =
0071 __wdi_.weekday() - chrono::weekday{static_cast<sys_days>(__y_ / __m_ / 1)} + days{(__wdi_.index() - 1) * 7 + 1};
0072 return static_cast<unsigned>(__nth_weekday_day.count()) <= static_cast<unsigned>((__y_ / __m_ / last).day());
0073 }
0074
0075 _LIBCPP_HIDE_FROM_ABI static constexpr year_month_weekday __from_days(days __d) noexcept;
0076 _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
0077 };
0078
0079 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday year_month_weekday::__from_days(days __d) noexcept {
0080 const sys_days __sysd{__d};
0081 const chrono::weekday __wd = chrono::weekday(__sysd);
0082 const year_month_day __ymd = year_month_day(__sysd);
0083 return year_month_weekday{__ymd.year(), __ymd.month(), __wd[(static_cast<unsigned>(__ymd.day()) - 1) / 7 + 1]};
0084 }
0085
0086 _LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_weekday::__to_days() const noexcept {
0087 const sys_days __sysd = sys_days(__y_ / __m_ / 1);
0088 return (__sysd + (__wdi_.weekday() - chrono::weekday(__sysd) + days{(__wdi_.index() - 1) * 7})).time_since_epoch();
0089 }
0090
0091 _LIBCPP_HIDE_FROM_ABI inline constexpr bool
0092 operator==(const year_month_weekday& __lhs, const year_month_weekday& __rhs) noexcept {
0093 return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() &&
0094 __lhs.weekday_indexed() == __rhs.weekday_indexed();
0095 }
0096
0097 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0098 operator/(const year_month& __lhs, const weekday_indexed& __rhs) noexcept {
0099 return year_month_weekday{__lhs.year(), __lhs.month(), __rhs};
0100 }
0101
0102 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0103 operator/(const year& __lhs, const month_weekday& __rhs) noexcept {
0104 return year_month_weekday{__lhs, __rhs.month(), __rhs.weekday_indexed()};
0105 }
0106
0107 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday operator/(int __lhs, const month_weekday& __rhs) noexcept {
0108 return year(__lhs) / __rhs;
0109 }
0110
0111 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0112 operator/(const month_weekday& __lhs, const year& __rhs) noexcept {
0113 return __rhs / __lhs;
0114 }
0115
0116 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday operator/(const month_weekday& __lhs, int __rhs) noexcept {
0117 return year(__rhs) / __lhs;
0118 }
0119
0120 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0121 operator+(const year_month_weekday& __lhs, const months& __rhs) noexcept {
0122 return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_indexed();
0123 }
0124
0125 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0126 operator+(const months& __lhs, const year_month_weekday& __rhs) noexcept {
0127 return __rhs + __lhs;
0128 }
0129
0130 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0131 operator-(const year_month_weekday& __lhs, const months& __rhs) noexcept {
0132 return __lhs + (-__rhs);
0133 }
0134
0135 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0136 operator+(const year_month_weekday& __lhs, const years& __rhs) noexcept {
0137 return year_month_weekday{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_indexed()};
0138 }
0139
0140 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0141 operator+(const years& __lhs, const year_month_weekday& __rhs) noexcept {
0142 return __rhs + __lhs;
0143 }
0144
0145 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
0146 operator-(const year_month_weekday& __lhs, const years& __rhs) noexcept {
0147 return __lhs + (-__rhs);
0148 }
0149
0150 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const months& __dm) noexcept {
0151 *this = *this + __dm;
0152 return *this;
0153 }
0154 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const months& __dm) noexcept {
0155 *this = *this - __dm;
0156 return *this;
0157 }
0158 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const years& __dy) noexcept {
0159 *this = *this + __dy;
0160 return *this;
0161 }
0162 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const years& __dy) noexcept {
0163 *this = *this - __dy;
0164 return *this;
0165 }
0166
0167 class year_month_weekday_last {
0168 private:
0169 chrono::year __y_;
0170 chrono::month __m_;
0171 chrono::weekday_last __wdl_;
0172
0173 public:
0174 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last(
0175 const chrono::year& __yval, const chrono::month& __mval, const chrono::weekday_last& __wdlval) noexcept
0176 : __y_{__yval}, __m_{__mval}, __wdl_{__wdlval} {}
0177 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const months& __dm) noexcept;
0178 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const months& __dm) noexcept;
0179 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const years& __dy) noexcept;
0180 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const years& __dy) noexcept;
0181
0182 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
0183 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
0184 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept { return __wdl_.weekday(); }
0185 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday_last weekday_last() const noexcept { return __wdl_; }
0186 _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }
0187 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
0188 return local_days{__to_days()};
0189 }
0190 _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok() && __wdl_.ok(); }
0191
0192 _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
0193 };
0194
0195 _LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_weekday_last::__to_days() const noexcept {
0196 const sys_days __last = sys_days{__y_ / __m_ / last};
0197 return (__last - (chrono::weekday{__last} - __wdl_.weekday())).time_since_epoch();
0198 }
0199
0200 _LIBCPP_HIDE_FROM_ABI inline constexpr bool
0201 operator==(const year_month_weekday_last& __lhs, const year_month_weekday_last& __rhs) noexcept {
0202 return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.weekday_last() == __rhs.weekday_last();
0203 }
0204
0205 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0206 operator/(const year_month& __lhs, const weekday_last& __rhs) noexcept {
0207 return year_month_weekday_last{__lhs.year(), __lhs.month(), __rhs};
0208 }
0209
0210 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0211 operator/(const year& __lhs, const month_weekday_last& __rhs) noexcept {
0212 return year_month_weekday_last{__lhs, __rhs.month(), __rhs.weekday_last()};
0213 }
0214
0215 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0216 operator/(int __lhs, const month_weekday_last& __rhs) noexcept {
0217 return year(__lhs) / __rhs;
0218 }
0219
0220 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0221 operator/(const month_weekday_last& __lhs, const year& __rhs) noexcept {
0222 return __rhs / __lhs;
0223 }
0224
0225 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0226 operator/(const month_weekday_last& __lhs, int __rhs) noexcept {
0227 return year(__rhs) / __lhs;
0228 }
0229
0230 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0231 operator+(const year_month_weekday_last& __lhs, const months& __rhs) noexcept {
0232 return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_last();
0233 }
0234
0235 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0236 operator+(const months& __lhs, const year_month_weekday_last& __rhs) noexcept {
0237 return __rhs + __lhs;
0238 }
0239
0240 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0241 operator-(const year_month_weekday_last& __lhs, const months& __rhs) noexcept {
0242 return __lhs + (-__rhs);
0243 }
0244
0245 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0246 operator+(const year_month_weekday_last& __lhs, const years& __rhs) noexcept {
0247 return year_month_weekday_last{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_last()};
0248 }
0249
0250 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0251 operator+(const years& __lhs, const year_month_weekday_last& __rhs) noexcept {
0252 return __rhs + __lhs;
0253 }
0254
0255 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
0256 operator-(const year_month_weekday_last& __lhs, const years& __rhs) noexcept {
0257 return __lhs + (-__rhs);
0258 }
0259
0260 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
0261 year_month_weekday_last::operator+=(const months& __dm) noexcept {
0262 *this = *this + __dm;
0263 return *this;
0264 }
0265 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
0266 year_month_weekday_last::operator-=(const months& __dm) noexcept {
0267 *this = *this - __dm;
0268 return *this;
0269 }
0270 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
0271 year_month_weekday_last::operator+=(const years& __dy) noexcept {
0272 *this = *this + __dy;
0273 return *this;
0274 }
0275 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
0276 year_month_weekday_last::operator-=(const years& __dy) noexcept {
0277 *this = *this - __dy;
0278 return *this;
0279 }
0280
0281 }
0282
0283 _LIBCPP_END_NAMESPACE_STD
0284
0285 #endif
0286
0287 #endif