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