Embedded Template Library 1.0
Loading...
Searching...
No Matches
weekday.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
35#include <string.h>
36
37namespace etl
38{
39 namespace chrono
40 {
41 class weekday;
42 class weekday_indexed;
43 class weekday_last;
44 struct last_spec;
45
46 ETL_CONSTEXPR14 etl::chrono::weekday operator +(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT;
47 ETL_CONSTEXPR14 etl::chrono::weekday operator +(const etl::chrono::days& ds, const etl::chrono::weekday& m) ETL_NOEXCEPT;
48 ETL_CONSTEXPR14 etl::chrono::weekday operator -(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT;
49
50 //***********************************************************************
52 //***********************************************************************
53 class weekday
54 {
55 public:
56
57 //***********************************************************************
59 //***********************************************************************
60 ETL_CONSTEXPR weekday() ETL_NOEXCEPT
61 : value(255U)
62 {
63 }
64
65 //***********************************************************************
67 //***********************************************************************
68 ETL_CONSTEXPR explicit weekday(unsigned value_) ETL_NOEXCEPT
69 : value(value_ == 7U ? 0U :value_)
70 {
71 }
72
73 //*************************************************************************
75 //*************************************************************************
76 ETL_CONSTEXPR14 weekday(const etl::chrono::sys_days& sd) ETL_NOEXCEPT
77 : value(255U)
78 {
79 // Get number of days since epoch.
80 etl::chrono::days days_since_epoch = sd.time_since_epoch();
81
82 // Convert to weekday. Beginning of the epoch was a Thursday (4).
83 value = (days_since_epoch.count() + 4) % 7;
84 }
85
86 //*************************************************************************
88 //*************************************************************************
89 ETL_CONSTEXPR14 weekday(const etl::chrono::local_days& ld) ETL_NOEXCEPT
90 : value(255U)
91 {
92 weekday wd(sys_days(ld.time_since_epoch()));
93
94 value = wd.c_encoding();
95 }
96
97 //***********************************************************************
99 //***********************************************************************
100 ETL_CONSTEXPR14 weekday(const etl::chrono::weekday& other) ETL_NOEXCEPT
101 : value(other.value)
102 {
103 }
104
105 //***********************************************************************
107 //***********************************************************************
108 ETL_CONSTEXPR14 etl::chrono::weekday& operator =(const etl::chrono::weekday& rhs) ETL_NOEXCEPT
109 {
110 value = rhs.value;
111
112 return *this;
113 }
114
115 //***********************************************************************
117 //***********************************************************************
118 ETL_CONSTEXPR14 etl::chrono::weekday& operator ++() ETL_NOEXCEPT
119 {
120 *this += etl::chrono::days(1);
121
122 return *this;
123 }
124
125 //***********************************************************************
127 //***********************************************************************
128 ETL_CONSTEXPR14 etl::chrono::weekday operator ++(int) ETL_NOEXCEPT
129 {
130 const etl::chrono::weekday temp = *this;
131
132 *this += etl::chrono::days(1);
133
134 return temp;
135 }
136
137 //***********************************************************************
139 //***********************************************************************
140 ETL_CONSTEXPR14 etl::chrono::weekday& operator --() ETL_NOEXCEPT
141 {
142 *this -= etl::chrono::days(1);
143
144 return *this;
145 }
146
147 //***********************************************************************
149 //***********************************************************************
150 ETL_CONSTEXPR14 etl::chrono::weekday operator --(int) ETL_NOEXCEPT
151 {
152 etl::chrono::weekday temp = *this;
153
154 *this -= etl::chrono::days(1);
155
156 return temp;
157 }
158
159 //***********************************************************************
161 //***********************************************************************
162 ETL_CONSTEXPR14 etl::chrono::weekday& operator +=(const etl::chrono::days& ds) ETL_NOEXCEPT
163 {
164 *this = *this + ds;
165
166 return *this;
167 }
168
169 //***********************************************************************
171 //***********************************************************************
172 ETL_CONSTEXPR14 etl::chrono::weekday& operator -=(const etl::chrono::days& ds) ETL_NOEXCEPT
173 {
174 *this = *this - ds;
175
176 return *this;
177 }
178
179 //***********************************************************************
181 //***********************************************************************
182 ETL_NODISCARD
183 ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
184 {
185 return (c_encoding() <= 6U);
186 }
187
188 //***********************************************************************
191 //***********************************************************************
192 ETL_NODISCARD
193 static ETL_CONSTEXPR14 unsigned min() ETL_NOEXCEPT
194 {
195 return 0;
196 }
197
198 //***********************************************************************
201 //***********************************************************************
202 ETL_NODISCARD
203 static ETL_CONSTEXPR14 unsigned max() ETL_NOEXCEPT
204 {
205 return 6;
206 }
207
208 //***********************************************************************
210 //***********************************************************************
211 ETL_NODISCARD
212 ETL_CONSTEXPR14 unsigned c_encoding() const ETL_NOEXCEPT
213 {
214 return value;
215 }
216
217 //***********************************************************************
219 //***********************************************************************
220 ETL_NODISCARD
221 ETL_CONSTEXPR14 unsigned iso_encoding() const ETL_NOEXCEPT
222 {
223 return (value == 0U) ? 7U : value;
224 }
225
226 //***********************************************************************
228 //***********************************************************************
229 ETL_NODISCARD
230 ETL_CONSTEXPR14 etl::chrono::weekday_indexed operator[](unsigned index) const ETL_NOEXCEPT;
231
232 //***********************************************************************
234 //***********************************************************************
235 ETL_NODISCARD
236 ETL_CONSTEXPR14 etl::chrono::weekday_last operator[](etl::chrono::last_spec last) const ETL_NOEXCEPT;
237
238 //***********************************************************************
240 //***********************************************************************
241 ETL_NODISCARD
242 ETL_CONSTEXPR14 bool is_weekend() const ETL_NOEXCEPT
243 {
244 return (c_encoding() == 0U) || (c_encoding() == 6U);
245 }
246
247 private:
248
249 // The weekday value in C encoding.
250 unsigned char value;
251 };
252
253 //***********************************************************************
255 //***********************************************************************
256 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
257 {
258 return (wd1.c_encoding() == wd2.c_encoding());
259 }
260
261 //***********************************************************************
263 //***********************************************************************
264 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
265 {
266 return !(wd1 == wd2);
267 }
268
269 //***********************************************************************
272 //***********************************************************************
273 inline ETL_CONSTEXPR14 etl::chrono::weekday operator +(const etl::chrono::weekday& wd, const etl::chrono::days& ds) ETL_NOEXCEPT
274 {
275 int delta = ds.count() % 7;
276
277 unsigned int value = wd.c_encoding();
278
279 // Adjust to allow a limited +-7 weekday delta
280 value %= 7U;
281 value += 7U;
282 value += delta;
283 value %= 7U;
284
285 return etl::chrono::weekday(value);
286 }
287
288 //***********************************************************************
291 //***********************************************************************
292 inline ETL_CONSTEXPR14 etl::chrono::weekday operator +(const etl::chrono::days& ds, const etl::chrono::weekday& wd) ETL_NOEXCEPT
293 {
294 return wd + ds;
295 }
296
297 //***********************************************************************
300 //***********************************************************************
301 inline ETL_CONSTEXPR14 etl::chrono::weekday operator -(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT
302 {
303 return m + etl::chrono::days(-ds.count());
304 }
305
306 //***********************************************************************
309 //***********************************************************************
310 inline ETL_CONSTEXPR14 etl::chrono::days operator -(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
311 {
312 if (wd1.ok() && wd2.ok())
313 {
314 int diff = static_cast<int>(wd1.c_encoding()) - static_cast<int>(wd2.c_encoding());
315
316 return etl::chrono::days((diff + 7) % 7);
317 }
318
319 return etl::chrono::days(0);
320 }
321
322#if ETL_USING_CPP17
323 inline constexpr etl::chrono::weekday Sunday{ 0U };
324 inline constexpr etl::chrono::weekday Monday{ 1U };
325 inline constexpr etl::chrono::weekday Tuesday{ 2U };
326 inline constexpr etl::chrono::weekday Wednesday{ 3U };
327 inline constexpr etl::chrono::weekday Thursday{ 4U };
328 inline constexpr etl::chrono::weekday Friday{ 5U };
329 inline constexpr etl::chrono::weekday Saturday{ 6U };
330#else
331 static ETL_CONSTANT etl::chrono::weekday Sunday{ 0U };
332 static ETL_CONSTANT etl::chrono::weekday Monday{ 1U };
333 static ETL_CONSTANT etl::chrono::weekday Tuesday{ 2U };
334 static ETL_CONSTANT etl::chrono::weekday Wednesday{ 3U };
335 static ETL_CONSTANT etl::chrono::weekday Thursday{ 4U };
336 static ETL_CONSTANT etl::chrono::weekday Friday{ 5U };
337 static ETL_CONSTANT etl::chrono::weekday Saturday{ 6U };
338#endif
339
340 //***********************************************************************
342 //***********************************************************************
344 {
345 public:
346
347 //***********************************************************************
349 //***********************************************************************
350 ETL_CONSTEXPR weekday_indexed() ETL_NOEXCEPT
351 : wd()
352 , i()
353 {
354 }
355
356 //***********************************************************************
358 //***********************************************************************
359 ETL_CONSTEXPR14 weekday_indexed(const etl::chrono::weekday& wd_, unsigned index_) ETL_NOEXCEPT
360 : wd(wd_)
361 , i(static_cast<uint_least8_t>(index_))
362 {
363 }
364
365 //***********************************************************************
367 //***********************************************************************
368 ETL_CONSTEXPR14 weekday_indexed(const etl::chrono::weekday_indexed& other) ETL_NOEXCEPT
369 : wd(other.wd)
370 , i(other.i)
371 {
372 }
373
374 //***********************************************************************
376 //***********************************************************************
378 {
379 wd = rhs.wd;
380 i = rhs.i;
381
382 return *this;
383 }
384
385 //***********************************************************************
387 //***********************************************************************
388 ETL_NODISCARD
389 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
390 {
391 return wd;
392 }
393
394 //***********************************************************************
396 //***********************************************************************
397 ETL_NODISCARD
398 ETL_NODISCARD ETL_CONSTEXPR14 unsigned index() const ETL_NOEXCEPT
399 {
400 return i;
401 }
402
403 //***********************************************************************
405 //***********************************************************************
406 ETL_NODISCARD
407 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
408 {
409 return wd.ok() && (i >= 1U) && (i <= 5U);
410 }
411
412 private:
413
415 uint_least8_t i;
416 };
417
418 //***********************************************************************
420 //***********************************************************************
421 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT
422 {
423 return (wd1.weekday() == wd2.weekday()) &&
424 (wd1.index() == wd2.index());
425 }
426
427 //***********************************************************************
429 //***********************************************************************
430 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT
431 {
432 return !(wd1 == wd2);
433 }
434
435 //***********************************************************************
437 //***********************************************************************
439 {
440 public:
441
442 //***********************************************************************
444 //***********************************************************************
445 ETL_CONSTEXPR14 explicit weekday_last(const etl::chrono::weekday& wd_) ETL_NOEXCEPT
446 : wd(wd_)
447 {
448 }
449
450 //***********************************************************************
452 //***********************************************************************
453 ETL_CONSTEXPR14 weekday_last(const etl::chrono::weekday_last& other) ETL_NOEXCEPT
454 : wd(other.wd)
455 {
456 }
457
458 //***********************************************************************
460 //***********************************************************************
461 ETL_CONSTEXPR14 etl::chrono::weekday_last& operator =(const etl::chrono::weekday_last& rhs) ETL_NOEXCEPT
462 {
463 wd = rhs.wd;
464
465 return *this;
466 }
467
468 //***********************************************************************
470 //***********************************************************************
471 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
472 {
473 return wd;
474 }
475
476 //***********************************************************************
478 //***********************************************************************
479 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
480 {
481 return wd.ok();
482 }
483
484 private:
485
487 };
488
489 //***********************************************************************
491 //***********************************************************************
492 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::weekday_last& wd1, const etl::chrono::weekday_last& wd2) ETL_NOEXCEPT
493 {
494 return (wd1.weekday() == wd2.weekday());
495 }
496
497 //***********************************************************************
499 //***********************************************************************
500 inline ETL_CONSTEXPR14 etl::chrono::weekday_indexed etl::chrono::weekday::operator[](unsigned index) const ETL_NOEXCEPT
501 {
502 return etl::chrono::weekday_indexed(*this, index);
503 }
504
505 //***********************************************************************
507 //***********************************************************************
509 {
510 return etl::chrono::weekday_last(*this);
511 }
512 }
513
514 //*************************************************************************
516 //*************************************************************************
517#if ETL_USING_8BIT_TYPES
518 template <>
519 struct hash<etl::chrono::weekday>
520 {
521 size_t operator()(const etl::chrono::weekday& wd) const
522 {
523 unsigned value = wd.c_encoding();
524 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
525
526 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(unsigned));
527 }
528 };
529#endif
530
531 //*************************************************************************
533 //*************************************************************************
534#if ETL_USING_8BIT_TYPES
535 template <>
536 struct hash<etl::chrono::weekday_indexed>
537 {
538 size_t operator()(const etl::chrono::weekday_indexed& wdi) const
539 {
540 unsigned int a = wdi.weekday().c_encoding();
541 unsigned int b = wdi.index();
542
543 uint8_t buffer[sizeof(a) + sizeof(b)];
544
545 memcpy(buffer, &a, sizeof(a));
546 memcpy(buffer + sizeof(a), &b, sizeof(b));
547
548 return etl::private_hash::generic_hash<size_t>(buffer, buffer + sizeof(a) + sizeof(b));
549 }
550 };
551#endif
552
553 //*************************************************************************
555 //*************************************************************************
556#if ETL_USING_8BIT_TYPES
557 template <>
558 struct hash<etl::chrono::weekday_last>
559 {
560 size_t operator()(const etl::chrono::weekday_last& wdl) const
561 {
562 return etl::hash<etl::chrono::weekday>()(wdl.weekday());
563 }
564 };
565#endif
566}
weekday_indexed
Definition weekday.h:344
ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
Get weekday.
Definition weekday.h:389
ETL_CONSTEXPR14 etl::chrono::weekday_indexed & operator=(const etl::chrono::weekday_indexed &rhs) ETL_NOEXCEPT
Assignment operator.
Definition weekday.h:377
ETL_CONSTEXPR14 weekday_indexed(const etl::chrono::weekday &wd_, unsigned index_) ETL_NOEXCEPT
Construct from weekday and index.
Definition weekday.h:359
ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the weekday and index are valid.
Definition weekday.h:407
ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 unsigned index() const ETL_NOEXCEPT
Get index.
Definition weekday.h:398
ETL_CONSTEXPR weekday_indexed() ETL_NOEXCEPT
Default constructor.
Definition weekday.h:350
ETL_CONSTEXPR14 weekday_indexed(const etl::chrono::weekday_indexed &other) ETL_NOEXCEPT
Copy constructor.
Definition weekday.h:368
weekday_last
Definition weekday.h:439
ETL_CONSTEXPR14 weekday_last(const etl::chrono::weekday &wd_) ETL_NOEXCEPT
Construct from unsigned.
Definition weekday.h:445
ETL_CONSTEXPR14 weekday_last(const etl::chrono::weekday_last &other) ETL_NOEXCEPT
Copy constructor.
Definition weekday.h:453
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
Get weekday.
Definition weekday.h:471
ETL_CONSTEXPR14 etl::chrono::weekday_last & operator=(const etl::chrono::weekday_last &rhs) ETL_NOEXCEPT
Assignment operator.
Definition weekday.h:461
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the weekday is valid.
Definition weekday.h:479
weekday
Definition weekday.h:54
ETL_NODISCARD ETL_CONSTEXPR14 unsigned iso_encoding() const ETL_NOEXCEPT
Get the ISO encoding of the weekday.
Definition weekday.h:221
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday_indexed operator[](unsigned index) const ETL_NOEXCEPT
Index operator from index.
Definition weekday.h:500
static ETL_NODISCARD ETL_CONSTEXPR14 unsigned max() ETL_NOEXCEPT
Definition weekday.h:203
static ETL_NODISCARD ETL_CONSTEXPR14 unsigned min() ETL_NOEXCEPT
Definition weekday.h:193
ETL_NODISCARD ETL_CONSTEXPR14 bool is_weekend() const ETL_NOEXCEPT
Returns true if the day is a weekend.
Definition weekday.h:242
ETL_NODISCARD ETL_CONSTEXPR14 unsigned c_encoding() const ETL_NOEXCEPT
Get the C encoding of the weekday.
Definition weekday.h:212
ETL_CONSTEXPR weekday() ETL_NOEXCEPT
Default constructor.
Definition weekday.h:60
ETL_CONSTEXPR14 weekday(const etl::chrono::local_days &ld) ETL_NOEXCEPT
Construct from local_days.
Definition weekday.h:89
ETL_CONSTEXPR14 etl::chrono::weekday & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition weekday.h:118
ETL_CONSTEXPR14 etl::chrono::weekday & operator-=(const etl::chrono::days &ds) ETL_NOEXCEPT
Minus-equals operator subtracting etl::chrono::days.
Definition weekday.h:172
ETL_CONSTEXPR14 weekday(const etl::chrono::weekday &other) ETL_NOEXCEPT
Copy constructor.
Definition weekday.h:100
ETL_CONSTEXPR14 weekday(const etl::chrono::sys_days &sd) ETL_NOEXCEPT
Construct from sys_days.
Definition weekday.h:76
ETL_CONSTEXPR14 etl::chrono::weekday & operator=(const etl::chrono::weekday &rhs) ETL_NOEXCEPT
Assignment operator.
Definition weekday.h:108
ETL_CONSTEXPR14 etl::chrono::weekday & operator+=(const etl::chrono::days &ds) ETL_NOEXCEPT
Plus-equals operator adding etl::chrono::days.
Definition weekday.h:162
ETL_CONSTEXPR14 etl::chrono::weekday & operator--() ETL_NOEXCEPT
Pre-decrement operator.
Definition weekday.h:140
ETL_CONSTEXPR weekday(unsigned value_) ETL_NOEXCEPT
Construct from unsigned.
Definition weekday.h:68
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the weekday is within the valid 1 to 31 range.
Definition weekday.h:183
bitset_ext
Definition absolute.h:39
Definition last_spec.h:40