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 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 #if _LIBCPP_STD_VER > 17
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 namespace chrono
27 {
28 
29 class year_month {
30     chrono::year  __y;
31     chrono::month __m;
32 public:
33     _LIBCPP_HIDE_FROM_ABI year_month() = default;
34     _LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept
35         : __y{__yval}, __m{__mval} {}
36     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year  year()  const noexcept { return __y; }
37     _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m; }
38     _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept { this->__m += __dm; return *this; }
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 years& __dy)  noexcept { this->__y += __dy; 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 bool ok() const noexcept { return __y.ok() && __m.ok(); }
43 };
44 
45 _LIBCPP_HIDE_FROM_ABI inline constexpr
46 year_month operator/(const year& __y, const month& __m) noexcept { return year_month{__y, __m}; }
47 
48 _LIBCPP_HIDE_FROM_ABI inline constexpr
49 year_month operator/(const year& __y, int __m) noexcept { return year_month{__y, month(__m)}; }
50 
51 _LIBCPP_HIDE_FROM_ABI inline constexpr
52 bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept
53 { return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month(); }
54 
55 _LIBCPP_HIDE_FROM_ABI inline constexpr
56 bool operator!=(const year_month& __lhs, const year_month& __rhs) noexcept
57 { return !(__lhs == __rhs); }
58 
59 _LIBCPP_HIDE_FROM_ABI inline constexpr
60 bool operator< (const year_month& __lhs, const year_month& __rhs) noexcept
61 { return __lhs.year() != __rhs.year() ? __lhs.year() < __rhs.year() : __lhs.month() < __rhs.month(); }
62 
63 _LIBCPP_HIDE_FROM_ABI inline constexpr
64 bool operator> (const year_month& __lhs, const year_month& __rhs) noexcept
65 { return __rhs < __lhs; }
66 
67 _LIBCPP_HIDE_FROM_ABI inline constexpr
68 bool operator<=(const year_month& __lhs, const year_month& __rhs) noexcept
69 { return !(__rhs < __lhs);}
70 
71 _LIBCPP_HIDE_FROM_ABI inline constexpr
72 bool operator>=(const year_month& __lhs, const year_month& __rhs) noexcept
73 { return !(__lhs < __rhs); }
74 
75 _LIBCPP_HIDE_FROM_ABI constexpr
76 year_month operator+(const year_month& __lhs, const months& __rhs) noexcept
77 {
78     int __dmi = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();
79     const int __dy = (__dmi >= 0 ? __dmi : __dmi-11) / 12;
80     __dmi = __dmi - __dy * 12 + 1;
81     return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));
82 }
83 
84 _LIBCPP_HIDE_FROM_ABI constexpr
85 year_month operator+(const months& __lhs, const year_month& __rhs) noexcept
86 { return __rhs + __lhs; }
87 
88 _LIBCPP_HIDE_FROM_ABI constexpr
89 year_month operator+(const year_month& __lhs, const years& __rhs) noexcept
90 { return (__lhs.year() + __rhs) / __lhs.month(); }
91 
92 _LIBCPP_HIDE_FROM_ABI constexpr
93 year_month operator+(const years& __lhs, const year_month& __rhs) noexcept
94 { return __rhs + __lhs; }
95 
96 _LIBCPP_HIDE_FROM_ABI constexpr
97 months operator-(const year_month& __lhs, const year_month& __rhs) noexcept
98 { return (__lhs.year() - __rhs.year()) + months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month())); }
99 
100 _LIBCPP_HIDE_FROM_ABI constexpr
101 year_month operator-(const year_month& __lhs, const months& __rhs) noexcept
102 { return __lhs + -__rhs; }
103 
104 _LIBCPP_HIDE_FROM_ABI constexpr
105 year_month operator-(const year_month& __lhs, const years& __rhs) noexcept
106 { return __lhs + -__rhs; }
107 
108 } // namespace chrono
109 
110 _LIBCPP_END_NAMESPACE_STD
111 
112 #endif // _LIBCPP_STD_VER > 17
113 
114 #endif // _LIBCPP___CHRONO_YEAR_MONTH_H
115