year.h 4.2 KB

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