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