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 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
41 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
42 [[nodiscard]] _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[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day
74operator/(const month& __lhs, const day& __rhs) noexcept {
75 return month_day{__lhs, __rhs};
76}
77
78[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day
79operator/(const day& __lhs, const month& __rhs) noexcept {
80 return __rhs / __lhs;
81}
82
83[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(const month& __lhs, int __rhs) noexcept {
84 return __lhs / day(__rhs);
85}
86
87[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(int __lhs, const day& __rhs) noexcept {
88 return month(__lhs) / __rhs;
89}
90
91[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day operator/(const day& __lhs, int __rhs) noexcept {
92 return month(__rhs) / __lhs;
93}
94
95class month_day_last {
96private:
97 chrono::month __m_;
98
99public:
100 _LIBCPP_HIDE_FROM_ABI explicit constexpr month_day_last(const chrono::month& __val) noexcept : __m_{__val} {}
101 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
102 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_.ok(); }
103};
104
105_LIBCPP_HIDE_FROM_ABI inline constexpr bool
106operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
107 return __lhs.month() == __rhs.month();
108}
109
110_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
111operator<=>(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
112 return __lhs.month() <=> __rhs.month();
113}
114
115[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(const month& __lhs, last_spec) noexcept {
116 return month_day_last{__lhs};
117}
118
119[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(last_spec, const month& __rhs) noexcept {
120 return month_day_last{__rhs};
121}
122
123[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(int __lhs, last_spec) noexcept {
124 return month_day_last{month(__lhs)};
125}
126
127[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month_day_last operator/(last_spec, int __rhs) noexcept {
128 return month_day_last{month(__rhs)};
129}
130
131} // namespace chrono
132
133# if _LIBCPP_STD_VER >= 26
134
135template <>
136struct hash<chrono::month_day> {
137 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::month_day& __md) noexcept {
138 return std::__hash_combine(hash<chrono::month>{}(__md.month()), hash<chrono::day>{}(__md.day()));
139 }
140};
141
142template <>
143struct hash<chrono::month_day_last> {
144 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::month_day_last& __mdl) noexcept {
145 return hash<chrono::month>{}(__mdl.month());
146 }
147};
148
149# endif // _LIBCPP_STD_VER >= 26
150
151_LIBCPP_END_NAMESPACE_STD
152
153#endif // _LIBCPP_STD_VER >= 20
154
155#endif // _LIBCPP___CHRONO_MONTHDAY_H
156