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