2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H
11 #define _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H
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>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
30 #if _LIBCPP_STD_VER >= 20
32 _LIBCPP_BEGIN_NAMESPACE_STD
37 class year_month_weekday
{
40 chrono::weekday_indexed __wdi_
;
42 year_month_weekday() = default;
43 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday(const chrono::year
& __yval
, const chrono::month
& __mval
,
44 const chrono::weekday_indexed
& __wdival
) noexcept
45 : __y_
{__yval
}, __m_
{__mval
}, __wdi_
{__wdival
} {}
46 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday(const sys_days
& __sysd
) noexcept
47 : year_month_weekday(__from_days(__sysd
.time_since_epoch())) {}
48 _LIBCPP_HIDE_FROM_ABI
inline explicit constexpr year_month_weekday(const local_days
& __locd
) noexcept
49 : year_month_weekday(__from_days(__locd
.time_since_epoch())) {}
50 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday
& operator+=(const months
&) noexcept
;
51 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday
& operator-=(const months
&) noexcept
;
52 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday
& operator+=(const years
&) noexcept
;
53 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday
& operator-=(const years
&) noexcept
;
55 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::year
year() const noexcept
{ return __y_
; }
56 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::month
month() const noexcept
{ return __m_
; }
57 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::weekday
weekday() const noexcept
{ return __wdi_
.weekday(); }
58 _LIBCPP_HIDE_FROM_ABI
inline constexpr unsigned index() const noexcept
{ return __wdi_
.index(); }
59 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::weekday_indexed
weekday_indexed() const noexcept
{ return __wdi_
; }
61 _LIBCPP_HIDE_FROM_ABI
inline constexpr operator sys_days() const noexcept
{ return sys_days
{__to_days()}; }
62 _LIBCPP_HIDE_FROM_ABI
inline explicit constexpr operator local_days() const noexcept
{ return local_days
{__to_days()}; }
63 _LIBCPP_HIDE_FROM_ABI
inline constexpr bool ok() const noexcept
65 if (!__y_
.ok() || !__m_
.ok() || !__wdi_
.ok()) return false;
66 if (__wdi_
.index() <= 4) return true;
67 auto __nth_weekday_day
=
69 chrono::weekday
{static_cast<sys_days
>(__y_
/ __m_
/ 1)} +
70 days
{(__wdi_
.index() - 1) * 7 + 1};
71 return static_cast<unsigned>(__nth_weekday_day
.count()) <=
72 static_cast<unsigned>((__y_
/ __m_
/ last
).day());
75 _LIBCPP_HIDE_FROM_ABI
static constexpr year_month_weekday
__from_days(days __d
) noexcept
;
76 _LIBCPP_HIDE_FROM_ABI
constexpr days
__to_days() const noexcept
;
79 _LIBCPP_HIDE_FROM_ABI
inline constexpr
80 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(),
86 __wd
[(static_cast<unsigned>(__ymd
.day())-1)/7+1]};
89 _LIBCPP_HIDE_FROM_ABI
inline constexpr
90 days
year_month_weekday::__to_days() const noexcept
92 const sys_days __sysd
= sys_days(__y_
/__m_
/1);
93 return (__sysd
+ (__wdi_
.weekday() - chrono::weekday(__sysd
) + days
{(__wdi_
.index()-1)*7}))
97 _LIBCPP_HIDE_FROM_ABI
inline constexpr
98 bool operator==(const year_month_weekday
& __lhs
, const year_month_weekday
& __rhs
) noexcept
99 { return __lhs
.year() == __rhs
.year() && __lhs
.month() == __rhs
.month() && __lhs
.weekday_indexed() == __rhs
.weekday_indexed(); }
101 _LIBCPP_HIDE_FROM_ABI
inline constexpr
102 year_month_weekday
operator/(const year_month
& __lhs
, const weekday_indexed
& __rhs
) noexcept
103 { return year_month_weekday
{__lhs
.year(), __lhs
.month(), __rhs
}; }
105 _LIBCPP_HIDE_FROM_ABI
inline constexpr
106 year_month_weekday
operator/(const year
& __lhs
, const month_weekday
& __rhs
) noexcept
107 { return year_month_weekday
{__lhs
, __rhs
.month(), __rhs
.weekday_indexed()}; }
109 _LIBCPP_HIDE_FROM_ABI
inline constexpr
110 year_month_weekday
operator/(int __lhs
, const month_weekday
& __rhs
) noexcept
111 { return year(__lhs
) / __rhs
; }
113 _LIBCPP_HIDE_FROM_ABI
inline constexpr
114 year_month_weekday
operator/(const month_weekday
& __lhs
, const year
& __rhs
) noexcept
115 { return __rhs
/ __lhs
; }
117 _LIBCPP_HIDE_FROM_ABI
inline constexpr
118 year_month_weekday
operator/(const month_weekday
& __lhs
, int __rhs
) noexcept
119 { return year(__rhs
) / __lhs
; }
122 _LIBCPP_HIDE_FROM_ABI
inline constexpr
123 year_month_weekday
operator+(const year_month_weekday
& __lhs
, const months
& __rhs
) noexcept
124 { return (__lhs
.year() / __lhs
.month() + __rhs
) / __lhs
.weekday_indexed(); }
126 _LIBCPP_HIDE_FROM_ABI
inline constexpr
127 year_month_weekday
operator+(const months
& __lhs
, const year_month_weekday
& __rhs
) noexcept
128 { return __rhs
+ __lhs
; }
130 _LIBCPP_HIDE_FROM_ABI
inline constexpr
131 year_month_weekday
operator-(const year_month_weekday
& __lhs
, const months
& __rhs
) noexcept
132 { return __lhs
+ (-__rhs
); }
134 _LIBCPP_HIDE_FROM_ABI
inline constexpr
135 year_month_weekday
operator+(const year_month_weekday
& __lhs
, const years
& __rhs
) noexcept
136 { return year_month_weekday
{__lhs
.year() + __rhs
, __lhs
.month(), __lhs
.weekday_indexed()}; }
138 _LIBCPP_HIDE_FROM_ABI
inline constexpr
139 year_month_weekday
operator+(const years
& __lhs
, const year_month_weekday
& __rhs
) noexcept
140 { return __rhs
+ __lhs
; }
142 _LIBCPP_HIDE_FROM_ABI
inline constexpr
143 year_month_weekday
operator-(const year_month_weekday
& __lhs
, const years
& __rhs
) noexcept
144 { return __lhs
+ (-__rhs
); }
147 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday
& year_month_weekday::operator+=(const months
& __dm
) noexcept
{ *this = *this + __dm
; return *this; }
148 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday
& year_month_weekday::operator-=(const months
& __dm
) noexcept
{ *this = *this - __dm
; return *this; }
149 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday
& year_month_weekday::operator+=(const years
& __dy
) noexcept
{ *this = *this + __dy
; return *this; }
150 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday
& year_month_weekday::operator-=(const years
& __dy
) noexcept
{ *this = *this - __dy
; return *this; }
152 class year_month_weekday_last
{
156 chrono::weekday_last __wdl_
;
158 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday_last(const chrono::year
& __yval
, const chrono::month
& __mval
,
159 const chrono::weekday_last
& __wdlval
) noexcept
160 : __y_
{__yval
}, __m_
{__mval
}, __wdl_
{__wdlval
} {}
161 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday_last
& operator+=(const months
& __dm
) noexcept
;
162 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday_last
& operator-=(const months
& __dm
) noexcept
;
163 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday_last
& operator+=(const years
& __dy
) noexcept
;
164 _LIBCPP_HIDE_FROM_ABI
constexpr year_month_weekday_last
& operator-=(const years
& __dy
) noexcept
;
166 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::year
year() const noexcept
{ return __y_
; }
167 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::month
month() const noexcept
{ return __m_
; }
168 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::weekday
weekday() const noexcept
{ return __wdl_
.weekday(); }
169 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::weekday_last
weekday_last() const noexcept
{ return __wdl_
; }
170 _LIBCPP_HIDE_FROM_ABI
inline constexpr operator sys_days() const noexcept
{ return sys_days
{__to_days()}; }
171 _LIBCPP_HIDE_FROM_ABI
inline explicit constexpr operator local_days() const noexcept
{ return local_days
{__to_days()}; }
172 _LIBCPP_HIDE_FROM_ABI
inline constexpr bool ok() const noexcept
{ return __y_
.ok() && __m_
.ok() && __wdl_
.ok(); }
174 _LIBCPP_HIDE_FROM_ABI
constexpr days
__to_days() const noexcept
;
178 _LIBCPP_HIDE_FROM_ABI
inline constexpr
179 days
year_month_weekday_last::__to_days() const noexcept
181 const sys_days __last
= sys_days
{__y_
/__m_
/last
};
182 return (__last
- (chrono::weekday
{__last
} - __wdl_
.weekday())).time_since_epoch();
186 _LIBCPP_HIDE_FROM_ABI
inline constexpr
187 bool operator==(const year_month_weekday_last
& __lhs
, const year_month_weekday_last
& __rhs
) noexcept
188 { return __lhs
.year() == __rhs
.year() && __lhs
.month() == __rhs
.month() && __lhs
.weekday_last() == __rhs
.weekday_last(); }
190 _LIBCPP_HIDE_FROM_ABI
inline constexpr
191 year_month_weekday_last
operator/(const year_month
& __lhs
, const weekday_last
& __rhs
) noexcept
192 { return year_month_weekday_last
{__lhs
.year(), __lhs
.month(), __rhs
}; }
194 _LIBCPP_HIDE_FROM_ABI
inline constexpr
195 year_month_weekday_last
operator/(const year
& __lhs
, const month_weekday_last
& __rhs
) noexcept
196 { return year_month_weekday_last
{__lhs
, __rhs
.month(), __rhs
.weekday_last()}; }
198 _LIBCPP_HIDE_FROM_ABI
inline constexpr
199 year_month_weekday_last
operator/(int __lhs
, const month_weekday_last
& __rhs
) noexcept
200 { return year(__lhs
) / __rhs
; }
202 _LIBCPP_HIDE_FROM_ABI
inline constexpr
203 year_month_weekday_last
operator/(const month_weekday_last
& __lhs
, const year
& __rhs
) noexcept
204 { return __rhs
/ __lhs
; }
206 _LIBCPP_HIDE_FROM_ABI
inline constexpr
207 year_month_weekday_last
operator/(const month_weekday_last
& __lhs
, int __rhs
) noexcept
208 { return year(__rhs
) / __lhs
; }
211 _LIBCPP_HIDE_FROM_ABI
inline constexpr
212 year_month_weekday_last
operator+(const year_month_weekday_last
& __lhs
, const months
& __rhs
) noexcept
213 { return (__lhs
.year() / __lhs
.month() + __rhs
) / __lhs
.weekday_last(); }
215 _LIBCPP_HIDE_FROM_ABI
inline constexpr
216 year_month_weekday_last
operator+(const months
& __lhs
, const year_month_weekday_last
& __rhs
) noexcept
217 { return __rhs
+ __lhs
; }
219 _LIBCPP_HIDE_FROM_ABI
inline constexpr
220 year_month_weekday_last
operator-(const year_month_weekday_last
& __lhs
, const months
& __rhs
) noexcept
221 { return __lhs
+ (-__rhs
); }
223 _LIBCPP_HIDE_FROM_ABI
inline constexpr
224 year_month_weekday_last
operator+(const year_month_weekday_last
& __lhs
, const years
& __rhs
) noexcept
225 { return year_month_weekday_last
{__lhs
.year() + __rhs
, __lhs
.month(), __lhs
.weekday_last()}; }
227 _LIBCPP_HIDE_FROM_ABI
inline constexpr
228 year_month_weekday_last
operator+(const years
& __lhs
, const year_month_weekday_last
& __rhs
) noexcept
229 { return __rhs
+ __lhs
; }
231 _LIBCPP_HIDE_FROM_ABI
inline constexpr
232 year_month_weekday_last
operator-(const year_month_weekday_last
& __lhs
, const years
& __rhs
) noexcept
233 { return __lhs
+ (-__rhs
); }
235 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday_last
& year_month_weekday_last::operator+=(const months
& __dm
) noexcept
{ *this = *this + __dm
; return *this; }
236 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday_last
& year_month_weekday_last::operator-=(const months
& __dm
) noexcept
{ *this = *this - __dm
; return *this; }
237 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday_last
& year_month_weekday_last::operator+=(const years
& __dy
) noexcept
{ *this = *this + __dy
; return *this; }
238 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month_weekday_last
& year_month_weekday_last::operator-=(const years
& __dy
) noexcept
{ *this = *this - __dy
; return *this; }
240 } // namespace chrono
242 _LIBCPP_END_NAMESPACE_STD
244 #endif // _LIBCPP_STD_VER >= 20
246 #endif // _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H