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