year_month.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. #if _LIBCPP_STD_VER > 17
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. namespace chrono
  21. {
  22. class year_month {
  23. chrono::year __y;
  24. chrono::month __m;
  25. public:
  26. _LIBCPP_HIDE_FROM_ABI 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 { this->__m += __dm; return *this; }
  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 years& __dy) noexcept { this->__y += __dy; 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 bool ok() const noexcept { return __y.ok() && __m.ok(); }
  36. };
  37. _LIBCPP_HIDE_FROM_ABI inline constexpr
  38. year_month operator/(const year& __y, const month& __m) noexcept { return year_month{__y, __m}; }
  39. _LIBCPP_HIDE_FROM_ABI inline constexpr
  40. year_month operator/(const year& __y, int __m) noexcept { return year_month{__y, month(__m)}; }
  41. _LIBCPP_HIDE_FROM_ABI inline constexpr
  42. bool operator==(const year_month& __lhs, const year_month& __rhs) noexcept
  43. { return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month(); }
  44. _LIBCPP_HIDE_FROM_ABI inline constexpr
  45. bool operator!=(const year_month& __lhs, const year_month& __rhs) noexcept
  46. { return !(__lhs == __rhs); }
  47. _LIBCPP_HIDE_FROM_ABI inline constexpr
  48. bool operator< (const year_month& __lhs, const year_month& __rhs) noexcept
  49. { return __lhs.year() != __rhs.year() ? __lhs.year() < __rhs.year() : __lhs.month() < __rhs.month(); }
  50. _LIBCPP_HIDE_FROM_ABI inline constexpr
  51. bool operator> (const year_month& __lhs, const year_month& __rhs) noexcept
  52. { return __rhs < __lhs; }
  53. _LIBCPP_HIDE_FROM_ABI inline constexpr
  54. bool operator<=(const year_month& __lhs, const year_month& __rhs) noexcept
  55. { return !(__rhs < __lhs);}
  56. _LIBCPP_HIDE_FROM_ABI inline constexpr
  57. bool operator>=(const year_month& __lhs, const year_month& __rhs) noexcept
  58. { return !(__lhs < __rhs); }
  59. _LIBCPP_HIDE_FROM_ABI constexpr
  60. year_month operator+(const year_month& __lhs, const months& __rhs) noexcept
  61. {
  62. int __dmi = static_cast<int>(static_cast<unsigned>(__lhs.month())) - 1 + __rhs.count();
  63. const int __dy = (__dmi >= 0 ? __dmi : __dmi-11) / 12;
  64. __dmi = __dmi - __dy * 12 + 1;
  65. return (__lhs.year() + years(__dy)) / month(static_cast<unsigned>(__dmi));
  66. }
  67. _LIBCPP_HIDE_FROM_ABI constexpr
  68. year_month operator+(const months& __lhs, const year_month& __rhs) noexcept
  69. { return __rhs + __lhs; }
  70. _LIBCPP_HIDE_FROM_ABI constexpr
  71. year_month operator+(const year_month& __lhs, const years& __rhs) noexcept
  72. { return (__lhs.year() + __rhs) / __lhs.month(); }
  73. _LIBCPP_HIDE_FROM_ABI constexpr
  74. year_month operator+(const years& __lhs, const year_month& __rhs) noexcept
  75. { return __rhs + __lhs; }
  76. _LIBCPP_HIDE_FROM_ABI constexpr
  77. months operator-(const year_month& __lhs, const year_month& __rhs) noexcept
  78. { return (__lhs.year() - __rhs.year()) + months(static_cast<unsigned>(__lhs.month()) - static_cast<unsigned>(__rhs.month())); }
  79. _LIBCPP_HIDE_FROM_ABI constexpr
  80. year_month operator-(const year_month& __lhs, const months& __rhs) noexcept
  81. { return __lhs + -__rhs; }
  82. _LIBCPP_HIDE_FROM_ABI constexpr
  83. year_month operator-(const year_month& __lhs, const years& __rhs) noexcept
  84. { return __lhs + -__rhs; }
  85. } // namespace chrono
  86. _LIBCPP_END_NAMESPACE_STD
  87. #endif // _LIBCPP_STD_VER > 17
  88. #endif // _LIBCPP___CHRONO_YEAR_MONTH_H