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<unsigned char>(value_))
61 {
62 }
63
64 //***********************************************************************
66 //***********************************************************************
67 ETL_CONSTEXPR14 day(const etl::chrono::day& other) ETL_NOEXCEPT
68 : value(other.value)
69 {
70 }
71
72 //***********************************************************************
74 //***********************************************************************
75 ETL_CONSTEXPR14 etl::chrono::day& operator =(const etl::chrono::day& rhs) ETL_NOEXCEPT
76 {
77 value = rhs.value;
78
79 return *this;
80 }
81
82 //***********************************************************************
84 //***********************************************************************
85 template <typename TToDuration, typename TValue2, typename TPeriod2>
92
93 //***********************************************************************
95 //***********************************************************************
96 ETL_CONSTEXPR14 etl::chrono::day& operator ++() ETL_NOEXCEPT
97 {
98 ++value;
99
100 return *this;
101 }
102
103 //***********************************************************************
105 //***********************************************************************
106 ETL_CONSTEXPR14 etl::chrono::day operator ++(int) ETL_NOEXCEPT
107 {
108 const etl::chrono::day temp = *this;
109 ++value;
110
111 return temp;
112 }
113
114 //***********************************************************************
116 //***********************************************************************
117 ETL_CONSTEXPR14 etl::chrono::day& operator --() ETL_NOEXCEPT
118 {
119 --value;
120
121 return *this;
122 }
123
124 //***********************************************************************
126 //***********************************************************************
127 ETL_CONSTEXPR14 etl::chrono::day operator --(int) ETL_NOEXCEPT
128 {
129 const etl::chrono::day temp = *this;
130 --value;
131
132 return temp;
133 }
134
135 //***********************************************************************
137 //***********************************************************************
138 ETL_CONSTEXPR14 etl::chrono::day& operator +=(const etl::chrono::days& ds) ETL_NOEXCEPT
139 {
140 value += static_cast<unsigned char>(ds.count());
141
142 return *this;
143 }
144
145 //***********************************************************************
147 //***********************************************************************
148 ETL_CONSTEXPR14 etl::chrono::day& operator -=(const etl::chrono::days& ds) ETL_NOEXCEPT
149 {
150 value -= static_cast<unsigned char>(ds.count());
151
152 return *this;
153 }
154
155 //***********************************************************************
157 //***********************************************************************
158 ETL_NODISCARD
159 ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
160 {
161 return (value >= 1U) && (value <= 31U);
162 }
163
164 //***********************************************************************
166 //***********************************************************************
167 ETL_CONSTEXPR14 operator unsigned() const ETL_NOEXCEPT
168 {
169 return static_cast<unsigned>(value);
170 }
171
172 //***********************************************************************
177 //***********************************************************************
178 ETL_NODISCARD
179 ETL_CONSTEXPR14 int compare(const day& other) const ETL_NOEXCEPT
180 {
181 if (value < other.value) return -1;
182 if (value > other.value) return 1;
183
184 return 0;
185 }
186
187 //***********************************************************************
189 //***********************************************************************
190 ETL_NODISCARD
191 static ETL_CONSTEXPR14 etl::chrono::day min() ETL_NOEXCEPT
192 {
193 return etl::chrono::day(1);
194 }
195
196 //***********************************************************************
198 //***********************************************************************
199 ETL_NODISCARD
200 static ETL_CONSTEXPR14 etl::chrono::day max() ETL_NOEXCEPT
201 {
202 return etl::chrono::day(31);
203 }
204
205 private:
206
207 rep value;
208 };
209
210 //***********************************************************************
212 //***********************************************************************
213 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
214 {
215 return (static_cast<unsigned>(d1) == static_cast<unsigned>(d2));
216 }
217
218 //***********************************************************************
220 //***********************************************************************
221 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
222 {
223 return !(d1 == d2);
224 }
225
226 //***********************************************************************
228 //***********************************************************************
229 inline ETL_CONSTEXPR14 bool operator <(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
230 {
231 return (static_cast<unsigned>(d1) < static_cast<unsigned>(d2));
232 }
233
234 //***********************************************************************
236 //***********************************************************************
237 inline ETL_CONSTEXPR14 bool operator <=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
238 {
239 return (static_cast<unsigned>(d1) <= static_cast<unsigned>(d2));
240 }
241
242 //***********************************************************************
244 //***********************************************************************
245 inline ETL_CONSTEXPR14 bool operator >(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
246 {
247 return (static_cast<unsigned>(d1) > static_cast<unsigned>(d2));
248 }
249
250 //***********************************************************************
252 //***********************************************************************
253 inline ETL_CONSTEXPR14 bool operator >=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
254 {
255 return (static_cast<unsigned>(d1) >= static_cast<unsigned>(d2));
256 }
257
258 //***********************************************************************
260 //***********************************************************************
261#if ETL_USING_CPP20
262 [[nodiscard]] inline constexpr auto operator <=>(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
263 {
264 return (static_cast<unsigned>(d1) <=> static_cast<unsigned>(d2));
265 }
266#endif
267
268 //***********************************************************************
271 //***********************************************************************
272 inline ETL_CONSTEXPR14 etl::chrono::day operator +(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
273 {
274 etl::chrono::day result(d);
275
276 result += ds;
277
278 return result;
279 }
280
281 //***********************************************************************
284 //***********************************************************************
285 inline ETL_CONSTEXPR14 etl::chrono::day operator +(const etl::chrono::days& ds, const etl::chrono::day& d) ETL_NOEXCEPT
286 {
287 etl::chrono::day result(d);
288
289 result += ds;
290
291 return result;
292 }
293
294 //***********************************************************************
297 //***********************************************************************
298 inline ETL_CONSTEXPR14 etl::chrono::day operator -(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
299 {
300 etl::chrono::day result(d);
301
302 result -= ds;
303
304 return result;
305 }
306
307 //***********************************************************************
310 //***********************************************************************
311 inline ETL_CONSTEXPR14 etl::chrono::days operator -(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
312 {
313 return etl::chrono::days(static_cast<int>(static_cast<unsigned>(d1)) -
314 static_cast<int>(static_cast<unsigned>(d2)));
315 }
316 }
317
318 //*************************************************************************
320 //*************************************************************************
321#if ETL_USING_8BIT_TYPES
322 template <>
323 struct hash<etl::chrono::day>
324 {
325 size_t operator()(const etl::chrono::day& d) const
326 {
327 etl::chrono::day::rep value = static_cast<etl::chrono::day::rep>(static_cast<unsigned>(d));
328 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
329
330 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
331 }
332 };
333#endif
334}
335
336#if ETL_HAS_CHRONO_LITERALS_DAY
337namespace etl
338{
339 inline namespace literals
340 {
341 inline namespace chrono_literals
342 {
343#if ETL_USING_VERBOSE_CHRONO_LITERALS
344 inline ETL_CONSTEXPR14 etl::chrono::day operator ""_day(unsigned long long d) ETL_NOEXCEPT
345#else
346 inline ETL_CONSTEXPR14 etl::chrono::day operator ""_d(unsigned long long d) ETL_NOEXCEPT
347#endif
348 {
349 return etl::chrono::day(static_cast<unsigned>(d));
350 }
351 }
352 }
353}
354#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:138
ETL_CONSTEXPR day() ETL_NOEXCEPT
Default constructor.
Definition day.h:51
ETL_CONSTEXPR14 etl::chrono::day & operator=(const etl::chrono::day &rhs) ETL_NOEXCEPT
Assignment operator.
Definition day.h:75
ETL_CONSTEXPR14 etl::chrono::day & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition day.h:96
ETL_CONSTEXPR14 day(const etl::chrono::day &other) ETL_NOEXCEPT
Copy constructor.
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:159
ETL_CONSTEXPR14 etl::chrono::day & operator-=(const etl::chrono::days &ds) ETL_NOEXCEPT
Minus-equals operator subtracting etl::chrono::days.
Definition day.h:148
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::day max() ETL_NOEXCEPT
The maximum day value for which ok() will return true.
Definition day.h:200
ETL_CONSTEXPR14 etl::chrono::day & operator--() ETL_NOEXCEPT
Pre-decrement operator.
Definition day.h:117
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const day &other) const ETL_NOEXCEPT
Definition day.h:179
ETL_CONSTEXPR day(unsigned value_) ETL_NOEXCEPT
Construct from unsigned.
Definition day.h:59
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::day min() ETL_NOEXCEPT
The minimum day value for which ok() will return true.
Definition day.h:191
duration
Definition duration.h:108
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:339
bitset_ext
Definition absolute.h:39