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