day.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_DAY_H
  10. #define _LIBCPP___CHRONO_DAY_H
  11. #include <__chrono/duration.h>
  12. #include <__config>
  13. #include <compare>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. #if _LIBCPP_STD_VER >= 20
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. namespace chrono {
  20. class day {
  21. private:
  22. unsigned char __d_;
  23. public:
  24. day() = default;
  25. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr day(unsigned __val) noexcept
  26. : __d_(static_cast<unsigned char>(__val)) {}
  27. _LIBCPP_HIDE_FROM_ABI inline constexpr day& operator++() noexcept {
  28. ++__d_;
  29. return *this;
  30. }
  31. _LIBCPP_HIDE_FROM_ABI inline constexpr day operator++(int) noexcept {
  32. day __tmp = *this;
  33. ++(*this);
  34. return __tmp;
  35. }
  36. _LIBCPP_HIDE_FROM_ABI inline constexpr day& operator--() noexcept {
  37. --__d_;
  38. return *this;
  39. }
  40. _LIBCPP_HIDE_FROM_ABI inline constexpr day operator--(int) noexcept {
  41. day __tmp = *this;
  42. --(*this);
  43. return __tmp;
  44. }
  45. _LIBCPP_HIDE_FROM_ABI constexpr day& operator+=(const days& __dd) noexcept;
  46. _LIBCPP_HIDE_FROM_ABI constexpr day& operator-=(const days& __dd) noexcept;
  47. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator unsigned() const noexcept { return __d_; }
  48. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __d_ >= 1 && __d_ <= 31; }
  49. };
  50. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const day& __lhs, const day& __rhs) noexcept {
  51. return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs);
  52. }
  53. _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(const day& __lhs, const day& __rhs) noexcept {
  54. return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
  55. }
  56. _LIBCPP_HIDE_FROM_ABI inline constexpr day operator+(const day& __lhs, const days& __rhs) noexcept {
  57. return day(static_cast<unsigned>(__lhs) + __rhs.count());
  58. }
  59. _LIBCPP_HIDE_FROM_ABI inline constexpr day operator+(const days& __lhs, const day& __rhs) noexcept {
  60. return __rhs + __lhs;
  61. }
  62. _LIBCPP_HIDE_FROM_ABI inline constexpr day operator-(const day& __lhs, const days& __rhs) noexcept {
  63. return __lhs + -__rhs;
  64. }
  65. _LIBCPP_HIDE_FROM_ABI inline constexpr days operator-(const day& __lhs, const day& __rhs) noexcept {
  66. return days(static_cast<int>(static_cast<unsigned>(__lhs)) - static_cast<int>(static_cast<unsigned>(__rhs)));
  67. }
  68. _LIBCPP_HIDE_FROM_ABI inline constexpr day& day::operator+=(const days& __dd) noexcept {
  69. *this = *this + __dd;
  70. return *this;
  71. }
  72. _LIBCPP_HIDE_FROM_ABI inline constexpr day& day::operator-=(const days& __dd) noexcept {
  73. *this = *this - __dd;
  74. return *this;
  75. }
  76. } // namespace chrono
  77. _LIBCPP_END_NAMESPACE_STD
  78. #endif // _LIBCPP_STD_VER >= 20
  79. #endif // _LIBCPP___CHRONO_DAY_H