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
34namespace chrono {
35
36class year_month_day_last;
37
38class year_month_day {
39private:
40 chrono::year __y_;
41 chrono::month __m_;
42 chrono::day __d_;
43
44public:
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
107operator==(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
112operator<=>(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
145operator+(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
150operator+(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
155operator-(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
160operator+(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
165operator+(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
170operator-(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
191class year_month_day_last {
192private:
193 chrono::year __y_;
194 chrono::month_day_last __mdl_;
195
196public:
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
238operator==(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
243operator<=>(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
254operator/(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
263operator/(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
272operator+(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
277operator+(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
282operator-(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
287operator+(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
292operator+(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
297operator-(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&
302year_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&
307year_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&
312year_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&
317year_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