xref: /openbsd-src/gnu/llvm/libcxx/include/__chrono/month.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert // -*- C++ -*-
2*4bdff4beSrobert //===----------------------------------------------------------------------===//
3*4bdff4beSrobert //
4*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
6*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4bdff4beSrobert //
8*4bdff4beSrobert //===----------------------------------------------------------------------===//
9*4bdff4beSrobert 
10*4bdff4beSrobert #ifndef _LIBCPP___CHRONO_MONTH_H
11*4bdff4beSrobert #define _LIBCPP___CHRONO_MONTH_H
12*4bdff4beSrobert 
13*4bdff4beSrobert #include <__chrono/duration.h>
14*4bdff4beSrobert #include <__config>
15*4bdff4beSrobert #include <compare>
16*4bdff4beSrobert 
17*4bdff4beSrobert #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18*4bdff4beSrobert #  pragma GCC system_header
19*4bdff4beSrobert #endif
20*4bdff4beSrobert 
21*4bdff4beSrobert #if _LIBCPP_STD_VER > 17
22*4bdff4beSrobert 
23*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
24*4bdff4beSrobert 
25*4bdff4beSrobert namespace chrono
26*4bdff4beSrobert {
27*4bdff4beSrobert 
28*4bdff4beSrobert class month {
29*4bdff4beSrobert private:
30*4bdff4beSrobert     unsigned char __m_;
31*4bdff4beSrobert public:
32*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI month() = default;
month(unsigned __val)33*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI explicit inline constexpr month(unsigned __val) noexcept : __m_(static_cast<unsigned char>(__val)) {}
34*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator++()    noexcept { ++__m_; return *this; }
35*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr month  operator++(int) noexcept { month __tmp = *this; ++(*this); return __tmp; }
36*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr month& operator--()    noexcept { --__m_; return *this; }
37*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr month  operator--(int) noexcept { month __tmp = *this; --(*this); return __tmp; }
38*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI        constexpr month& operator+=(const months& __m1) noexcept;
39*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI        constexpr month& operator-=(const months& __m1) noexcept;
40*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator unsigned() const noexcept { return __m_; }
ok()41*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_ >= 1 && __m_ <= 12; }
42*4bdff4beSrobert };
43*4bdff4beSrobert 
44*4bdff4beSrobert 
45*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
46*4bdff4beSrobert bool operator==(const month& __lhs, const month& __rhs) noexcept
47*4bdff4beSrobert { return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs); }
48*4bdff4beSrobert 
49*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const month& __lhs, const month& __rhs) noexcept {
50*4bdff4beSrobert     return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
51*4bdff4beSrobert }
52*4bdff4beSrobert 
53*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
54*4bdff4beSrobert month operator+ (const month& __lhs, const months& __rhs) noexcept
55*4bdff4beSrobert {
56*4bdff4beSrobert     auto const __mu = static_cast<long long>(static_cast<unsigned>(__lhs)) + (__rhs.count() - 1);
57*4bdff4beSrobert     auto const __yr = (__mu >= 0 ? __mu : __mu - 11) / 12;
58*4bdff4beSrobert     return month{static_cast<unsigned>(__mu - __yr * 12 + 1)};
59*4bdff4beSrobert }
60*4bdff4beSrobert 
61*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
62*4bdff4beSrobert month operator+ (const months& __lhs, const month& __rhs) noexcept
63*4bdff4beSrobert { return __rhs + __lhs; }
64*4bdff4beSrobert 
65*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
66*4bdff4beSrobert month operator- (const month& __lhs, const months& __rhs) noexcept
67*4bdff4beSrobert { return __lhs + -__rhs; }
68*4bdff4beSrobert 
69*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
70*4bdff4beSrobert months operator-(const month& __lhs, const month& __rhs) noexcept
71*4bdff4beSrobert {
72*4bdff4beSrobert     auto const __dm = static_cast<unsigned>(__lhs) - static_cast<unsigned>(__rhs);
73*4bdff4beSrobert     return months(__dm <= 11 ? __dm : __dm + 12);
74*4bdff4beSrobert }
75*4bdff4beSrobert 
76*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
77*4bdff4beSrobert month& month::operator+=(const months& __dm) noexcept
78*4bdff4beSrobert { *this = *this + __dm; return *this; }
79*4bdff4beSrobert 
80*4bdff4beSrobert _LIBCPP_HIDE_FROM_ABI inline constexpr
81*4bdff4beSrobert month& month::operator-=(const months& __dm) noexcept
82*4bdff4beSrobert { *this = *this - __dm; return *this; }
83*4bdff4beSrobert 
84*4bdff4beSrobert inline constexpr month January{1};
85*4bdff4beSrobert inline constexpr month February{2};
86*4bdff4beSrobert inline constexpr month March{3};
87*4bdff4beSrobert inline constexpr month April{4};
88*4bdff4beSrobert inline constexpr month May{5};
89*4bdff4beSrobert inline constexpr month June{6};
90*4bdff4beSrobert inline constexpr month July{7};
91*4bdff4beSrobert inline constexpr month August{8};
92*4bdff4beSrobert inline constexpr month September{9};
93*4bdff4beSrobert inline constexpr month October{10};
94*4bdff4beSrobert inline constexpr month November{11};
95*4bdff4beSrobert inline constexpr month December{12};
96*4bdff4beSrobert 
97*4bdff4beSrobert } // namespace chrono
98*4bdff4beSrobert 
99*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
100*4bdff4beSrobert 
101*4bdff4beSrobert #endif // _LIBCPP_STD_VER > 17
102*4bdff4beSrobert 
103*4bdff4beSrobert #endif // _LIBCPP___CHRONO_MONTH_H
104