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 | 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 | |
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(d: __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(d: __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 { |
65 | return local_days{__to_days()}; |
66 | } |
67 | |
68 | _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept; |
69 | |
70 | _LIBCPP_HIDE_FROM_ABI static constexpr year_month_day __from_days(days __d) noexcept; |
71 | _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept; |
72 | }; |
73 | |
74 | // https://howardhinnant.github.io/date_algorithms.html#civil_from_days |
75 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day year_month_day::__from_days(days __d) noexcept { |
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 days year_month_day::__to_days() const noexcept { |
92 | static_assert(numeric_limits<unsigned>::digits >= 18, "" ); |
93 | static_assert(numeric_limits<int>::digits >= 20, "" ); |
94 | |
95 | const int __yr = static_cast<int>(__y_) - (__m_ <= February); |
96 | const unsigned __mth = static_cast<unsigned>(__m_); |
97 | const unsigned __dy = static_cast<unsigned>(__d_); |
98 | |
99 | const int __era = (__yr >= 0 ? __yr : __yr - 399) / 400; |
100 | const unsigned __yoe = static_cast<unsigned>(__yr - __era * 400); // [0, 399] |
101 | const unsigned __doy = (153 * (__mth + (__mth > 2 ? -3 : 9)) + 2) / 5 + __dy - 1; // [0, 365] |
102 | const unsigned __doe = __yoe * 365 + __yoe / 4 - __yoe / 100 + __doy; // [0, 146096] |
103 | return days{__era * 146097 + static_cast<int>(__doe) - 719468}; |
104 | } |
105 | |
106 | _LIBCPP_HIDE_FROM_ABI inline constexpr bool |
107 | operator==(const year_month_day& __lhs, const year_month_day& __rhs) noexcept { |
108 | return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); |
109 | } |
110 | |
111 | _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering |
112 | operator<=>(const year_month_day& __lhs, const year_month_day& __rhs) noexcept { |
113 | if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0) |
114 | return __c; |
115 | if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0) |
116 | return __c; |
117 | return __lhs.day() <=> __rhs.day(); |
118 | } |
119 | |
120 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, const day& __rhs) noexcept { |
121 | return year_month_day{__lhs.year(), __lhs.month(), __rhs}; |
122 | } |
123 | |
124 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, int __rhs) noexcept { |
125 | return __lhs / day(__rhs); |
126 | } |
127 | |
128 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year& __lhs, const month_day& __rhs) noexcept { |
129 | return __lhs / __rhs.month() / __rhs.day(); |
130 | } |
131 | |
132 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(int __lhs, const month_day& __rhs) noexcept { |
133 | return year(__lhs) / __rhs; |
134 | } |
135 | |
136 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, const year& __rhs) noexcept { |
137 | return __rhs / __lhs; |
138 | } |
139 | |
140 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, int __rhs) noexcept { |
141 | return year(__rhs) / __lhs; |
142 | } |
143 | |
144 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
145 | operator+(const year_month_day& __lhs, const months& __rhs) noexcept { |
146 | return (__lhs.year() / __lhs.month() + __rhs) / __lhs.day(); |
147 | } |
148 | |
149 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
150 | operator+(const months& __lhs, const year_month_day& __rhs) noexcept { |
151 | return __rhs + __lhs; |
152 | } |
153 | |
154 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
155 | operator-(const year_month_day& __lhs, const months& __rhs) noexcept { |
156 | return __lhs + -__rhs; |
157 | } |
158 | |
159 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
160 | operator+(const year_month_day& __lhs, const years& __rhs) noexcept { |
161 | return (__lhs.year() + __rhs) / __lhs.month() / __lhs.day(); |
162 | } |
163 | |
164 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
165 | operator+(const years& __lhs, const year_month_day& __rhs) noexcept { |
166 | return __rhs + __lhs; |
167 | } |
168 | |
169 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day |
170 | operator-(const year_month_day& __lhs, const years& __rhs) noexcept { |
171 | return __lhs + -__rhs; |
172 | } |
173 | |
174 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const months& __dm) noexcept { |
175 | *this = *this + __dm; |
176 | return *this; |
177 | } |
178 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const months& __dm) noexcept { |
179 | *this = *this - __dm; |
180 | return *this; |
181 | } |
182 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const years& __dy) noexcept { |
183 | *this = *this + __dy; |
184 | return *this; |
185 | } |
186 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const years& __dy) noexcept { |
187 | *this = *this - __dy; |
188 | return *this; |
189 | } |
190 | |
191 | class year_month_day_last { |
192 | private: |
193 | chrono::year __y_; |
194 | chrono::month_day_last __mdl_; |
195 | |
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 { |
210 | return sys_days{year() / month() / day()}; |
211 | } |
212 | _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept { |
213 | return local_days{year() / month() / day()}; |
214 | } |
215 | _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __mdl_.ok(); } |
216 | }; |
217 | |
218 | _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day year_month_day_last::day() const noexcept { |
219 | constexpr chrono::day __d[] = { |
220 | chrono::day(31), |
221 | chrono::day(28), |
222 | chrono::day(31), |
223 | chrono::day(30), |
224 | chrono::day(31), |
225 | chrono::day(30), |
226 | chrono::day(31), |
227 | chrono::day(31), |
228 | chrono::day(30), |
229 | chrono::day(31), |
230 | chrono::day(30), |
231 | chrono::day(31)}; |
232 | return (month() != February || !__y_.is_leap()) && month().ok() |
233 | ? __d[static_cast<unsigned>(month()) - 1] |
234 | : chrono::day{29}; |
235 | } |
236 | |
237 | _LIBCPP_HIDE_FROM_ABI inline constexpr bool |
238 | operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { |
239 | return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last(); |
240 | } |
241 | |
242 | _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering |
243 | operator<=>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept { |
244 | if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0) |
245 | return __c; |
246 | return __lhs.month_day_last() <=> __rhs.month_day_last(); |
247 | } |
248 | |
249 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept { |
250 | return year_month_day_last{__lhs.year(), month_day_last{__lhs.month()}}; |
251 | } |
252 | |
253 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
254 | operator/(const year& __lhs, const month_day_last& __rhs) noexcept { |
255 | return year_month_day_last{__lhs, __rhs}; |
256 | } |
257 | |
258 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(int __lhs, const month_day_last& __rhs) noexcept { |
259 | return year_month_day_last{year{__lhs}, __rhs}; |
260 | } |
261 | |
262 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
263 | operator/(const month_day_last& __lhs, const year& __rhs) noexcept { |
264 | return __rhs / __lhs; |
265 | } |
266 | |
267 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const month_day_last& __lhs, int __rhs) noexcept { |
268 | return year{__rhs} / __lhs; |
269 | } |
270 | |
271 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
272 | operator+(const year_month_day_last& __lhs, const months& __rhs) noexcept { |
273 | return (__lhs.year() / __lhs.month() + __rhs) / last; |
274 | } |
275 | |
276 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
277 | operator+(const months& __lhs, const year_month_day_last& __rhs) noexcept { |
278 | return __rhs + __lhs; |
279 | } |
280 | |
281 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
282 | operator-(const year_month_day_last& __lhs, const months& __rhs) noexcept { |
283 | return __lhs + (-__rhs); |
284 | } |
285 | |
286 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
287 | operator+(const year_month_day_last& __lhs, const years& __rhs) noexcept { |
288 | return year_month_day_last{__lhs.year() + __rhs, __lhs.month_day_last()}; |
289 | } |
290 | |
291 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
292 | operator+(const years& __lhs, const year_month_day_last& __rhs) noexcept { |
293 | return __rhs + __lhs; |
294 | } |
295 | |
296 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last |
297 | operator-(const year_month_day_last& __lhs, const years& __rhs) noexcept { |
298 | return __lhs + (-__rhs); |
299 | } |
300 | |
301 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
302 | year_month_day_last::operator+=(const months& __dm) noexcept { |
303 | *this = *this + __dm; |
304 | return *this; |
305 | } |
306 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
307 | year_month_day_last::operator-=(const months& __dm) noexcept { |
308 | *this = *this - __dm; |
309 | return *this; |
310 | } |
311 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
312 | year_month_day_last::operator+=(const years& __dy) noexcept { |
313 | *this = *this + __dy; |
314 | return *this; |
315 | } |
316 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& |
317 | year_month_day_last::operator-=(const years& __dy) noexcept { |
318 | *this = *this - __dy; |
319 | return *this; |
320 | } |
321 | |
322 | _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept |
323 | : __y_{__ymdl.year()}, __m_{__ymdl.month()}, __d_{__ymdl.day()} {} |
324 | |
325 | _LIBCPP_HIDE_FROM_ABI inline constexpr bool year_month_day::ok() const noexcept { |
326 | if (!__y_.ok() || !__m_.ok()) |
327 | return false; |
328 | return chrono::day{1} <= __d_ && __d_ <= (__y_ / __m_ / last).day(); |
329 | } |
330 | |
331 | } // namespace chrono |
332 | |
333 | _LIBCPP_END_NAMESPACE_STD |
334 | |
335 | #endif // _LIBCPP_STD_VER >= 20 |
336 | |
337 | #endif // _LIBCPP___CHRONO_YEAR_MONTH_DAY_H |
338 | |