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 = static_cast<unsigned char>((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 ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
183 {
184 return (c_encoding() <= 6U);
185 }
186
187 //***********************************************************************
190 //***********************************************************************
191 ETL_NODISCARD
192 static ETL_CONSTEXPR14 unsigned min() ETL_NOEXCEPT
193 {
194 return 0;
195 }
196
197 //***********************************************************************
200 //***********************************************************************
201 ETL_NODISCARD
202 static ETL_CONSTEXPR14 unsigned max() ETL_NOEXCEPT
203 {
204 return 6;
205 }
206
207 //***********************************************************************
209 //***********************************************************************
210 ETL_NODISCARD ETL_CONSTEXPR14 unsigned c_encoding() const ETL_NOEXCEPT
211 {
212 return value;
213 }
214
215 //***********************************************************************
217 //***********************************************************************
218 ETL_NODISCARD ETL_CONSTEXPR14 unsigned iso_encoding() const ETL_NOEXCEPT
219 {
220 return (value == 0U) ? 7U : value;
221 }
222
223 //***********************************************************************
225 //***********************************************************************
226 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday_indexed operator[](unsigned index) const ETL_NOEXCEPT;
227
228 //***********************************************************************
230 //***********************************************************************
231 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday_last operator[](etl::chrono::last_spec last) const ETL_NOEXCEPT;
232
233 //***********************************************************************
235 //***********************************************************************
236 ETL_NODISCARD ETL_CONSTEXPR14 bool is_weekend() const ETL_NOEXCEPT
237 {
238 return (c_encoding() == 0U) || (c_encoding() == 6U);
239 }
240
241 private:
242
243 // The weekday value in C encoding.
244 unsigned char value;
245 };
246
247 //***********************************************************************
249 //***********************************************************************
250 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
251 {
252 return (wd1.c_encoding() == wd2.c_encoding());
253 }
254
255 //***********************************************************************
257 //***********************************************************************
258 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
259 {
260 return !(wd1 == wd2);
261 }
262
263 //***********************************************************************
266 //***********************************************************************
267 inline ETL_CONSTEXPR14 etl::chrono::weekday operator+(const etl::chrono::weekday& wd, const etl::chrono::days& ds) ETL_NOEXCEPT
268 {
269 int delta = ds.count() % 7;
270
271 unsigned int value = wd.c_encoding();
272
273 // Adjust to allow a limited +-7 weekday delta
274 value %= 7U;
275 value += 7U;
276 value += static_cast<unsigned int>(delta);
277 value %= 7U;
278
279 return etl::chrono::weekday(value);
280 }
281
282 //***********************************************************************
285 //***********************************************************************
286 inline ETL_CONSTEXPR14 etl::chrono::weekday operator+(const etl::chrono::days& ds, const etl::chrono::weekday& wd) ETL_NOEXCEPT
287 {
288 return wd + ds;
289 }
290
291 //***********************************************************************
294 //***********************************************************************
295 inline ETL_CONSTEXPR14 etl::chrono::weekday operator-(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT
296 {
297 return m + etl::chrono::days(-ds.count());
298 }
299
300 //***********************************************************************
303 //***********************************************************************
304 inline ETL_CONSTEXPR14 etl::chrono::days operator-(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
305 {
306 if (wd1.ok() && wd2.ok())
307 {
308 int diff = static_cast<int>(wd1.c_encoding()) - static_cast<int>(wd2.c_encoding());
309
310 return etl::chrono::days((diff + 7) % 7);
311 }
312
313 return etl::chrono::days(0);
314 }
315
316#if ETL_USING_CPP17
317 inline constexpr etl::chrono::weekday Sunday{0U};
318 inline constexpr etl::chrono::weekday Monday{1U};
319 inline constexpr etl::chrono::weekday Tuesday{2U};
320 inline constexpr etl::chrono::weekday Wednesday{3U};
321 inline constexpr etl::chrono::weekday Thursday{4U};
322 inline constexpr etl::chrono::weekday Friday{5U};
323 inline constexpr etl::chrono::weekday Saturday{6U};
324#else
325 static ETL_CONSTANT etl::chrono::weekday Sunday{0U};
326 static ETL_CONSTANT etl::chrono::weekday Monday{1U};
327 static ETL_CONSTANT etl::chrono::weekday Tuesday{2U};
328 static ETL_CONSTANT etl::chrono::weekday Wednesday{3U};
329 static ETL_CONSTANT etl::chrono::weekday Thursday{4U};
330 static ETL_CONSTANT etl::chrono::weekday Friday{5U};
331 static ETL_CONSTANT etl::chrono::weekday Saturday{6U};
332#endif
333
334 //***********************************************************************
336 //***********************************************************************
338 {
339 public:
340
341 //***********************************************************************
343 //***********************************************************************
344 ETL_CONSTEXPR weekday_indexed() ETL_NOEXCEPT
345 : wd()
346 , i()
347 {
348 }
349
350 //***********************************************************************
352 //***********************************************************************
353 ETL_CONSTEXPR14 weekday_indexed(const etl::chrono::weekday& wd_, unsigned index_) ETL_NOEXCEPT
354 : wd(wd_)
355 , i(static_cast<uint_least8_t>(index_))
356 {
357 }
358
359 //***********************************************************************
361 //***********************************************************************
362 ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
363 {
364 return wd;
365 }
366
367 //***********************************************************************
369 //***********************************************************************
370 ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 unsigned index() const ETL_NOEXCEPT
371 {
372 return i;
373 }
374
375 //***********************************************************************
377 //***********************************************************************
378 ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
379 {
380 return wd.ok() && (i >= 1U) && (i <= 5U);
381 }
382
383 private:
384
386 uint_least8_t i;
387 };
388
389 //***********************************************************************
391 //***********************************************************************
392 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT
393 {
394 return (wd1.weekday() == wd2.weekday()) && (wd1.index() == wd2.index());
395 }
396
397 //***********************************************************************
399 //***********************************************************************
400 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT
401 {
402 return !(wd1 == wd2);
403 }
404
405 //***********************************************************************
407 //***********************************************************************
409 {
410 public:
411
412 //***********************************************************************
414 //***********************************************************************
415 ETL_CONSTEXPR14 explicit weekday_last(const etl::chrono::weekday& wd_) ETL_NOEXCEPT
416 : wd(wd_)
417 {
418 }
419
420 //***********************************************************************
422 //***********************************************************************
423 ETL_CONSTEXPR14 weekday_last(const etl::chrono::weekday_last& other) ETL_NOEXCEPT
424 : wd(other.wd)
425 {
426 }
427
428 //***********************************************************************
430 //***********************************************************************
431 ETL_CONSTEXPR14 etl::chrono::weekday_last& operator=(const etl::chrono::weekday_last& rhs) ETL_NOEXCEPT
432 {
433 wd = rhs.wd;
434
435 return *this;
436 }
437
438 //***********************************************************************
440 //***********************************************************************
441 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
442 {
443 return wd;
444 }
445
446 //***********************************************************************
448 //***********************************************************************
449 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
450 {
451 return wd.ok();
452 }
453
454 private:
455
457 };
458
459 //***********************************************************************
461 //***********************************************************************
462 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::weekday_last& wd1, const etl::chrono::weekday_last& wd2) ETL_NOEXCEPT
463 {
464 return (wd1.weekday() == wd2.weekday());
465 }
466
467 //***********************************************************************
469 //***********************************************************************
470 inline ETL_CONSTEXPR14 etl::chrono::weekday_indexed etl::chrono::weekday::operator[](unsigned index) const ETL_NOEXCEPT
471 {
472 return etl::chrono::weekday_indexed(*this, index);
473 }
474
475 //***********************************************************************
477 //***********************************************************************
479 {
480 return etl::chrono::weekday_last(*this);
481 }
482 } // namespace chrono
483
484 //*************************************************************************
486 //*************************************************************************
487#if ETL_USING_8BIT_TYPES
488 template <>
489 struct hash<etl::chrono::weekday>
490 {
491 size_t operator()(const etl::chrono::weekday& wd) const
492 {
493 unsigned value = wd.c_encoding();
494 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
495
496 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(unsigned));
497 }
498 };
499#endif
500
501 //*************************************************************************
503 //*************************************************************************
504#if ETL_USING_8BIT_TYPES
505 template <>
506 struct hash<etl::chrono::weekday_indexed>
507 {
508 size_t operator()(const etl::chrono::weekday_indexed& wdi) const
509 {
510 unsigned int a = wdi.weekday().c_encoding();
511 unsigned int b = wdi.index();
512
513 uint8_t buffer[sizeof(a) + sizeof(b)];
514
515 memcpy(buffer, &a, sizeof(a));
516 memcpy(buffer + sizeof(a), &b, sizeof(b));
517
518 return etl::private_hash::generic_hash<size_t>(buffer, buffer + sizeof(a) + sizeof(b));
519 }
520 };
521#endif
522
523 //*************************************************************************
525 //*************************************************************************
526#if ETL_USING_8BIT_TYPES
527 template <>
528 struct hash<etl::chrono::weekday_last>
529 {
530 size_t operator()(const etl::chrono::weekday_last& wdl) const
531 {
532 return etl::hash<etl::chrono::weekday>()(wdl.weekday());
533 }
534 };
535#endif
536} // namespace etl
weekday_indexed
Definition weekday.h:338
ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
Get weekday.
Definition weekday.h:362
ETL_CONSTEXPR14 weekday_indexed(const etl::chrono::weekday &wd_, unsigned index_) ETL_NOEXCEPT
Construct from weekday and index.
Definition weekday.h:353
ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the weekday and index are valid.
Definition weekday.h:378
ETL_NODISCARD ETL_NODISCARD ETL_CONSTEXPR14 unsigned index() const ETL_NOEXCEPT
Get index.
Definition weekday.h:370
ETL_CONSTEXPR weekday_indexed() ETL_NOEXCEPT
Default constructor.
Definition weekday.h:344
weekday_last
Definition weekday.h:409
ETL_CONSTEXPR14 weekday_last(const etl::chrono::weekday &wd_) ETL_NOEXCEPT
Construct from unsigned.
Definition weekday.h:415
ETL_CONSTEXPR14 weekday_last(const etl::chrono::weekday_last &other) ETL_NOEXCEPT
Copy constructor.
Definition weekday.h:423
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday weekday() const ETL_NOEXCEPT
Get weekday.
Definition weekday.h:441
ETL_CONSTEXPR14 etl::chrono::weekday_last & operator=(const etl::chrono::weekday_last &rhs) ETL_NOEXCEPT
Assignment operator.
Definition weekday.h:431
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the weekday is valid.
Definition weekday.h:449
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:218
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::weekday_indexed operator[](unsigned index) const ETL_NOEXCEPT
Index operator from index.
Definition weekday.h:470
static ETL_NODISCARD ETL_CONSTEXPR14 unsigned max() ETL_NOEXCEPT
Definition weekday.h:202
static ETL_NODISCARD ETL_CONSTEXPR14 unsigned min() ETL_NOEXCEPT
Definition weekday.h:192
ETL_NODISCARD ETL_CONSTEXPR14 bool is_weekend() const ETL_NOEXCEPT
Returns true if the day is a weekend.
Definition weekday.h:236
ETL_NODISCARD ETL_CONSTEXPR14 unsigned c_encoding() const ETL_NOEXCEPT
Get the C encoding of the weekday.
Definition weekday.h:210
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 etl::chrono::weekday operator++(int) ETL_NOEXCEPT
Post-increment operator.
Definition weekday.h:128
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--(int) ETL_NOEXCEPT
Post-decrement operator.
Definition weekday.h:150
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:182
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
Equality operator.
Definition day.h:166
ETL_CONSTEXPR14 bool operator!=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Inequality operator.
Definition day.h:174
bitset_ext
Definition absolute.h:40
Definition last_spec.h:40