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