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