month.h 3.7 KB

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