Revert "[InstCombine] Support gep nuw in icmp folds" (#118698)
[llvm-project.git] / libcxx / include / __chrono / year_month_day.h
bloba0510a14f4edeb93d24e65ce5388f82808383de0
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHRONO_YEAR_MONTH_DAY_H
11 #define _LIBCPP___CHRONO_YEAR_MONTH_DAY_H
13 #include <__chrono/calendar.h>
14 #include <__chrono/day.h>
15 #include <__chrono/duration.h>
16 #include <__chrono/month.h>
17 #include <__chrono/monthday.h>
18 #include <__chrono/system_clock.h>
19 #include <__chrono/time_point.h>
20 #include <__chrono/year.h>
21 #include <__chrono/year_month.h>
22 #include <__compare/ordering.h>
23 #include <__config>
24 #include <limits>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
28 #endif
30 #if _LIBCPP_STD_VER >= 20
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 namespace chrono {
36 class year_month_day_last;
38 class year_month_day {
39 private:
40 chrono::year __y_;
41 chrono::month __m_;
42 chrono::day __d_;
44 public:
45 year_month_day() = default;
46 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(
47 const chrono::year& __yval, const chrono::month& __mval, const chrono::day& __dval) noexcept
48 : __y_{__yval}, __m_{__mval}, __d_{__dval} {}
49 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day(const year_month_day_last& __ymdl) noexcept;
50 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(const sys_days& __sysd) noexcept
51 : year_month_day(__from_days(__sysd.time_since_epoch())) {}
52 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_day(const local_days& __locd) noexcept
53 : year_month_day(__from_days(__locd.time_since_epoch())) {}
55 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const months& __dm) noexcept;
56 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const months& __dm) noexcept;
57 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const years& __dy) noexcept;
58 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const years& __dy) noexcept;
60 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
61 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
62 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
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()};
68 _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
70 _LIBCPP_HIDE_FROM_ABI static constexpr year_month_day __from_days(days __d) noexcept;
71 _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
74 // https://howardhinnant.github.io/date_algorithms.html#civil_from_days
75 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day year_month_day::__from_days(days __d) noexcept {
76 static_assert(numeric_limits<unsigned>::digits >= 18, "");
77 static_assert(numeric_limits<int>::digits >= 20, "");
78 const int __z = __d.count() + 719468;
79 const int __era = (__z >= 0 ? __z : __z - 146096) / 146097;
80 const unsigned __doe = static_cast<unsigned>(__z - __era * 146097); // [0, 146096]
81 const unsigned __yoe = (__doe - __doe / 1460 + __doe / 36524 - __doe / 146096) / 365; // [0, 399]
82 const int __yr = static_cast<int>(__yoe) + __era * 400;
83 const unsigned __doy = __doe - (365 * __yoe + __yoe / 4 - __yoe / 100); // [0, 365]
84 const unsigned __mp = (5 * __doy + 2) / 153; // [0, 11]
85 const unsigned __dy = __doy - (153 * __mp + 2) / 5 + 1; // [1, 31]
86 const unsigned __mth = __mp + (__mp < 10 ? 3 : -9); // [1, 12]
87 return year_month_day{chrono::year{__yr + (__mth <= 2)}, chrono::month{__mth}, chrono::day{__dy}};
90 // https://howardhinnant.github.io/date_algorithms.html#days_from_civil
91 _LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_day::__to_days() const noexcept {
92 static_assert(numeric_limits<unsigned>::digits >= 18, "");
93 static_assert(numeric_limits<int>::digits >= 20, "");
95 const int __yr = static_cast<int>(__y_) - (__m_ <= February);
96 const unsigned __mth = static_cast<unsigned>(__m_);
97 const unsigned __dy = static_cast<unsigned>(__d_);
99 const int __era = (__yr >= 0 ? __yr : __yr - 399) / 400;
100 const unsigned __yoe = static_cast<unsigned>(__yr - __era * 400); // [0, 399]
101 const unsigned __doy = (153 * (__mth + (__mth > 2 ? -3 : 9)) + 2) / 5 + __dy - 1; // [0, 365]
102 const unsigned __doe = __yoe * 365 + __yoe / 4 - __yoe / 100 + __doy; // [0, 146096]
103 return days{__era * 146097 + static_cast<int>(__doe) - 719468};
106 _LIBCPP_HIDE_FROM_ABI inline constexpr bool
107 operator==(const year_month_day& __lhs, const year_month_day& __rhs) noexcept {
108 return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day();
111 _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
112 operator<=>(const year_month_day& __lhs, const year_month_day& __rhs) noexcept {
113 if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
114 return __c;
115 if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
116 return __c;
117 return __lhs.day() <=> __rhs.day();
120 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, const day& __rhs) noexcept {
121 return year_month_day{__lhs.year(), __lhs.month(), __rhs};
124 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, int __rhs) noexcept {
125 return __lhs / day(__rhs);
128 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year& __lhs, const month_day& __rhs) noexcept {
129 return __lhs / __rhs.month() / __rhs.day();
132 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(int __lhs, const month_day& __rhs) noexcept {
133 return year(__lhs) / __rhs;
136 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, const year& __rhs) noexcept {
137 return __rhs / __lhs;
140 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, int __rhs) noexcept {
141 return year(__rhs) / __lhs;
144 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
145 operator+(const year_month_day& __lhs, const months& __rhs) noexcept {
146 return (__lhs.year() / __lhs.month() + __rhs) / __lhs.day();
149 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
150 operator+(const months& __lhs, const year_month_day& __rhs) noexcept {
151 return __rhs + __lhs;
154 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
155 operator-(const year_month_day& __lhs, const months& __rhs) noexcept {
156 return __lhs + -__rhs;
159 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
160 operator+(const year_month_day& __lhs, const years& __rhs) noexcept {
161 return (__lhs.year() + __rhs) / __lhs.month() / __lhs.day();
164 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
165 operator+(const years& __lhs, const year_month_day& __rhs) noexcept {
166 return __rhs + __lhs;
169 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
170 operator-(const year_month_day& __lhs, const years& __rhs) noexcept {
171 return __lhs + -__rhs;
174 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const months& __dm) noexcept {
175 *this = *this + __dm;
176 return *this;
178 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const months& __dm) noexcept {
179 *this = *this - __dm;
180 return *this;
182 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const years& __dy) noexcept {
183 *this = *this + __dy;
184 return *this;
186 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const years& __dy) noexcept {
187 *this = *this - __dy;
188 return *this;
191 class year_month_day_last {
192 private:
193 chrono::year __y_;
194 chrono::month_day_last __mdl_;
196 public:
197 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last(const year& __yval, const month_day_last& __mdlval) noexcept
198 : __y_{__yval}, __mdl_{__mdlval} {}
200 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const months& __m) noexcept;
201 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const months& __m) noexcept;
202 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const years& __y) noexcept;
203 _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const years& __y) noexcept;
205 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
206 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __mdl_.month(); }
207 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month_day_last month_day_last() const noexcept { return __mdl_; }
208 _LIBCPP_HIDE_FROM_ABI constexpr chrono::day day() const noexcept;
209 _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept {
210 return sys_days{year() / month() / day()};
212 _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
213 return local_days{year() / month() / day()};
215 _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __mdl_.ok(); }
218 _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day year_month_day_last::day() const noexcept {
219 constexpr chrono::day __d[] = {
220 chrono::day(31),
221 chrono::day(28),
222 chrono::day(31),
223 chrono::day(30),
224 chrono::day(31),
225 chrono::day(30),
226 chrono::day(31),
227 chrono::day(31),
228 chrono::day(30),
229 chrono::day(31),
230 chrono::day(30),
231 chrono::day(31)};
232 return (month() != February || !__y_.is_leap()) && month().ok()
233 ? __d[static_cast<unsigned>(month()) - 1]
234 : chrono::day{29};
237 _LIBCPP_HIDE_FROM_ABI inline constexpr bool
238 operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
239 return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last();
242 _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
243 operator<=>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
244 if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
245 return __c;
246 return __lhs.month_day_last() <=> __rhs.month_day_last();
249 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept {
250 return year_month_day_last{__lhs.year(), month_day_last{__lhs.month()}};
253 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
254 operator/(const year& __lhs, const month_day_last& __rhs) noexcept {
255 return year_month_day_last{__lhs, __rhs};
258 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(int __lhs, const month_day_last& __rhs) noexcept {
259 return year_month_day_last{year{__lhs}, __rhs};
262 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
263 operator/(const month_day_last& __lhs, const year& __rhs) noexcept {
264 return __rhs / __lhs;
267 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const month_day_last& __lhs, int __rhs) noexcept {
268 return year{__rhs} / __lhs;
271 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
272 operator+(const year_month_day_last& __lhs, const months& __rhs) noexcept {
273 return (__lhs.year() / __lhs.month() + __rhs) / last;
276 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
277 operator+(const months& __lhs, const year_month_day_last& __rhs) noexcept {
278 return __rhs + __lhs;
281 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
282 operator-(const year_month_day_last& __lhs, const months& __rhs) noexcept {
283 return __lhs + (-__rhs);
286 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
287 operator+(const year_month_day_last& __lhs, const years& __rhs) noexcept {
288 return year_month_day_last{__lhs.year() + __rhs, __lhs.month_day_last()};
291 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
292 operator+(const years& __lhs, const year_month_day_last& __rhs) noexcept {
293 return __rhs + __lhs;
296 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
297 operator-(const year_month_day_last& __lhs, const years& __rhs) noexcept {
298 return __lhs + (-__rhs);
301 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
302 year_month_day_last::operator+=(const months& __dm) noexcept {
303 *this = *this + __dm;
304 return *this;
306 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
307 year_month_day_last::operator-=(const months& __dm) noexcept {
308 *this = *this - __dm;
309 return *this;
311 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
312 year_month_day_last::operator+=(const years& __dy) noexcept {
313 *this = *this + __dy;
314 return *this;
316 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
317 year_month_day_last::operator-=(const years& __dy) noexcept {
318 *this = *this - __dy;
319 return *this;
322 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
323 : __y_{__ymdl.year()}, __m_{__ymdl.month()}, __d_{__ymdl.day()} {}
325 _LIBCPP_HIDE_FROM_ABI inline constexpr bool year_month_day::ok() const noexcept {
326 if (!__y_.ok() || !__m_.ok())
327 return false;
328 return chrono::day{1} <= __d_ && __d_ <= (__y_ / __m_ / last).day();
331 } // namespace chrono
333 _LIBCPP_END_NAMESPACE_STD
335 #endif // _LIBCPP_STD_VER >= 20
337 #endif // _LIBCPP___CHRONO_YEAR_MONTH_DAY_H