2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHRONO_YEAR_MONTH_H
11 #define _LIBCPP___CHRONO_YEAR_MONTH_H
13 #include <__chrono/duration.h>
14 #include <__chrono/month.h>
15 #include <__chrono/year.h>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
23 #if _LIBCPP_STD_VER >= 20
25 _LIBCPP_BEGIN_NAMESPACE_STD
34 year_month() = default;
35 _LIBCPP_HIDE_FROM_ABI
constexpr year_month(const chrono::year
& __yval
, const chrono::month
& __mval
) noexcept
36 : __y_
{__yval
}, __m_
{__mval
} {}
37 _LIBCPP_HIDE_FROM_ABI
inline constexpr chrono::year
year() const noexcept
{ return __y_
; }
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
{ this->__m_
+= __dm
; return *this; }
40 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month
& operator-=(const months
& __dm
) noexcept
{ this->__m_
-= __dm
; return *this; }
41 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month
& operator+=(const years
& __dy
) noexcept
{ this->__y_
+= __dy
; return *this; }
42 _LIBCPP_HIDE_FROM_ABI
inline constexpr year_month
& operator-=(const years
& __dy
) noexcept
{ this->__y_
-= __dy
; return *this; }
43 _LIBCPP_HIDE_FROM_ABI
inline constexpr bool ok() const noexcept
{ return __y_
.ok() && __m_
.ok(); }
46 _LIBCPP_HIDE_FROM_ABI
inline constexpr
47 year_month
operator/(const year
& __y
, const month
& __m
) noexcept
{ return year_month
{__y
, __m
}; }
49 _LIBCPP_HIDE_FROM_ABI
inline constexpr
50 year_month
operator/(const year
& __y
, int __m
) noexcept
{ return year_month
{__y
, month(__m
)}; }
52 _LIBCPP_HIDE_FROM_ABI
inline constexpr
53 bool operator==(const year_month
& __lhs
, const year_month
& __rhs
) noexcept
54 { return __lhs
.year() == __rhs
.year() && __lhs
.month() == __rhs
.month(); }
56 _LIBCPP_HIDE_FROM_ABI
constexpr strong_ordering
operator<=>(const year_month
& __lhs
, const year_month
& __rhs
) noexcept
{
57 if (auto __c
= __lhs
.year() <=> __rhs
.year(); __c
!= 0)
59 return __lhs
.month() <=> __rhs
.month();
62 _LIBCPP_HIDE_FROM_ABI
constexpr
63 year_month
operator+(const year_month
& __lhs
, const months
& __rhs
) noexcept
65 int __dmi
= static_cast<int>(static_cast<unsigned>(__lhs
.month())) - 1 + __rhs
.count();
66 const int __dy
= (__dmi
>= 0 ? __dmi
: __dmi
-11) / 12;
67 __dmi
= __dmi
- __dy
* 12 + 1;
68 return (__lhs
.year() + years(__dy
)) / month(static_cast<unsigned>(__dmi
));
71 _LIBCPP_HIDE_FROM_ABI
constexpr
72 year_month
operator+(const months
& __lhs
, const year_month
& __rhs
) noexcept
73 { return __rhs
+ __lhs
; }
75 _LIBCPP_HIDE_FROM_ABI
constexpr
76 year_month
operator+(const year_month
& __lhs
, const years
& __rhs
) noexcept
77 { return (__lhs
.year() + __rhs
) / __lhs
.month(); }
79 _LIBCPP_HIDE_FROM_ABI
constexpr
80 year_month
operator+(const years
& __lhs
, const year_month
& __rhs
) noexcept
81 { return __rhs
+ __lhs
; }
83 _LIBCPP_HIDE_FROM_ABI
constexpr
84 months
operator-(const year_month
& __lhs
, const year_month
& __rhs
) noexcept
85 { return (__lhs
.year() - __rhs
.year()) + months(static_cast<unsigned>(__lhs
.month()) - static_cast<unsigned>(__rhs
.month())); }
87 _LIBCPP_HIDE_FROM_ABI
constexpr
88 year_month
operator-(const year_month
& __lhs
, const months
& __rhs
) noexcept
89 { return __lhs
+ -__rhs
; }
91 _LIBCPP_HIDE_FROM_ABI
constexpr
92 year_month
operator-(const year_month
& __lhs
, const years
& __rhs
) noexcept
93 { return __lhs
+ -__rhs
; }
97 _LIBCPP_END_NAMESPACE_STD
99 #endif // _LIBCPP_STD_VER >= 20
101 #endif // _LIBCPP___CHRONO_YEAR_MONTH_H