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 <__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 year_month {
30   chrono::year __y_;
31   chrono::month __m_;
32 
33 public:
34   year_month() = default;
year_month(const chrono::year & __yval,const chrono::month & __mval)35   _LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept
36       : __y_{__yval}, __m_{__mval} {}
year()37   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
month()38   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
39   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept;
40   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const months& __dm) noexcept;
41   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const years& __dy) noexcept;
42   _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const years& __dy) noexcept;
ok()43   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok(); }
44 };
45 
46 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, const month& __m) noexcept {
47   return year_month{__y, __m};
48 }
49 
50 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, int __m) noexcept {
51   return year_month{__y, month(__m)};
52 }
53 
54 _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept {
55   return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month();
56 }
57 
58 _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
59 operator<=>(const year_month& __lhs, const year_month& __rhs) noexcept {
60   if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
61     return __c;
62   return __lhs.month() <=> __rhs.month();
63 }
64 
65 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const months& __rhs) noexcept {
66   int __dmi      = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();
67   const int __dy = (__dmi >= 0 ? __dmi : __dmi - 11) / 12;
68   __dmi          = __dmi - __dy * 12 + 1;
69   return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));
70 }
71 
72 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const months& __lhs, const year_month& __rhs) noexcept {
73   return __rhs + __lhs;
74 }
75 
76 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const years& __rhs) noexcept {
77   return (__lhs.year() + __rhs) / __lhs.month();
78 }
79 
80 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const years& __lhs, const year_month& __rhs) noexcept {
81   return __rhs + __lhs;
82 }
83 
84 _LIBCPP_HIDE_FROM_ABI inline constexpr months operator-(const year_month& __lhs, const year_month& __rhs) noexcept {
85   return (__lhs.year() - __rhs.year()) +
86          months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month()));
87 }
88 
89 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const months& __rhs) noexcept {
90   return __lhs + -__rhs;
91 }
92 
93 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const years& __rhs) noexcept {
94   return __lhs + -__rhs;
95 }
96 
97 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const months& __dm) noexcept {
98   *this = *this + __dm;
99   return *this;
100 }
101 
102 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const months& __dm) noexcept {
103   *this = *this - __dm;
104   return *this;
105 }
106 
107 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const years& __dy) noexcept {
108   *this = *this + __dy;
109   return *this;
110 }
111 
112 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const years& __dy) noexcept {
113   *this = *this - __dy;
114   return *this;
115 }
116 
117 } // namespace chrono
118 
119 _LIBCPP_END_NAMESPACE_STD
120 
121 #endif // _LIBCPP_STD_VER >= 20
122 
123 #endif // _LIBCPP___CHRONO_YEAR_MONTH_H
124