weekday.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_WEEKDAY_H
  10. #define _LIBCPP___CHRONO_WEEKDAY_H
  11. #include <__chrono/calendar.h>
  12. #include <__chrono/duration.h>
  13. #include <__chrono/system_clock.h>
  14. #include <__chrono/time_point.h>
  15. #include <__config>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. #if _LIBCPP_STD_VER >= 20
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. namespace chrono {
  22. class weekday_indexed;
  23. class weekday_last;
  24. class weekday {
  25. private:
  26. unsigned char __wd_;
  27. _LIBCPP_HIDE_FROM_ABI static constexpr unsigned char __weekday_from_days(int __days) noexcept;
  28. public:
  29. weekday() = default;
  30. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr weekday(unsigned __val) noexcept
  31. : __wd_(static_cast<unsigned char>(__val == 7 ? 0 : __val)) {}
  32. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday(const sys_days& __sysd) noexcept
  33. : __wd_(__weekday_from_days(__sysd.time_since_epoch().count())) {}
  34. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr weekday(const local_days& __locd) noexcept
  35. : __wd_(__weekday_from_days(__locd.time_since_epoch().count())) {}
  36. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& operator++() noexcept {
  37. __wd_ = (__wd_ == 6 ? 0 : __wd_ + 1);
  38. return *this;
  39. }
  40. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator++(int) noexcept {
  41. weekday __tmp = *this;
  42. ++(*this);
  43. return __tmp;
  44. }
  45. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& operator--() noexcept {
  46. __wd_ = (__wd_ == 0 ? 6 : __wd_ - 1);
  47. return *this;
  48. }
  49. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator--(int) noexcept {
  50. weekday __tmp = *this;
  51. --(*this);
  52. return __tmp;
  53. }
  54. _LIBCPP_HIDE_FROM_ABI constexpr weekday& operator+=(const days& __dd) noexcept;
  55. _LIBCPP_HIDE_FROM_ABI constexpr weekday& operator-=(const days& __dd) noexcept;
  56. _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned c_encoding() const noexcept { return __wd_; }
  57. _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned iso_encoding() const noexcept { return __wd_ == 0u ? 7 : __wd_; }
  58. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __wd_ <= 6; }
  59. _LIBCPP_HIDE_FROM_ABI constexpr weekday_indexed operator[](unsigned __index) const noexcept;
  60. _LIBCPP_HIDE_FROM_ABI constexpr weekday_last operator[](last_spec) const noexcept;
  61. };
  62. // https://howardhinnant.github.io/date_algorithms.html#weekday_from_days
  63. _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned char weekday::__weekday_from_days(int __days) noexcept {
  64. return static_cast<unsigned char>(static_cast<unsigned>(__days >= -4 ? (__days + 4) % 7 : (__days + 5) % 7 + 6));
  65. }
  66. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const weekday& __lhs, const weekday& __rhs) noexcept {
  67. return __lhs.c_encoding() == __rhs.c_encoding();
  68. }
  69. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<(const weekday& __lhs, const weekday& __rhs) noexcept {
  70. return __lhs.c_encoding() < __rhs.c_encoding();
  71. }
  72. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>(const weekday& __lhs, const weekday& __rhs) noexcept {
  73. return __rhs < __lhs;
  74. }
  75. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<=(const weekday& __lhs, const weekday& __rhs) noexcept {
  76. return !(__rhs < __lhs);
  77. }
  78. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(const weekday& __lhs, const weekday& __rhs) noexcept {
  79. return !(__lhs < __rhs);
  80. }
  81. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator+(const weekday& __lhs, const days& __rhs) noexcept {
  82. auto const __mu = static_cast<long long>(__lhs.c_encoding()) + __rhs.count();
  83. auto const __yr = (__mu >= 0 ? __mu : __mu - 6) / 7;
  84. return weekday{static_cast<unsigned>(__mu - __yr * 7)};
  85. }
  86. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator+(const days& __lhs, const weekday& __rhs) noexcept {
  87. return __rhs + __lhs;
  88. }
  89. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator-(const weekday& __lhs, const days& __rhs) noexcept {
  90. return __lhs + -__rhs;
  91. }
  92. _LIBCPP_HIDE_FROM_ABI inline constexpr days operator-(const weekday& __lhs, const weekday& __rhs) noexcept {
  93. const int __wdu = __lhs.c_encoding() - __rhs.c_encoding();
  94. const int __wk = (__wdu >= 0 ? __wdu : __wdu - 6) / 7;
  95. return days{__wdu - __wk * 7};
  96. }
  97. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& weekday::operator+=(const days& __dd) noexcept {
  98. *this = *this + __dd;
  99. return *this;
  100. }
  101. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& weekday::operator-=(const days& __dd) noexcept {
  102. *this = *this - __dd;
  103. return *this;
  104. }
  105. class weekday_indexed {
  106. private:
  107. chrono::weekday __wd_;
  108. unsigned char __idx_;
  109. public:
  110. weekday_indexed() = default;
  111. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday_indexed(const chrono::weekday& __wdval, unsigned __idxval) noexcept
  112. : __wd_{__wdval}, __idx_(__idxval) {}
  113. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept { return __wd_; }
  114. _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned index() const noexcept { return __idx_; }
  115. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __wd_.ok() && __idx_ >= 1 && __idx_ <= 5; }
  116. };
  117. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  118. operator==(const weekday_indexed& __lhs, const weekday_indexed& __rhs) noexcept {
  119. return __lhs.weekday() == __rhs.weekday() && __lhs.index() == __rhs.index();
  120. }
  121. class weekday_last {
  122. private:
  123. chrono::weekday __wd_;
  124. public:
  125. _LIBCPP_HIDE_FROM_ABI explicit constexpr weekday_last(const chrono::weekday& __val) noexcept : __wd_{__val} {}
  126. _LIBCPP_HIDE_FROM_ABI constexpr chrono::weekday weekday() const noexcept { return __wd_; }
  127. _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept { return __wd_.ok(); }
  128. };
  129. _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const weekday_last& __lhs, const weekday_last& __rhs) noexcept {
  130. return __lhs.weekday() == __rhs.weekday();
  131. }
  132. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday_indexed weekday::operator[](unsigned __index) const noexcept {
  133. return weekday_indexed{*this, __index};
  134. }
  135. _LIBCPP_HIDE_FROM_ABI inline constexpr weekday_last weekday::operator[](last_spec) const noexcept {
  136. return weekday_last{*this};
  137. }
  138. inline constexpr weekday Sunday{0};
  139. inline constexpr weekday Monday{1};
  140. inline constexpr weekday Tuesday{2};
  141. inline constexpr weekday Wednesday{3};
  142. inline constexpr weekday Thursday{4};
  143. inline constexpr weekday Friday{5};
  144. inline constexpr weekday Saturday{6};
  145. } // namespace chrono
  146. _LIBCPP_END_NAMESPACE_STD
  147. #endif // _LIBCPP_STD_VER >= 20
  148. #endif // _LIBCPP___CHRONO_WEEKDAY_H