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