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