xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/year_month_weekday.h (revision 577b62c2bacc7dfa228591ca3da361e1bc398301)
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 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 #if _LIBCPP_STD_VER > 17
31 
32 _LIBCPP_BEGIN_NAMESPACE_STD
33 
34 namespace chrono
35 {
36 
37 class year_month_weekday {
38     chrono::year            __y;
39     chrono::month           __m;
40     chrono::weekday_indexed __wdi;
41 public:
42     _LIBCPP_HIDE_FROM_ABI 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;
54 
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; }
60 
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
64     {
65         if (!__y.ok() || !__m.ok() || !__wdi.ok()) return false;
66         if (__wdi.index() <= 4) return true;
67         auto __nth_weekday_day =
68             __wdi.weekday() -
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());
73     }
74 
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;
77 };
78 
79 _LIBCPP_HIDE_FROM_ABI inline constexpr
80 year_month_weekday year_month_weekday::__from_days(days __d) noexcept
81 {
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]};
87 }
88 
89 _LIBCPP_HIDE_FROM_ABI inline constexpr
90 days year_month_weekday::__to_days() const noexcept
91 {
92     const sys_days __sysd = sys_days(__y/__m/1);
93     return (__sysd + (__wdi.weekday() - chrono::weekday(__sysd) + days{(__wdi.index()-1)*7}))
94                 .time_since_epoch();
95 }
96 
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(); }
100 
101 _LIBCPP_HIDE_FROM_ABI inline constexpr
102 bool operator!=(const year_month_weekday& __lhs, const year_month_weekday& __rhs) noexcept
103 { return !(__lhs == __rhs); }
104 
105 _LIBCPP_HIDE_FROM_ABI inline constexpr
106 year_month_weekday operator/(const year_month& __lhs, const weekday_indexed& __rhs) noexcept
107 { return year_month_weekday{__lhs.year(), __lhs.month(), __rhs}; }
108 
109 _LIBCPP_HIDE_FROM_ABI inline constexpr
110 year_month_weekday operator/(const year& __lhs, const month_weekday& __rhs) noexcept
111 { return year_month_weekday{__lhs, __rhs.month(), __rhs.weekday_indexed()}; }
112 
113 _LIBCPP_HIDE_FROM_ABI inline constexpr
114 year_month_weekday operator/(int __lhs, const month_weekday& __rhs) noexcept
115 { return year(__lhs) / __rhs; }
116 
117 _LIBCPP_HIDE_FROM_ABI inline constexpr
118 year_month_weekday operator/(const month_weekday& __lhs, const year& __rhs) noexcept
119 { return __rhs / __lhs; }
120 
121 _LIBCPP_HIDE_FROM_ABI inline constexpr
122 year_month_weekday operator/(const month_weekday& __lhs, int __rhs) noexcept
123 { return year(__rhs) / __lhs; }
124 
125 
126 _LIBCPP_HIDE_FROM_ABI inline constexpr
127 year_month_weekday operator+(const year_month_weekday& __lhs, const months& __rhs) noexcept
128 { return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_indexed(); }
129 
130 _LIBCPP_HIDE_FROM_ABI inline constexpr
131 year_month_weekday operator+(const months& __lhs, const year_month_weekday& __rhs) noexcept
132 { return __rhs + __lhs; }
133 
134 _LIBCPP_HIDE_FROM_ABI inline constexpr
135 year_month_weekday operator-(const year_month_weekday& __lhs, const months& __rhs) noexcept
136 { return __lhs + (-__rhs); }
137 
138 _LIBCPP_HIDE_FROM_ABI inline constexpr
139 year_month_weekday operator+(const year_month_weekday& __lhs, const years& __rhs) noexcept
140 { return year_month_weekday{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_indexed()}; }
141 
142 _LIBCPP_HIDE_FROM_ABI inline constexpr
143 year_month_weekday operator+(const years& __lhs, const year_month_weekday& __rhs) noexcept
144 { return __rhs + __lhs; }
145 
146 _LIBCPP_HIDE_FROM_ABI inline constexpr
147 year_month_weekday operator-(const year_month_weekday& __lhs, const years& __rhs) noexcept
148 { return __lhs + (-__rhs); }
149 
150 
151 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const months& __dm) noexcept { *this = *this + __dm; return *this; }
152 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const months& __dm) noexcept { *this = *this - __dm; return *this; }
153 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const years& __dy)  noexcept { *this = *this + __dy; return *this; }
154 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const years& __dy)  noexcept { *this = *this - __dy; return *this; }
155 
156 class year_month_weekday_last {
157 private:
158     chrono::year         __y;
159     chrono::month        __m;
160     chrono::weekday_last __wdl;
161 public:
162     _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last(const chrono::year& __yval, const chrono::month& __mval,
163                                       const chrono::weekday_last& __wdlval) noexcept
164                 : __y{__yval}, __m{__mval}, __wdl{__wdlval} {}
165     _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const months& __dm) noexcept;
166     _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const months& __dm) noexcept;
167     _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const years& __dy)  noexcept;
168     _LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const years& __dy)  noexcept;
169 
170     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year                 year() const noexcept { return __y; }
171     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month               month() const noexcept { return __m; }
172     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday           weekday() const noexcept { return __wdl.weekday(); }
173     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday_last weekday_last() const noexcept { return __wdl; }
174     _LIBCPP_HIDE_FROM_ABI inline constexpr operator                 sys_days() const noexcept { return   sys_days{__to_days()}; }
175     _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator      local_days() const noexcept { return local_days{__to_days()}; }
176     _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y.ok() && __m.ok() && __wdl.ok(); }
177 
178     _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
179 
180 };
181 
182 _LIBCPP_HIDE_FROM_ABI inline constexpr
183 days year_month_weekday_last::__to_days() const noexcept
184 {
185     const sys_days __last = sys_days{__y/__m/last};
186     return (__last - (chrono::weekday{__last} - __wdl.weekday())).time_since_epoch();
187 
188 }
189 
190 _LIBCPP_HIDE_FROM_ABI inline constexpr
191 bool operator==(const year_month_weekday_last& __lhs, const year_month_weekday_last& __rhs) noexcept
192 { return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.weekday_last() == __rhs.weekday_last(); }
193 
194 _LIBCPP_HIDE_FROM_ABI inline constexpr
195 bool operator!=(const year_month_weekday_last& __lhs, const year_month_weekday_last& __rhs) noexcept
196 { return !(__lhs == __rhs); }
197 
198 
199 _LIBCPP_HIDE_FROM_ABI inline constexpr
200 year_month_weekday_last operator/(const year_month& __lhs, const weekday_last& __rhs) noexcept
201 { return year_month_weekday_last{__lhs.year(), __lhs.month(), __rhs}; }
202 
203 _LIBCPP_HIDE_FROM_ABI inline constexpr
204 year_month_weekday_last operator/(const year& __lhs, const month_weekday_last& __rhs) noexcept
205 { return year_month_weekday_last{__lhs, __rhs.month(), __rhs.weekday_last()}; }
206 
207 _LIBCPP_HIDE_FROM_ABI inline constexpr
208 year_month_weekday_last operator/(int __lhs, const month_weekday_last& __rhs) noexcept
209 { return year(__lhs) / __rhs; }
210 
211 _LIBCPP_HIDE_FROM_ABI inline constexpr
212 year_month_weekday_last operator/(const month_weekday_last& __lhs, const year& __rhs) noexcept
213 { return __rhs / __lhs; }
214 
215 _LIBCPP_HIDE_FROM_ABI inline constexpr
216 year_month_weekday_last operator/(const month_weekday_last& __lhs, int __rhs) noexcept
217 { return year(__rhs) / __lhs; }
218 
219 
220 _LIBCPP_HIDE_FROM_ABI inline constexpr
221 year_month_weekday_last operator+(const year_month_weekday_last& __lhs, const months& __rhs) noexcept
222 { return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_last(); }
223 
224 _LIBCPP_HIDE_FROM_ABI inline constexpr
225 year_month_weekday_last operator+(const months& __lhs, const year_month_weekday_last& __rhs) noexcept
226 { return __rhs + __lhs; }
227 
228 _LIBCPP_HIDE_FROM_ABI inline constexpr
229 year_month_weekday_last operator-(const year_month_weekday_last& __lhs, const months& __rhs) noexcept
230 { return __lhs + (-__rhs); }
231 
232 _LIBCPP_HIDE_FROM_ABI inline constexpr
233 year_month_weekday_last operator+(const year_month_weekday_last& __lhs, const years& __rhs) noexcept
234 { return year_month_weekday_last{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_last()}; }
235 
236 _LIBCPP_HIDE_FROM_ABI inline constexpr
237 year_month_weekday_last operator+(const years& __lhs, const year_month_weekday_last& __rhs) noexcept
238 { return __rhs + __lhs; }
239 
240 _LIBCPP_HIDE_FROM_ABI inline constexpr
241 year_month_weekday_last operator-(const year_month_weekday_last& __lhs, const years& __rhs) noexcept
242 { return __lhs + (-__rhs); }
243 
244 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last& year_month_weekday_last::operator+=(const months& __dm) noexcept { *this = *this + __dm; return *this; }
245 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last& year_month_weekday_last::operator-=(const months& __dm) noexcept { *this = *this - __dm; return *this; }
246 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last& year_month_weekday_last::operator+=(const years& __dy)  noexcept { *this = *this + __dy; return *this; }
247 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last& year_month_weekday_last::operator-=(const years& __dy)  noexcept { *this = *this - __dy; return *this; }
248 
249 } // namespace chrono
250 
251 _LIBCPP_END_NAMESPACE_STD
252 
253 #endif // _LIBCPP_STD_VER > 17
254 
255 #endif // _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H
256