monthday.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_MONTHDAY_H
  10. #define _LIBCPP___CHRONO_MONTHDAY_H
  11. #include <__chrono/calendar.h>
  12. #include <__chrono/day.h>
  13. #include <__chrono/month.h>
  14. #include <__config>
  15. #include <compare>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. #if _LIBCPP_STD_VER > 17
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. namespace chrono
  22. {
  23. class month_day {
  24. private:
  25. chrono::month __m_;
  26. chrono::day __d_;
  27. public:
  28. _LIBCPP_HIDE_FROM_ABI month_day() = default;
  29. _LIBCPP_HIDE_FROM_ABI constexpr month_day(const chrono::month& __mval, const chrono::day& __dval) noexcept
  30. : __m_{__mval}, __d_{__dval} {}
  31. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
  32. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
  33. _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
  34. };
  35. _LIBCPP_HIDE_FROM_ABI inline constexpr
  36. bool month_day::ok() const noexcept
  37. {
  38. if (!__m_.ok()) return false;
  39. const unsigned __dval = static_cast<unsigned>(__d_);
  40. if (__dval < 1 || __dval > 31) return false;
  41. if (__dval <= 29) return true;
  42. // Now we've got either 30 or 31
  43. const unsigned __mval = static_cast<unsigned>(__m_);
  44. if (__mval == 2) return false;
  45. if (__mval == 4 || __mval == 6 || __mval == 9 || __mval == 11)
  46. return __dval == 30;
  47. return true;
  48. }
  49. _LIBCPP_HIDE_FROM_ABI inline constexpr
  50. bool operator==(const month_day& __lhs, const month_day& __rhs) noexcept
  51. { return __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); }
  52. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const month_day& __lhs, const month_day& __rhs) noexcept {
  53. if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
  54. return __c;
  55. return __lhs.day() <=> __rhs.day();
  56. }
  57. _LIBCPP_HIDE_FROM_ABI inline constexpr
  58. month_day operator/(const month& __lhs, const day& __rhs) noexcept
  59. { return month_day{__lhs, __rhs}; }
  60. _LIBCPP_HIDE_FROM_ABI constexpr
  61. month_day operator/(const day& __lhs, const month& __rhs) noexcept
  62. { return __rhs / __lhs; }
  63. _LIBCPP_HIDE_FROM_ABI inline constexpr
  64. month_day operator/(const month& __lhs, int __rhs) noexcept
  65. { return __lhs / day(__rhs); }
  66. _LIBCPP_HIDE_FROM_ABI constexpr
  67. month_day operator/(int __lhs, const day& __rhs) noexcept
  68. { return month(__lhs) / __rhs; }
  69. _LIBCPP_HIDE_FROM_ABI constexpr
  70. month_day operator/(const day& __lhs, int __rhs) noexcept
  71. { return month(__rhs) / __lhs; }
  72. class month_day_last {
  73. private:
  74. chrono::month __m_;
  75. public:
  76. _LIBCPP_HIDE_FROM_ABI explicit constexpr month_day_last(const chrono::month& __val) noexcept
  77. : __m_{__val} {}
  78. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
  79. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __m_.ok(); }
  80. };
  81. _LIBCPP_HIDE_FROM_ABI inline constexpr
  82. bool operator==(const month_day_last& __lhs, const month_day_last& __rhs) noexcept
  83. { return __lhs.month() == __rhs.month(); }
  84. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering
  85. operator<=>(const month_day_last& __lhs, const month_day_last& __rhs) noexcept {
  86. return __lhs.month() <=> __rhs.month();
  87. }
  88. _LIBCPP_HIDE_FROM_ABI inline constexpr
  89. month_day_last operator/(const month& __lhs, last_spec) noexcept
  90. { return month_day_last{__lhs}; }
  91. _LIBCPP_HIDE_FROM_ABI inline constexpr
  92. month_day_last operator/(last_spec, const month& __rhs) noexcept
  93. { return month_day_last{__rhs}; }
  94. _LIBCPP_HIDE_FROM_ABI inline constexpr
  95. month_day_last operator/(int __lhs, last_spec) noexcept
  96. { return month_day_last{month(__lhs)}; }
  97. _LIBCPP_HIDE_FROM_ABI inline constexpr
  98. month_day_last operator/(last_spec, int __rhs) noexcept
  99. { return month_day_last{month(__rhs)}; }
  100. } // namespace chrono
  101. _LIBCPP_END_NAMESPACE_STD
  102. #endif // _LIBCPP_STD_VER > 17
  103. #endif // _LIBCPP___CHRONO_MONTHDAY_H