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_MONTH_H
11#define _LIBCPP___CHRONO_MONTH_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
19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20# pragma GCC system_header
21#endif
22
23#if _LIBCPP_STD_VER >= 20
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26
27namespace chrono {
28
29class month {
30private:
31 unsigned char __m_;
32
33public:
34 month() = default;
35 _LIBCPP_HIDE_FROM_ABI explicit inline constexpr month(unsigned __val) noexcept
36 : __m_(static_cast<unsigned char>(__val)) {}
37 _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator++() noexcept {
38 *this += months{1};
39 return *this;
40 }
41 _LIBCPP_HIDE_FROM_ABI inline constexpr month operator++(int) noexcept {
42 month __tmp = *this;
43 ++(*this);
44 return __tmp;
45 }
46 _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator--() noexcept {
47 *this -= months{1};
48 return *this;
49 }
50 _LIBCPP_HIDE_FROM_ABI inline constexpr month operator--(int) noexcept {
51 month __tmp = *this;
52 --(*this);
53 return __tmp;
54 }
55 _LIBCPP_HIDE_FROM_ABI constexpr month& operator+=(const months& __m1) noexcept;
56 _LIBCPP_HIDE_FROM_ABI constexpr month& operator-=(const months& __m1) noexcept;
57 _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator unsigned() const noexcept { return __m_; }
58 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_ >= 1 && __m_ <= 12; }
59};
60
61_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const month& __lhs, const month& __rhs) noexcept {
62 return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs);
63}
64
65_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(const month& __lhs, const month& __rhs) noexcept {
66 return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
67}
68
69[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month operator+(const month& __lhs, const months& __rhs) noexcept {
70 auto const __mu = static_cast<long long>(static_cast<unsigned>(__lhs)) + (__rhs.count() - 1);
71 auto const __yr = (__mu >= 0 ? __mu : __mu - 11) / 12;
72 return month{static_cast<unsigned>(__mu - __yr * 12 + 1)};
73}
74
75[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month operator+(const months& __lhs, const month& __rhs) noexcept {
76 return __rhs + __lhs;
77}
78
79[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr month operator-(const month& __lhs, const months& __rhs) noexcept {
80 return __lhs + -__rhs;
81}
82
83[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline constexpr months operator-(const month& __lhs, const month& __rhs) noexcept {
84 auto const __dm = static_cast<unsigned>(__lhs) - static_cast<unsigned>(__rhs);
85 return months(__dm <= 11 ? __dm : __dm + 12);
86}
87
88_LIBCPP_HIDE_FROM_ABI inline constexpr month& month::operator+=(const months& __dm) noexcept {
89 *this = *this + __dm;
90 return *this;
91}
92
93_LIBCPP_HIDE_FROM_ABI inline constexpr month& month::operator-=(const months& __dm) noexcept {
94 *this = *this - __dm;
95 return *this;
96}
97
98inline constexpr month January{1};
99inline constexpr month February{2};
100inline constexpr month March{3};
101inline constexpr month April{4};
102inline constexpr month May{5};
103inline constexpr month June{6};
104inline constexpr month July{7};
105inline constexpr month August{8};
106inline constexpr month September{9};
107inline constexpr month October{10};
108inline constexpr month November{11};
109inline constexpr month December{12};
110
111} // namespace chrono
112
113# if _LIBCPP_STD_VER >= 26
114
115template <>
116struct hash<chrono::month> {
117 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::month& __m) noexcept {
118 return static_cast<unsigned>(__m);
119 }
120};
121
122# endif // _LIBCPP_STD_VER >= 26
123
124_LIBCPP_END_NAMESPACE_STD
125
126#endif // _LIBCPP_STD_VER >= 20
127
128#endif // _LIBCPP___CHRONO_MONTH_H
129