Embedded Template Library 1.0
Loading...
Searching...
No Matches
day.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2023 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_IN_CHRONO_H
32 #error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H
33#endif
34
35namespace etl
36{
37 namespace chrono
38 {
39 //***********************************************************************
41 //***********************************************************************
42 class day
43 {
44 public:
45
46 using rep = uint_least8_t;
47
48 //***********************************************************************
50 //***********************************************************************
51 ETL_CONSTEXPR day() ETL_NOEXCEPT
52 : value(0)
53 {
54 }
55
56 //***********************************************************************
58 //***********************************************************************
59 ETL_CONSTEXPR explicit day(unsigned value_) ETL_NOEXCEPT
60 : value(static_cast<rep>(value_))
61 {
62 }
63
64 //***********************************************************************
66 //***********************************************************************
67 ETL_CONSTEXPR14 etl::chrono::day& operator++() ETL_NOEXCEPT
68 {
69 ++value;
70
71 return *this;
72 }
73
74 //***********************************************************************
76 //***********************************************************************
77 ETL_CONSTEXPR14 etl::chrono::day operator++(int) ETL_NOEXCEPT
78 {
79 etl::chrono::day temp = *this;
80 ++value;
81
82 return temp;
83 }
84
85 //***********************************************************************
87 //***********************************************************************
88 ETL_CONSTEXPR14 etl::chrono::day& operator--() ETL_NOEXCEPT
89 {
90 --value;
91
92 return *this;
93 }
94
95 //***********************************************************************
97 //***********************************************************************
98 ETL_CONSTEXPR14 etl::chrono::day operator--(int) ETL_NOEXCEPT
99 {
100 etl::chrono::day temp = *this;
101 --value;
102
103 return temp;
104 }
105
106 //***********************************************************************
108 //***********************************************************************
109 ETL_CONSTEXPR14 etl::chrono::day& operator+=(const etl::chrono::days& ds) ETL_NOEXCEPT
110 {
111 value += static_cast<rep>(ds.count());
112
113 return *this;
114 }
115
116 //***********************************************************************
118 //***********************************************************************
119 ETL_CONSTEXPR14 etl::chrono::day& operator-=(const etl::chrono::days& ds) ETL_NOEXCEPT
120 {
121 value -= static_cast<rep>(ds.count());
122
123 return *this;
124 }
125
126 //***********************************************************************
128 //***********************************************************************
129 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
130 {
131 return (value >= 1U) && (value <= 31U);
132 }
133
134 //***********************************************************************
136 //***********************************************************************
137 ETL_CONSTEXPR14 /*explicit*/ operator unsigned() const ETL_NOEXCEPT
138 {
139 return value;
140 }
141
142 //***********************************************************************
147 //***********************************************************************
148 ETL_NODISCARD ETL_CONSTEXPR14 int compare(const day& other) const ETL_NOEXCEPT
149 {
150 if (value < other.value)
151 return -1;
152 if (value > other.value)
153 return 1;
154
155 return 0;
156 }
157
158 private:
159
160 rep value;
161 };
162
163 //***********************************************************************
165 //***********************************************************************
166 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
167 {
168 return (static_cast<unsigned>(d1) == static_cast<unsigned>(d2));
169 }
170
171 //***********************************************************************
173 //***********************************************************************
174 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
175 {
176 return !(d1 == d2);
177 }
178
179 //***********************************************************************
181 //***********************************************************************
182 inline ETL_CONSTEXPR14 bool operator<(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
183 {
184 return (static_cast<unsigned>(d1) < static_cast<unsigned>(d2));
185 }
186
187 //***********************************************************************
189 //***********************************************************************
190 inline ETL_CONSTEXPR14 bool operator<=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
191 {
192 return (static_cast<unsigned>(d1) <= static_cast<unsigned>(d2));
193 }
194
195 //***********************************************************************
197 //***********************************************************************
198 inline ETL_CONSTEXPR14 bool operator>(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
199 {
200 return (static_cast<unsigned>(d1) > static_cast<unsigned>(d2));
201 }
202
203 //***********************************************************************
205 //***********************************************************************
206 inline ETL_CONSTEXPR14 bool operator>=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
207 {
208 return (static_cast<unsigned>(d1) >= static_cast<unsigned>(d2));
209 }
210
211 //***********************************************************************
213 //***********************************************************************
214#if ETL_USING_CPP20
215 [[nodiscard]]
216 inline constexpr auto operator<=>(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
217 {
218 return (static_cast<unsigned>(d1) <=> static_cast<unsigned>(d2));
219 }
220#endif
221
222 //***********************************************************************
225 //***********************************************************************
226 inline ETL_CONSTEXPR14 etl::chrono::day operator+(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
227 {
228 etl::chrono::day result(d);
229
230 result += ds;
231
232 return result;
233 }
234
235 //***********************************************************************
238 //***********************************************************************
239 inline ETL_CONSTEXPR14 etl::chrono::day operator+(const etl::chrono::days& ds, const etl::chrono::day& d) ETL_NOEXCEPT
240 {
241 etl::chrono::day result(d);
242
243 result += ds;
244
245 return result;
246 }
247
248 //***********************************************************************
251 //***********************************************************************
252 inline ETL_CONSTEXPR14 etl::chrono::day operator-(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
253 {
254 etl::chrono::day result(d);
255
256 result -= ds;
257
258 return result;
259 }
260
261 //***********************************************************************
264 //***********************************************************************
265 inline ETL_CONSTEXPR14 etl::chrono::days operator-(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
266 {
267 return etl::chrono::days(static_cast<int>(static_cast<unsigned>(d1)) - static_cast<int>(static_cast<unsigned>(d2)));
268 }
269 } // namespace chrono
270
271 //*************************************************************************
273 //*************************************************************************
274#if ETL_USING_8BIT_TYPES
275 template <>
276 struct hash<etl::chrono::day>
277 {
278 size_t operator()(const etl::chrono::day& d) const
279 {
280 etl::chrono::day::rep value = static_cast<etl::chrono::day::rep>(static_cast<unsigned>(d));
281 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
282
283 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
284 }
285 };
286#endif
287} // namespace etl
288
289#if ETL_HAS_CHRONO_LITERALS_DAY
290namespace etl
291{
292 inline namespace literals
293 {
294 inline namespace chrono_literals
295 {
296 #if ETL_USING_VERBOSE_CHRONO_LITERALS
297 inline ETL_CONSTEXPR14 etl::chrono::day operator""_day(unsigned long long d) ETL_NOEXCEPT
298 #else
299 inline ETL_CONSTEXPR14 etl::chrono::day operator""_d(unsigned long long d) ETL_NOEXCEPT
300 #endif
301 {
302 return etl::chrono::day(static_cast<unsigned>(d));
303 }
304 } // namespace chrono_literals
305 } // namespace literals
306} // namespace etl
307#endif
day
Definition day.h:43
ETL_CONSTEXPR14 etl::chrono::day & operator+=(const etl::chrono::days &ds) ETL_NOEXCEPT
Plus-equals operator adding etl::chrono::days.
Definition day.h:109
ETL_CONSTEXPR day() ETL_NOEXCEPT
Default constructor.
Definition day.h:51
ETL_CONSTEXPR14 etl::chrono::day & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition day.h:67
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the day is within the valid 1 to 31 range.
Definition day.h:129
ETL_CONSTEXPR14 etl::chrono::day & operator-=(const etl::chrono::days &ds) ETL_NOEXCEPT
Minus-equals operator subtracting etl::chrono::days.
Definition day.h:119
ETL_CONSTEXPR14 etl::chrono::day & operator--() ETL_NOEXCEPT
Pre-decrement operator.
Definition day.h:88
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const day &other) const ETL_NOEXCEPT
Definition day.h:148
ETL_CONSTEXPR14 etl::chrono::day operator--(int) ETL_NOEXCEPT
Post-decrement operator.
Definition day.h:98
ETL_CONSTEXPR day(unsigned value_) ETL_NOEXCEPT
Construct from unsigned.
Definition day.h:59
ETL_CONSTEXPR14 etl::chrono::day operator++(int) ETL_NOEXCEPT
Post-increment operator.
Definition day.h:77
ETL_CONSTEXPR14 etl::chrono::day operator+(const etl::chrono::day &d, const etl::chrono::days &ds) ETL_NOEXCEPT
Spaceship operator.
Definition day.h:226
ETL_CONSTEXPR14 etl::chrono::day operator-(const etl::chrono::day &d, const etl::chrono::days &ds) ETL_NOEXCEPT
Definition day.h:252
ETL_CONSTEXPR14 bool operator<(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Less-than operator.
Definition day.h:182
ETL_CONSTEXPR14 bool operator>=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Greater-than-or-equal operator.
Definition day.h:206
ETL_CONSTEXPR14 bool operator==(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Equality operator.
Definition day.h:166
ETL_CONSTEXPR14 bool operator<=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Less-than-or-equal operator.
Definition day.h:190
ETL_CONSTEXPR14 bool operator!=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Inequality operator.
Definition day.h:174
ETL_CONSTEXPR14 bool operator>(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Greater-than operator.
Definition day.h:198
bitset_ext
Definition absolute.h:40