month.h 3.7 KB

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