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