year.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_H
  10. #define _LIBCPP___CHRONO_YEAR_H
  11. #include <__chrono/duration.h>
  12. #include <__config>
  13. #include <compare>
  14. #include <limits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. #if _LIBCPP_STD_VER >= 20
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. namespace chrono {
  23. class year {
  24. private:
  25. short __y_;
  26. public:
  27. year() = default;
  28. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr year(int __val) noexcept : __y_(static_cast<short>(__val)) {}
  29. _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator++() noexcept {
  30. ++__y_;
  31. return *this;
  32. }
  33. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator++(int) noexcept {
  34. year __tmp = *this;
  35. ++(*this);
  36. return __tmp;
  37. }
  38. _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator--() noexcept {
  39. --__y_;
  40. return *this;
  41. }
  42. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator--(int) noexcept {
  43. year __tmp = *this;
  44. --(*this);
  45. return __tmp;
  46. }
  47. _LIBCPP_HIDE_FROM_ABI constexpr year& operator+=(const years& __dy) noexcept;
  48. _LIBCPP_HIDE_FROM_ABI constexpr year& operator-=(const years& __dy) noexcept;
  49. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator+() const noexcept { return *this; }
  50. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator-() const noexcept { return year{-__y_}; }
  51. _LIBCPP_HIDE_FROM_ABI inline constexpr bool is_leap() const noexcept {
  52. return __y_ % 4 == 0 && (__y_ % 100 != 0 || __y_ % 400 == 0);
  53. }
  54. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator int() const noexcept { return __y_; }
  55. _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
  56. _LIBCPP_HIDE_FROM_ABI static inline constexpr year min() noexcept { return year{-32767}; }
  57. _LIBCPP_HIDE_FROM_ABI static inline constexpr year max() noexcept { return year{32767}; }
  58. };
  59. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year& __lhs, const year& __rhs) noexcept {
  60. return static_cast<int>(__lhs) == static_cast<int>(__rhs);
  61. }
  62. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const year& __lhs, const year& __rhs) noexcept {
  63. return static_cast<int>(__lhs) <=> static_cast<int>(__rhs);
  64. }
  65. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator+(const year& __lhs, const years& __rhs) noexcept {
  66. return year(static_cast<int>(__lhs) + __rhs.count());
  67. }
  68. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator+(const years& __lhs, const year& __rhs) noexcept {
  69. return __rhs + __lhs;
  70. }
  71. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator-(const year& __lhs, const years& __rhs) noexcept {
  72. return __lhs + -__rhs;
  73. }
  74. _LIBCPP_HIDE_FROM_ABI inline constexpr years operator-(const year& __lhs, const year& __rhs) noexcept {
  75. return years{static_cast<int>(__lhs) - static_cast<int>(__rhs)};
  76. }
  77. _LIBCPP_HIDE_FROM_ABI inline constexpr year& year::operator+=(const years& __dy) noexcept {
  78. *this = *this + __dy;
  79. return *this;
  80. }
  81. _LIBCPP_HIDE_FROM_ABI inline constexpr year& year::operator-=(const years& __dy) noexcept {
  82. *this = *this - __dy;
  83. return *this;
  84. }
  85. _LIBCPP_HIDE_FROM_ABI constexpr bool year::ok() const noexcept {
  86. static_assert(static_cast<int>(std::numeric_limits<decltype(__y_)>::max()) == static_cast<int>(max()));
  87. return static_cast<int>(min()) <= __y_;
  88. }
  89. } // namespace chrono
  90. _LIBCPP_END_NAMESPACE_STD
  91. #endif // _LIBCPP_STD_VER >= 20
  92. _LIBCPP_POP_MACROS
  93. #endif // _LIBCPP___CHRONO_YEAR_H