monthday.h 4.1 KB

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