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_H
11 #define _LIBCPP___CHRONO_YEAR_H
12 
13 #include <__chrono/duration.h>
14 #include <__config>
15 #include <limits>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 _LIBCPP_PUSH_MACROS
22 #include <__undef_macros>
23 
24 #if _LIBCPP_STD_VER > 17
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 namespace chrono
29 {
30 
31 class year {
32 private:
33     short __y;
34 public:
35     _LIBCPP_HIDE_FROM_ABI year() = default;
36     _LIBCPP_HIDE_FROM_ABI explicit inline constexpr year(int __val) noexcept : __y(static_cast<short>(__val)) {}
37 
38     _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator++()    noexcept { ++__y; return *this; }
39     _LIBCPP_HIDE_FROM_ABI inline constexpr year  operator++(int) noexcept { year __tmp = *this; ++(*this); return __tmp; }
40     _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator--()    noexcept { --__y; return *this; }
41     _LIBCPP_HIDE_FROM_ABI inline constexpr year  operator--(int) noexcept { year __tmp = *this; --(*this); return __tmp; }
42     _LIBCPP_HIDE_FROM_ABI        constexpr year& operator+=(const years& __dy) noexcept;
43     _LIBCPP_HIDE_FROM_ABI        constexpr year& operator-=(const years& __dy) noexcept;
44     _LIBCPP_HIDE_FROM_ABI inline constexpr year operator+() const noexcept { return *this; }
45     _LIBCPP_HIDE_FROM_ABI inline constexpr year operator-() const noexcept { return year{-__y}; }
46 
47     _LIBCPP_HIDE_FROM_ABI inline constexpr bool is_leap() const noexcept { return __y % 4 == 0 && (__y % 100 != 0 || __y % 400 == 0); }
48     _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator int() const noexcept { return __y; }
49     _LIBCPP_HIDE_FROM_ABI        constexpr bool ok() const noexcept;
50     _LIBCPP_HIDE_FROM_ABI static inline constexpr year min() noexcept { return year{-32767}; }
51     _LIBCPP_HIDE_FROM_ABI static inline constexpr year max() noexcept { return year{ 32767}; }
52 };
53 
54 
55 _LIBCPP_HIDE_FROM_ABI inline constexpr
56 bool operator==(const year& __lhs, const year& __rhs) noexcept
57 { return static_cast<int>(__lhs) == static_cast<int>(__rhs); }
58 
59 _LIBCPP_HIDE_FROM_ABI inline constexpr
60 bool operator!=(const year& __lhs, const year& __rhs) noexcept
61 { return !(__lhs == __rhs); }
62 
63 _LIBCPP_HIDE_FROM_ABI inline constexpr
64 bool operator< (const year& __lhs, const year& __rhs) noexcept
65 { return static_cast<int>(__lhs)  < static_cast<int>(__rhs); }
66 
67 _LIBCPP_HIDE_FROM_ABI inline constexpr
68 bool operator> (const year& __lhs, const year& __rhs) noexcept
69 { return __rhs < __lhs; }
70 
71 _LIBCPP_HIDE_FROM_ABI inline constexpr
72 bool operator<=(const year& __lhs, const year& __rhs) noexcept
73 { return !(__rhs < __lhs); }
74 
75 _LIBCPP_HIDE_FROM_ABI inline constexpr
76 bool operator>=(const year& __lhs, const year& __rhs) noexcept
77 { return !(__lhs < __rhs); }
78 
79 _LIBCPP_HIDE_FROM_ABI inline constexpr
80 year operator+ (const year& __lhs, const years& __rhs) noexcept
81 { return year(static_cast<int>(__lhs) + __rhs.count()); }
82 
83 _LIBCPP_HIDE_FROM_ABI inline constexpr
84 year operator+ (const years& __lhs, const year& __rhs) noexcept
85 { return __rhs + __lhs; }
86 
87 _LIBCPP_HIDE_FROM_ABI inline constexpr
88 year operator- (const year& __lhs, const years& __rhs) noexcept
89 { return __lhs + -__rhs; }
90 
91 _LIBCPP_HIDE_FROM_ABI inline constexpr
92 years operator-(const year& __lhs, const year& __rhs) noexcept
93 { return years{static_cast<int>(__lhs) - static_cast<int>(__rhs)}; }
94 
95 
96 _LIBCPP_HIDE_FROM_ABI inline constexpr
97 year& year::operator+=(const years& __dy) noexcept
98 { *this = *this + __dy; return *this; }
99 
100 _LIBCPP_HIDE_FROM_ABI inline constexpr
101 year& year::operator-=(const years& __dy) noexcept
102 { *this = *this - __dy; return *this; }
103 
104 _LIBCPP_HIDE_FROM_ABI constexpr bool year::ok() const noexcept {
105   static_assert(static_cast<int>(std::numeric_limits<decltype(__y)>::max()) == static_cast<int>(max()));
106   return static_cast<int>(min()) <= __y;
107 }
108 
109 } // namespace chrono
110 
111 _LIBCPP_END_NAMESPACE_STD
112 
113 #endif // _LIBCPP_STD_VER > 17
114 
115 _LIBCPP_POP_MACROS
116 
117 #endif // _LIBCPP___CHRONO_YEAR_H
118