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