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