year.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. {
  24. class year {
  25. private:
  26. short __y_;
  27. public:
  28. year() = default;
  29. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr year(int __val) noexcept : __y_(static_cast<short>(__val)) {}
  30. _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator++() noexcept { ++__y_; return *this; }
  31. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator++(int) noexcept { year __tmp = *this; ++(*this); return __tmp; }
  32. _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator--() noexcept { --__y_; return *this; }
  33. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator--(int) noexcept { year __tmp = *this; --(*this); return __tmp; }
  34. _LIBCPP_HIDE_FROM_ABI constexpr year& operator+=(const years& __dy) noexcept;
  35. _LIBCPP_HIDE_FROM_ABI constexpr year& operator-=(const years& __dy) noexcept;
  36. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator+() const noexcept { return *this; }
  37. _LIBCPP_HIDE_FROM_ABI inline constexpr year operator-() const noexcept { return year{-__y_}; }
  38. _LIBCPP_HIDE_FROM_ABI inline constexpr bool is_leap() const noexcept { return __y_ % 4 == 0 && (__y_ % 100 != 0 || __y_ % 400 == 0); }
  39. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator int() const noexcept { return __y_; }
  40. _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
  41. _LIBCPP_HIDE_FROM_ABI static inline constexpr year min() noexcept { return year{-32767}; }
  42. _LIBCPP_HIDE_FROM_ABI static inline constexpr year max() noexcept { return year{ 32767}; }
  43. };
  44. _LIBCPP_HIDE_FROM_ABI inline constexpr
  45. bool operator==(const year& __lhs, const year& __rhs) noexcept
  46. { return static_cast<int>(__lhs) == static_cast<int>(__rhs); }
  47. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const year& __lhs, const year& __rhs) noexcept {
  48. return static_cast<int>(__lhs) <=> static_cast<int>(__rhs);
  49. }
  50. _LIBCPP_HIDE_FROM_ABI inline constexpr
  51. year operator+ (const year& __lhs, const years& __rhs) noexcept
  52. { return year(static_cast<int>(__lhs) + __rhs.count()); }
  53. _LIBCPP_HIDE_FROM_ABI inline constexpr
  54. year operator+ (const years& __lhs, const year& __rhs) noexcept
  55. { return __rhs + __lhs; }
  56. _LIBCPP_HIDE_FROM_ABI inline constexpr
  57. year operator- (const year& __lhs, const years& __rhs) noexcept
  58. { return __lhs + -__rhs; }
  59. _LIBCPP_HIDE_FROM_ABI inline constexpr
  60. years operator-(const year& __lhs, const year& __rhs) noexcept
  61. { return years{static_cast<int>(__lhs) - static_cast<int>(__rhs)}; }
  62. _LIBCPP_HIDE_FROM_ABI inline constexpr
  63. year& year::operator+=(const years& __dy) noexcept
  64. { *this = *this + __dy; return *this; }
  65. _LIBCPP_HIDE_FROM_ABI inline constexpr
  66. year& year::operator-=(const years& __dy) noexcept
  67. { *this = *this - __dy; return *this; }
  68. _LIBCPP_HIDE_FROM_ABI constexpr bool year::ok() const noexcept {
  69. static_assert(static_cast<int>(std::numeric_limits<decltype(__y_)>::max()) == static_cast<int>(max()));
  70. return static_cast<int>(min()) <= __y_;
  71. }
  72. } // namespace chrono
  73. _LIBCPP_END_NAMESPACE_STD
  74. #endif // _LIBCPP_STD_VER >= 20
  75. _LIBCPP_POP_MACROS
  76. #endif // _LIBCPP___CHRONO_YEAR_H