year_month.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_YEAR_MONTH_H
  10. #define _LIBCPP___CHRONO_YEAR_MONTH_H
  11. #include <__chrono/duration.h>
  12. #include <__chrono/month.h>
  13. #include <__chrono/year.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 year_month {
  23. chrono::year __y_;
  24. chrono::month __m_;
  25. public:
  26. year_month() = default;
  27. _LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept
  28. : __y_{__yval}, __m_{__mval} {}
  29. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
  30. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
  31. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept;
  32. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const months& __dm) noexcept;
  33. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const years& __dy) noexcept;
  34. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const years& __dy) noexcept;
  35. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok(); }
  36. };
  37. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, const month& __m) noexcept {
  38. return year_month{__y, __m};
  39. }
  40. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator/(const year& __y, int __m) noexcept {
  41. return year_month{__y, month(__m)};
  42. }
  43. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept {
  44. return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month();
  45. }
  46. _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
  47. operator<=>(const year_month& __lhs, const year_month& __rhs) noexcept {
  48. if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
  49. return __c;
  50. return __lhs.month() <=> __rhs.month();
  51. }
  52. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const months& __rhs) noexcept {
  53. int __dmi = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();
  54. const int __dy = (__dmi >= 0 ? __dmi : __dmi - 11) / 12;
  55. __dmi = __dmi - __dy * 12 + 1;
  56. return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));
  57. }
  58. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const months& __lhs, const year_month& __rhs) noexcept {
  59. return __rhs + __lhs;
  60. }
  61. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const year_month& __lhs, const years& __rhs) noexcept {
  62. return (__lhs.year() + __rhs) / __lhs.month();
  63. }
  64. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator+(const years& __lhs, const year_month& __rhs) noexcept {
  65. return __rhs + __lhs;
  66. }
  67. _LIBCPP_HIDE_FROM_ABI inline constexpr months operator-(const year_month& __lhs, const year_month& __rhs) noexcept {
  68. return (__lhs.year() - __rhs.year()) +
  69. months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month()));
  70. }
  71. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const months& __rhs) noexcept {
  72. return __lhs + -__rhs;
  73. }
  74. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month operator-(const year_month& __lhs, const years& __rhs) noexcept {
  75. return __lhs + -__rhs;
  76. }
  77. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const months& __dm) noexcept {
  78. *this = *this + __dm;
  79. return *this;
  80. }
  81. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const months& __dm) noexcept {
  82. *this = *this - __dm;
  83. return *this;
  84. }
  85. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator+=(const years& __dy) noexcept {
  86. *this = *this + __dy;
  87. return *this;
  88. }
  89. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& year_month::operator-=(const years& __dy) noexcept {
  90. *this = *this - __dy;
  91. return *this;
  92. }
  93. } // namespace chrono
  94. _LIBCPP_END_NAMESPACE_STD
  95. #endif // _LIBCPP_STD_VER >= 20
  96. #endif // _LIBCPP___CHRONO_YEAR_MONTH_H