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_MONTHDAY_H
11 #define _LIBCPP___CHRONO_MONTHDAY_H
12 
13 #include <__chrono/calendar.h>
14 #include <__chrono/day.h>
15 #include <__chrono/month.h>
16 #include <__config>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 #if _LIBCPP_STD_VER > 17
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 namespace chrono
27 {
28 
29 class month_day {
30 private:
31    chrono::month __m;
32    chrono::day   __d;
33 public:
34     _LIBCPP_HIDE_FROM_ABI month_day() = default;
35     _LIBCPP_HIDE_FROM_ABI constexpr month_day(const chrono::month& __mval, const chrono::day& __dval) noexcept
36         : __m{__mval}, __d{__dval} {}
37     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m; }
38     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day   day()   const noexcept { return __d; }
39     _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
40 };
41 
42 _LIBCPP_HIDE_FROM_ABI inline constexpr
43 bool month_day::ok() const noexcept
44 {
45     if (!__m.ok()) return false;
46     const unsigned __dval = static_cast<unsigned>(__d);
47     if (__dval < 1 || __dval > 31) return false;
48     if (__dval <= 29) return true;
49 //  Now we've got either 30 or 31
50     const unsigned __mval = static_cast<unsigned>(__m);
51     if (__mval == 2) return false;
52     if (__mval == 4 || __mval == 6 || __mval == 9 || __mval == 11)
53         return __dval == 30;
54     return true;
55 }
56 
57 _LIBCPP_HIDE_FROM_ABI inline constexpr
58 bool operator==(const month_day& __lhs, const month_day& __rhs) noexcept
59 { return __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); }
60 
61 _LIBCPP_HIDE_FROM_ABI inline constexpr
62 bool operator!=(const month_day& __lhs, const month_day& __rhs) noexcept
63 { return !(__lhs == __rhs); }
64 
65 _LIBCPP_HIDE_FROM_ABI inline constexpr
66 month_day operator/(const month& __lhs, const day& __rhs) noexcept
67 { return month_day{__lhs, __rhs}; }
68 
69 _LIBCPP_HIDE_FROM_ABI constexpr
70 month_day operator/(const day& __lhs, const month& __rhs) noexcept
71 { return __rhs / __lhs; }
72 
73 _LIBCPP_HIDE_FROM_ABI inline constexpr
74 month_day operator/(const month& __lhs, int __rhs) noexcept
75 { return __lhs / day(__rhs); }
76 
77 _LIBCPP_HIDE_FROM_ABI constexpr
78 month_day operator/(int __lhs, const day& __rhs) noexcept
79 { return month(__lhs) / __rhs; }
80 
81 _LIBCPP_HIDE_FROM_ABI constexpr
82 month_day operator/(const day& __lhs, int __rhs) noexcept
83 { return month(__rhs) / __lhs; }
84 
85 
86 _LIBCPP_HIDE_FROM_ABI inline constexpr
87 bool operator< (const month_day& __lhs, const month_day& __rhs) noexcept
88 { return __lhs.month() != __rhs.month() ? __lhs.month() < __rhs.month() : __lhs.day() < __rhs.day(); }
89 
90 _LIBCPP_HIDE_FROM_ABI inline constexpr
91 bool operator> (const month_day& __lhs, const month_day& __rhs) noexcept
92 { return __rhs < __lhs; }
93 
94 _LIBCPP_HIDE_FROM_ABI inline constexpr
95 bool operator<=(const month_day& __lhs, const month_day& __rhs) noexcept
96 { return !(__rhs < __lhs);}
97 
98 _LIBCPP_HIDE_FROM_ABI inline constexpr
99 bool operator>=(const month_day& __lhs, const month_day& __rhs) noexcept
100 { return !(__lhs < __rhs); }
101 
102 
103 
104 class month_day_last {
105 private:
106     chrono::month __m;
107 public:
108     _LIBCPP_HIDE_FROM_ABI explicit constexpr month_day_last(const chrono::month& __val) noexcept
109         : __m{__val} {}
110     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m; }
111     _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m.ok(); }
112 };
113 
114 _LIBCPP_HIDE_FROM_ABI inline constexpr
115 bool operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
116 { return __lhs.month() == __rhs.month(); }
117 
118 _LIBCPP_HIDE_FROM_ABI inline constexpr
119 bool operator!=(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
120 { return !(__lhs == __rhs); }
121 
122 _LIBCPP_HIDE_FROM_ABI inline constexpr
123 bool operator< (const month_day_last& __lhs, const month_day_last& __rhs) noexcept
124 { return __lhs.month() < __rhs.month(); }
125 
126 _LIBCPP_HIDE_FROM_ABI inline constexpr
127 bool operator> (const month_day_last& __lhs, const month_day_last& __rhs) noexcept
128 { return __rhs < __lhs; }
129 
130 _LIBCPP_HIDE_FROM_ABI inline constexpr
131 bool operator<=(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
132 { return !(__rhs < __lhs);}
133 
134 _LIBCPP_HIDE_FROM_ABI inline constexpr
135 bool operator>=(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
136 { return !(__lhs < __rhs); }
137 
138 _LIBCPP_HIDE_FROM_ABI inline constexpr
139 month_day_last operator/(const month& __lhs, last_spec) noexcept
140 { return month_day_last{__lhs}; }
141 
142 _LIBCPP_HIDE_FROM_ABI inline constexpr
143 month_day_last operator/(last_spec, const month& __rhs) noexcept
144 { return month_day_last{__rhs}; }
145 
146 _LIBCPP_HIDE_FROM_ABI inline constexpr
147 month_day_last operator/(int __lhs, last_spec) noexcept
148 { return month_day_last{month(__lhs)}; }
149 
150 _LIBCPP_HIDE_FROM_ABI inline constexpr
151 month_day_last operator/(last_spec, int __rhs) noexcept
152 { return month_day_last{month(__rhs)}; }
153 
154 } // namespace chrono
155 
156 _LIBCPP_END_NAMESPACE_STD
157 
158 #endif // _LIBCPP_STD_VER > 17
159 
160 #endif // _LIBCPP___CHRONO_MONTHDAY_H
161