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_YEAR_MONTH_H
11#define _LIBCPP___CHRONO_YEAR_MONTH_H
12
13#include <__chrono/duration.h>
14#include <__chrono/month.h>
15#include <__chrono/year.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 year_month {
32 chrono::year __y_;
33 chrono::month __m_;
34
35public:
36 year_month() = default;
37 _LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept
38 : __y_{__yval}, __m_{__mval} {}
39 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
40 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
41 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept;
42 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const months& __dm) noexcept;
43 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const years& __dy) noexcept;
44 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const years& __dy) noexcept;
45 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok(); }
46};
47
48[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, const month& __m) noexcept {
49 return year_month{__y, __m};
50}
51
52[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, int __m) noexcept {
53 return year_month{__y, month(__m)};
54}
55
56_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept {
57 return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month();
58}
59
60_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
61operator<=>(const year_month& __lhs, const year_month& __rhs) noexcept {
62 if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
63 return __c;
64 return __lhs.month() <=> __rhs.month();
65}
66
67[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month
68operator+(const year_month& __lhs, const months& __rhs) noexcept {
69 int __dmi = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();
70 const int __dy = (__dmi >= 0 ? __dmi : __dmi - 11) / 12;
71 __dmi = __dmi - __dy * 12 + 1;
72 return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));
73}
74
75[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month
76operator+(const months& __lhs, const year_month& __rhs) noexcept {
77 return __rhs + __lhs;
78}
79
80[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month
81operator+(const year_month& __lhs, const years& __rhs) noexcept {
82 return (__lhs.year() + __rhs) / __lhs.month();
83}
84
85[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month
86operator+(const years& __lhs, const year_month& __rhs) noexcept {
87 return __rhs + __lhs;
88}
89
90[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr months
91operator-(const year_month& __lhs, const year_month& __rhs) noexcept {
92 return (__lhs.year() - __rhs.year()) +
93 months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month()));
94}
95
96[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month
97operator-(const year_month& __lhs, const months& __rhs) noexcept {
98 return __lhs + -__rhs;
99}
100
101[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr year_month
102operator-(const year_month& __lhs, const years& __rhs) noexcept {
103 return __lhs + -__rhs;
104}
105
106_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const months& __dm) noexcept {
107 *this = *this + __dm;
108 return *this;
109}
110
111_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const months& __dm) noexcept {
112 *this = *this - __dm;
113 return *this;
114}
115
116_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const years& __dy) noexcept {
117 *this = *this + __dy;
118 return *this;
119}
120
121_LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const years& __dy) noexcept {
122 *this = *this - __dy;
123 return *this;
124}
125
126} // namespace chrono
127
128# if _LIBCPP_STD_VER >= 26
129
130template <>
131struct hash<chrono::year_month> {
132 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::year_month& __ym) noexcept {
133 return std::__hash_combine(hash<chrono::year>{}(__ym.year()), hash<chrono::month>{}(__ym.month()));
134 }
135};
136
137# endif // _LIBCPP_STD_VER >= 26
138
139_LIBCPP_END_NAMESPACE_STD
140
141#endif // _LIBCPP_STD_VER >= 20
142
143#endif // _LIBCPP___CHRONO_YEAR_MONTH_H
144