xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/monthday.h (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
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 #include <compare>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 #if _LIBCPP_STD_VER > 17
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 namespace chrono
28 {
29 
30 class month_day {
31 private:
32    chrono::month __m_;
33    chrono::day   __d_;
34 public:
35     _LIBCPP_HIDE_FROM_ABI month_day() = default;
36     _LIBCPP_HIDE_FROM_ABI constexpr month_day(const chrono::month& __mval, const chrono::day& __dval) noexcept
37         : __m_{__mval}, __d_{__dval} {}
38     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
39     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day   day()   const noexcept { return __d_; }
40     _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
41 };
42 
43 _LIBCPP_HIDE_FROM_ABI inline constexpr
44 bool month_day::ok() const noexcept
45 {
46     if (!__m_.ok()) return false;
47     const unsigned __dval = static_cast<unsigned>(__d_);
48     if (__dval < 1 || __dval > 31) return false;
49     if (__dval <= 29) return true;
50 //  Now we've got either 30 or 31
51     const unsigned __mval = static_cast<unsigned>(__m_);
52     if (__mval == 2) return false;
53     if (__mval == 4 || __mval == 6 || __mval == 9 || __mval == 11)
54         return __dval == 30;
55     return true;
56 }
57 
58 _LIBCPP_HIDE_FROM_ABI inline constexpr
59 bool operator==(const month_day& __lhs, const month_day& __rhs) noexcept
60 { return __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); }
61 
62 _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const month_day& __lhs, const month_day& __rhs) noexcept {
63     if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
64         return __c;
65     return __lhs.day() <=> __rhs.day();
66 }
67 
68 _LIBCPP_HIDE_FROM_ABI inline constexpr
69 month_day operator/(const month& __lhs, const day& __rhs) noexcept
70 { return month_day{__lhs, __rhs}; }
71 
72 _LIBCPP_HIDE_FROM_ABI constexpr
73 month_day operator/(const day& __lhs, const month& __rhs) noexcept
74 { return __rhs / __lhs; }
75 
76 _LIBCPP_HIDE_FROM_ABI inline constexpr
77 month_day operator/(const month& __lhs, int __rhs) noexcept
78 { return __lhs / day(__rhs); }
79 
80 _LIBCPP_HIDE_FROM_ABI constexpr
81 month_day operator/(int __lhs, const day& __rhs) noexcept
82 { return month(__lhs) / __rhs; }
83 
84 _LIBCPP_HIDE_FROM_ABI constexpr
85 month_day operator/(const day& __lhs, int __rhs) noexcept
86 { return month(__rhs) / __lhs; }
87 
88 class month_day_last {
89 private:
90     chrono::month __m_;
91 public:
92     _LIBCPP_HIDE_FROM_ABI explicit constexpr month_day_last(const chrono::month& __val) noexcept
93         : __m_{__val} {}
94     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
95     _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_.ok(); }
96 };
97 
98 _LIBCPP_HIDE_FROM_ABI inline constexpr
99 bool operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
100 { return __lhs.month() == __rhs.month(); }
101 
102 _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering
103 operator<=>(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
104     return __lhs.month() <=> __rhs.month();
105 }
106 
107 _LIBCPP_HIDE_FROM_ABI inline constexpr
108 month_day_last operator/(const month& __lhs, last_spec) noexcept
109 { return month_day_last{__lhs}; }
110 
111 _LIBCPP_HIDE_FROM_ABI inline constexpr
112 month_day_last operator/(last_spec, const month& __rhs) noexcept
113 { return month_day_last{__rhs}; }
114 
115 _LIBCPP_HIDE_FROM_ABI inline constexpr
116 month_day_last operator/(int __lhs, last_spec) noexcept
117 { return month_day_last{month(__lhs)}; }
118 
119 _LIBCPP_HIDE_FROM_ABI inline constexpr
120 month_day_last operator/(last_spec, int __rhs) noexcept
121 { return month_day_last{month(__rhs)}; }
122 
123 } // namespace chrono
124 
125 _LIBCPP_END_NAMESPACE_STD
126 
127 #endif // _LIBCPP_STD_VER > 17
128 
129 #endif // _LIBCPP___CHRONO_MONTHDAY_H
130