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_WEEKDAY_H
11#define _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_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/month_weekday.h>
18#include <__chrono/system_clock.h>
19#include <__chrono/time_point.h>
20#include <__chrono/weekday.h>
21#include <__chrono/year.h>
22#include <__chrono/year_month.h>
23#include <__chrono/year_month_day.h>
24#include <__config>
25#include <__cstddef/size_t.h>
26#include <__functional/hash.h>
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_weekday {
39 chrono::year __y_;
40 chrono::month __m_;
41 chrono::weekday_indexed __wdi_;
42
43public:
44 year_month_weekday() = default;
45 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday(
46 const chrono::year& __yval, const chrono::month& __mval, const chrono::weekday_indexed& __wdival) noexcept
47 : __y_{__yval}, __m_{__mval}, __wdi_{__wdival} {}
48 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday(const sys_days& __sysd) noexcept
49 : year_month_weekday(__from_days(d: __sysd.time_since_epoch())) {}
50 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_weekday(const local_days& __locd) noexcept
51 : year_month_weekday(__from_days(d: __locd.time_since_epoch())) {}
52 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator+=(const months&) noexcept;
53 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator-=(const months&) noexcept;
54 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator+=(const years&) noexcept;
55 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator-=(const years&) noexcept;
56
57 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
58 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
59 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept {
60 return __wdi_.weekday();
61 }
62 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned index() const noexcept { return __wdi_.index(); }
63 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday_indexed weekday_indexed() const noexcept {
64 return __wdi_;
65 }
66
67 _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }
68 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
69 return local_days{__to_days()};
70 }
71 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept {
72 if (!__y_.ok() || !__m_.ok() || !__wdi_.ok())
73 return false;
74 if (__wdi_.index() <= 4)
75 return true;
76 auto __nth_weekday_day =
77 __wdi_.weekday() - chrono::weekday{static_cast<sys_days>(__y_ / __m_ / 1)} + days{(__wdi_.index() - 1) * 7 + 1};
78 return static_cast<unsigned>(__nth_weekday_day.count()) <= static_cast<unsigned>((__y_ / __m_ / last).day());
79 }
80
81 _LIBCPP_HIDE_FROM_ABI static constexpr year_month_weekday __from_days(days __d) noexcept;
82 _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
83};
84
85_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday year_month_weekday::__from_days(days __d) noexcept {
86 const sys_days __sysd{__d};
87 const chrono::weekday __wd = chrono::weekday(__sysd);
88 const year_month_day __ymd = year_month_day(__sysd);
89 return year_month_weekday{__ymd.year(), __ymd.month(), __wd[(static_cast<unsigned>(__ymd.day()) - 1) / 7 + 1]};
90}
91
92_LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_weekday::__to_days() const noexcept {
93 const sys_days __sysd = sys_days(__y_ / __m_ / 1);
94 return (__sysd + (__wdi_.weekday() - chrono::weekday(__sysd) + days{(__wdi_.index() - 1) * 7})).time_since_epoch();
95}
96
97_LIBCPP_HIDE_FROM_ABI inline constexpr bool
98operator==(const year_month_weekday& __lhs, const year_month_weekday& __rhs) noexcept {
99 return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() &&
100 __lhs.weekday_indexed() == __rhs.weekday_indexed();
101}
102
103[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
104operator/(const year_month& __lhs, const weekday_indexed& __rhs) noexcept {
105 return year_month_weekday{__lhs.year(), __lhs.month(), __rhs};
106}
107
108[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
109operator/(const year& __lhs, const month_weekday& __rhs) noexcept {
110 return year_month_weekday{__lhs, __rhs.month(), __rhs.weekday_indexed()};
111}
112
113[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
114operator/(int __lhs, const month_weekday& __rhs) noexcept {
115 return year(__lhs) / __rhs;
116}
117
118[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
119operator/(const month_weekday& __lhs, const year& __rhs) noexcept {
120 return __rhs / __lhs;
121}
122
123[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
124operator/(const month_weekday& __lhs, int __rhs) noexcept {
125 return year(__rhs) / __lhs;
126}
127
128[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
129operator+(const year_month_weekday& __lhs, const months& __rhs) noexcept {
130 return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_indexed();
131}
132
133[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
134operator+(const months& __lhs, const year_month_weekday& __rhs) noexcept {
135 return __rhs + __lhs;
136}
137
138[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
139operator-(const year_month_weekday& __lhs, const months& __rhs) noexcept {
140 return __lhs + (-__rhs);
141}
142
143[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
144operator+(const year_month_weekday& __lhs, const years& __rhs) noexcept {
145 return year_month_weekday{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_indexed()};
146}
147
148[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
149operator+(const years& __lhs, const year_month_weekday& __rhs) noexcept {
150 return __rhs + __lhs;
151}
152
153[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday
154operator-(const year_month_weekday& __lhs, const years& __rhs) noexcept {
155 return __lhs + (-__rhs);
156}
157
158_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const months& __dm) noexcept {
159 *this = *this + __dm;
160 return *this;
161}
162_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const months& __dm) noexcept {
163 *this = *this - __dm;
164 return *this;
165}
166_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const years& __dy) noexcept {
167 *this = *this + __dy;
168 return *this;
169}
170_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const years& __dy) noexcept {
171 *this = *this - __dy;
172 return *this;
173}
174
175class year_month_weekday_last {
176private:
177 chrono::year __y_;
178 chrono::month __m_;
179 chrono::weekday_last __wdl_;
180
181public:
182 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last(
183 const chrono::year& __yval, const chrono::month& __mval, const chrono::weekday_last& __wdlval) noexcept
184 : __y_{__yval}, __m_{__mval}, __wdl_{__wdlval} {}
185 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const months& __dm) noexcept;
186 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const months& __dm) noexcept;
187 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const years& __dy) noexcept;
188 _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const years& __dy) noexcept;
189
190 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
191 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
192 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept {
193 return __wdl_.weekday();
194 }
195 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday_last weekday_last() const noexcept {
196 return __wdl_;
197 }
198 _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }
199 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
200 return local_days{__to_days()};
201 }
202 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept {
203 return __y_.ok() && __m_.ok() && __wdl_.ok();
204 }
205
206 _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
207};
208
209_LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_weekday_last::__to_days() const noexcept {
210 const sys_days __last = sys_days{__y_ / __m_ / last};
211 return (__last - (chrono::weekday{__last} - __wdl_.weekday())).time_since_epoch();
212}
213
214_LIBCPP_HIDE_FROM_ABI inline constexpr bool
215operator==(const year_month_weekday_last& __lhs, const year_month_weekday_last& __rhs) noexcept {
216 return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.weekday_last() == __rhs.weekday_last();
217}
218
219[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
220operator/(const year_month& __lhs, const weekday_last& __rhs) noexcept {
221 return year_month_weekday_last{__lhs.year(), __lhs.month(), __rhs};
222}
223
224[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
225operator/(const year& __lhs, const month_weekday_last& __rhs) noexcept {
226 return year_month_weekday_last{__lhs, __rhs.month(), __rhs.weekday_last()};
227}
228
229[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
230operator/(int __lhs, const month_weekday_last& __rhs) noexcept {
231 return year(__lhs) / __rhs;
232}
233
234[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
235operator/(const month_weekday_last& __lhs, const year& __rhs) noexcept {
236 return __rhs / __lhs;
237}
238
239[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
240operator/(const month_weekday_last& __lhs, int __rhs) noexcept {
241 return year(__rhs) / __lhs;
242}
243
244[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
245operator+(const year_month_weekday_last& __lhs, const months& __rhs) noexcept {
246 return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_last();
247}
248
249[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
250operator+(const months& __lhs, const year_month_weekday_last& __rhs) noexcept {
251 return __rhs + __lhs;
252}
253
254[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
255operator-(const year_month_weekday_last& __lhs, const months& __rhs) noexcept {
256 return __lhs + (-__rhs);
257}
258
259[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
260operator+(const year_month_weekday_last& __lhs, const years& __rhs) noexcept {
261 return year_month_weekday_last{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_last()};
262}
263
264[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
265operator+(const years& __lhs, const year_month_weekday_last& __rhs) noexcept {
266 return __rhs + __lhs;
267}
268
269[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last
270operator-(const year_month_weekday_last& __lhs, const years& __rhs) noexcept {
271 return __lhs + (-__rhs);
272}
273
274_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
275year_month_weekday_last::operator+=(const months& __dm) noexcept {
276 *this = *this + __dm;
277 return *this;
278}
279_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
280year_month_weekday_last::operator-=(const months& __dm) noexcept {
281 *this = *this - __dm;
282 return *this;
283}
284_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
285year_month_weekday_last::operator+=(const years& __dy) noexcept {
286 *this = *this + __dy;
287 return *this;
288}
289_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&
290year_month_weekday_last::operator-=(const years& __dy) noexcept {
291 *this = *this - __dy;
292 return *this;
293}
294
295} // namespace chrono
296
297# if _LIBCPP_STD_VER >= 26
298
299template <>
300struct hash<chrono::year_month_weekday> {
301 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::year_month_weekday& __ymw) noexcept {
302 return std::__hash_combine(
303 hash<chrono::year>{}(__ymw.year()),
304 std::__hash_combine(
305 hash<chrono::month>{}(__ymw.month()), hash<chrono::weekday_indexed>{}(__ymw.weekday_indexed())));
306 }
307};
308
309template <>
310struct hash<chrono::year_month_weekday_last> {
311 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::year_month_weekday_last& __ymwl) noexcept {
312 return std::__hash_combine(
313 hash<chrono::year>{}(__ymwl.year()),
314 std::__hash_combine(
315 hash<chrono::month>{}(__ymwl.month()), hash<chrono::weekday_last>{}(__ymwl.weekday_last())));
316 }
317};
318
319# endif // _LIBCPP_STD_VER >= 26
320
321_LIBCPP_END_NAMESPACE_STD
322
323#endif // _LIBCPP_STD_VER >= 20
324
325#endif // _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H
326