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