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