xref: /openbsd-src/gnu/llvm/libcxx/include/__chrono/monthday.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert // -*- C++ -*-
2*4bdff4beSrobert //===----------------------------------------------------------------------===//
3*4bdff4beSrobert //
4*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
6*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4bdff4beSrobert //
8*4bdff4beSrobert //===----------------------------------------------------------------------===//
9*4bdff4beSrobert 
10*4bdff4beSrobert #ifndef _LIBCPP___CHRONO_MONTHDAY_H
11*4bdff4beSrobert #define _LIBCPP___CHRONO_MONTHDAY_H
12*4bdff4beSrobert 
13*4bdff4beSrobert #include <__chrono/calendar.h>
14*4bdff4beSrobert #include <__chrono/day.h>
15*4bdff4beSrobert #include <__chrono/month.h>
16*4bdff4beSrobert #include <__config>
17*4bdff4beSrobert #include <compare>
18*4bdff4beSrobert 
19*4bdff4beSrobert #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20*4bdff4beSrobert #  pragma GCC system_header
21*4bdff4beSrobert #endif
22*4bdff4beSrobert 
23*4bdff4beSrobert #if _LIBCPP_STD_VER > 17
24*4bdff4beSrobert 
25*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
26*4bdff4beSrobert 
27*4bdff4beSrobert namespace chrono
28*4bdff4beSrobert {
29*4bdff4beSrobert 
30*4bdff4beSrobert class month_day {
31*4bdff4beSrobert private:
32*4bdff4beSrobert    chrono::month __m_;
33*4bdff4beSrobert    chrono::day   __d_;
34*4bdff4beSrobert public:
35*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI month_day() = default;
month_day(const chrono::month & __mval,const chrono::day & __dval)36*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr month_day(const chrono::month& __mval, const chrono::day& __dval) noexcept
37*4bdff4beSrobert         : __m_{__mval}, __d_{__dval} {}
month()38*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
day()39*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day   day()   const noexcept { return __d_; }
40*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
41*4bdff4beSrobert };
42*4bdff4beSrobert 
43*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
ok()44*4bdff4beSrobert bool month_day::ok() const noexcept
45*4bdff4beSrobert {
46*4bdff4beSrobert     if (!__m_.ok()) return false;
47*4bdff4beSrobert     const unsigned __dval = static_cast<unsigned>(__d_);
48*4bdff4beSrobert     if (__dval < 1 || __dval > 31) return false;
49*4bdff4beSrobert     if (__dval <= 29) return true;
50*4bdff4beSrobert //  Now we've got either 30 or 31
51*4bdff4beSrobert     const unsigned __mval = static_cast<unsigned>(__m_);
52*4bdff4beSrobert     if (__mval == 2) return false;
53*4bdff4beSrobert     if (__mval == 4 || __mval == 6 || __mval == 9 || __mval == 11)
54*4bdff4beSrobert         return __dval == 30;
55*4bdff4beSrobert     return true;
56*4bdff4beSrobert }
57*4bdff4beSrobert 
58*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
59*4bdff4beSrobert bool operator==(const month_day& __lhs, const month_day& __rhs) noexcept
60*4bdff4beSrobert { return __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); }
61*4bdff4beSrobert 
62*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const month_day& __lhs, const month_day& __rhs) noexcept {
63*4bdff4beSrobert     if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
64*4bdff4beSrobert         return __c;
65*4bdff4beSrobert     return __lhs.day() <=> __rhs.day();
66*4bdff4beSrobert }
67*4bdff4beSrobert 
68*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
69*4bdff4beSrobert month_day operator/(const month& __lhs, const day& __rhs) noexcept
70*4bdff4beSrobert { return month_day{__lhs, __rhs}; }
71*4bdff4beSrobert 
72*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr
73*4bdff4beSrobert month_day operator/(const day& __lhs, const month& __rhs) noexcept
74*4bdff4beSrobert { return __rhs / __lhs; }
75*4bdff4beSrobert 
76*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
77*4bdff4beSrobert month_day operator/(const month& __lhs, int __rhs) noexcept
78*4bdff4beSrobert { return __lhs / day(__rhs); }
79*4bdff4beSrobert 
80*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr
81*4bdff4beSrobert month_day operator/(int __lhs, const day& __rhs) noexcept
82*4bdff4beSrobert { return month(__lhs) / __rhs; }
83*4bdff4beSrobert 
84*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr
85*4bdff4beSrobert month_day operator/(const day& __lhs, int __rhs) noexcept
86*4bdff4beSrobert { return month(__rhs) / __lhs; }
87*4bdff4beSrobert 
88*4bdff4beSrobert class month_day_last {
89*4bdff4beSrobert private:
90*4bdff4beSrobert     chrono::month __m_;
91*4bdff4beSrobert public:
month_day_last(const chrono::month & __val)92*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI explicit constexpr month_day_last(const chrono::month& __val) noexcept
93*4bdff4beSrobert         : __m_{__val} {}
month()94*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
ok()95*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_.ok(); }
96*4bdff4beSrobert };
97*4bdff4beSrobert 
98*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
99*4bdff4beSrobert bool operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
100*4bdff4beSrobert { return __lhs.month() == __rhs.month(); }
101*4bdff4beSrobert 
102*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering
103*4bdff4beSrobert operator<=>(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
104*4bdff4beSrobert     return __lhs.month() <=> __rhs.month();
105*4bdff4beSrobert }
106*4bdff4beSrobert 
107*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
108*4bdff4beSrobert month_day_last operator/(const month& __lhs, last_spec) noexcept
109*4bdff4beSrobert { return month_day_last{__lhs}; }
110*4bdff4beSrobert 
111*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
112*4bdff4beSrobert month_day_last operator/(last_spec, const month& __rhs) noexcept
113*4bdff4beSrobert { return month_day_last{__rhs}; }
114*4bdff4beSrobert 
115*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
116*4bdff4beSrobert month_day_last operator/(int __lhs, last_spec) noexcept
117*4bdff4beSrobert { return month_day_last{month(__lhs)}; }
118*4bdff4beSrobert 
119*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
120*4bdff4beSrobert month_day_last operator/(last_spec, int __rhs) noexcept
121*4bdff4beSrobert { return month_day_last{month(__rhs)}; }
122*4bdff4beSrobert 
123*4bdff4beSrobert } // namespace chrono
124*4bdff4beSrobert 
125*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
126*4bdff4beSrobert 
127*4bdff4beSrobert #endif // _LIBCPP_STD_VER > 17
128*4bdff4beSrobert 
129*4bdff4beSrobert #endif // _LIBCPP___CHRONO_MONTHDAY_H
130