year_month.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. {
  23. class year_month {
  24. chrono::year __y_;
  25. chrono::month __m_;
  26. public:
  27. year_month() = default;
  28. _LIBCPP_HIDE_FROM_ABI constexpr year_month(const chrono::year& __yval, const chrono::month& __mval) noexcept
  29. : __y_{__yval}, __m_{__mval} {}
  30. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
  31. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
  32. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const months& __dm) noexcept { this->__m_ += __dm; return *this; }
  33. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const months& __dm) noexcept { this->__m_ -= __dm; return *this; }
  34. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator+=(const years& __dy) noexcept { this->__y_ += __dy; return *this; }
  35. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month& operator-=(const years& __dy) noexcept { this->__y_ -= __dy; return *this; }
  36. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok(); }
  37. };
  38. _LIBCPP_HIDE_FROM_ABI inline constexpr
  39. year_month operator/(const year& __y, const month& __m) noexcept { return year_month{__y, __m}; }
  40. _LIBCPP_HIDE_FROM_ABI inline constexpr
  41. year_month operator/(const year& __y, int __m) noexcept { return year_month{__y, month(__m)}; }
  42. _LIBCPP_HIDE_FROM_ABI inline constexpr
  43. bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept
  44. { return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month(); }
  45. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const year_month& __lhs, const year_month& __rhs) noexcept {
  46. if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
  47. return __c;
  48. return __lhs.month() <=> __rhs.month();
  49. }
  50. _LIBCPP_HIDE_FROM_ABI constexpr
  51. year_month operator+(const year_month& __lhs, const months& __rhs) noexcept
  52. {
  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 constexpr
  59. year_month operator+(const months& __lhs, const year_month& __rhs) noexcept
  60. { return __rhs + __lhs; }
  61. _LIBCPP_HIDE_FROM_ABI constexpr
  62. year_month operator+(const year_month& __lhs, const years& __rhs) noexcept
  63. { return (__lhs.year() + __rhs) / __lhs.month(); }
  64. _LIBCPP_HIDE_FROM_ABI constexpr
  65. year_month operator+(const years& __lhs, const year_month& __rhs) noexcept
  66. { return __rhs + __lhs; }
  67. _LIBCPP_HIDE_FROM_ABI constexpr
  68. months operator-(const year_month& __lhs, const year_month& __rhs) noexcept
  69. { return (__lhs.year() - __rhs.year()) + months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month())); }
  70. _LIBCPP_HIDE_FROM_ABI constexpr
  71. year_month operator-(const year_month& __lhs, const months& __rhs) noexcept
  72. { return __lhs + -__rhs; }
  73. _LIBCPP_HIDE_FROM_ABI constexpr
  74. year_month operator-(const year_month& __lhs, const years& __rhs) noexcept
  75. { return __lhs + -__rhs; }
  76. } // namespace chrono
  77. _LIBCPP_END_NAMESPACE_STD
  78. #endif // _LIBCPP_STD_VER >= 20
  79. #endif // _LIBCPP___CHRONO_YEAR_MONTH_H