Embedded Template Library 1.0
Loading...
Searching...
No Matches
month.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 class month;
40
41 ETL_CONSTEXPR14 etl::chrono::month operator +(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT;
42 ETL_CONSTEXPR14 etl::chrono::month operator +(const etl::chrono::months& ms, const etl::chrono::month& m) ETL_NOEXCEPT;
43 ETL_CONSTEXPR14 etl::chrono::month operator -(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT;
44
45 namespace private_chrono
46 {
47 static ETL_CONSTANT unsigned char days_in_month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
48 }
49
50 //***********************************************************************
52 //***********************************************************************
53 class month
54 {
55 public:
56
57 using rep = uint_least8_t;
58
59 //***********************************************************************
61 //***********************************************************************
62 ETL_CONSTEXPR month() ETL_NOEXCEPT
63 : value(0)
64 {
65 }
66
67 //***********************************************************************
69 //***********************************************************************
70 ETL_CONSTEXPR explicit month(unsigned value_) ETL_NOEXCEPT
71 : value(value_)
72 {
73 }
74
75 //***********************************************************************
77 //***********************************************************************
78 ETL_CONSTEXPR14 month(const etl::chrono::month& other) ETL_NOEXCEPT
79 : value(other.value)
80 {
81 }
82
83 //***********************************************************************
85 //***********************************************************************
86 ETL_CONSTEXPR14 etl::chrono::month& operator =(const etl::chrono::month& rhs) ETL_NOEXCEPT
87 {
88 value = rhs.value;
89
90 return *this;
91 }
92
93 //***********************************************************************
95 //***********************************************************************
96 ETL_CONSTEXPR14 etl::chrono::month& operator ++() ETL_NOEXCEPT
97 {
98 *this += etl::chrono::months(1);
99
100 return *this;
101 }
102
103 //***********************************************************************
105 //***********************************************************************
106 ETL_CONSTEXPR14 etl::chrono::month operator ++(int) ETL_NOEXCEPT
107 {
108 const etl::chrono::month temp = *this;
109
110 *this += etl::chrono::months(1);
111
112 return temp;
113 }
114
115 //***********************************************************************
117 //***********************************************************************
118 ETL_CONSTEXPR14 etl::chrono::month& operator --() ETL_NOEXCEPT
119 {
120 *this -= etl::chrono::months(1);
121
122 return *this;
123 }
124
125 //***********************************************************************
127 //***********************************************************************
128 ETL_CONSTEXPR14 etl::chrono::month operator --(int) ETL_NOEXCEPT
129 {
130 etl::chrono::month temp = *this;
131
132 *this -= etl::chrono::months(1);
133
134 return temp;
135 }
136
137 //***********************************************************************
139 //***********************************************************************
140 ETL_CONSTEXPR14 etl::chrono::month& operator +=(const etl::chrono::months& ms) ETL_NOEXCEPT
141 {
142 *this = *this + ms;
143
144 return *this;
145 }
146
147 //***********************************************************************
149 //***********************************************************************
150 ETL_CONSTEXPR14 etl::chrono::month& operator -=(const etl::chrono::months& ms) ETL_NOEXCEPT
151 {
152 *this = *this - ms;
153
154 return *this;
155 }
156
157 //***********************************************************************
159 //***********************************************************************
160 ETL_NODISCARD
161 ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
162 {
163 return (value >= 1U) && (value <= 12U);
164 }
165
166 //***********************************************************************
171 //***********************************************************************
172 ETL_NODISCARD
173 ETL_CONSTEXPR14 int compare(const month& other) const ETL_NOEXCEPT
174 {
175 if (value < other.value) return -1;
176 if (value > other.value) return 1;
177
178 return 0;
179 }
180
181 //***********************************************************************
183 //***********************************************************************
184 ETL_NODISCARD
185 static ETL_CONSTEXPR14 etl::chrono::month min() ETL_NOEXCEPT
186 {
187 return etl::chrono::month(1);
188 }
189
190 //***********************************************************************
192 //***********************************************************************
193 ETL_NODISCARD
194 static ETL_CONSTEXPR14 etl::chrono::month max() ETL_NOEXCEPT
195 {
196 return etl::chrono::month(12);
197 }
198
199 //***********************************************************************
201 //***********************************************************************
202 ETL_CONSTEXPR14 operator unsigned() const ETL_NOEXCEPT
203 {
204 return static_cast<unsigned>(value);
205 }
206
207 private:
208
209 rep value;
210 };
211
212 //***********************************************************************
214 //***********************************************************************
215 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
216 {
217 return (static_cast<unsigned>(d1) == static_cast<unsigned>(d2));
218 }
219
220 //***********************************************************************
222 //***********************************************************************
223 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
224 {
225 return !(d1 == d2);
226 }
227
228 //***********************************************************************
230 //***********************************************************************
231 inline ETL_CONSTEXPR14 bool operator <(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
232 {
233 return (static_cast<unsigned>(d1) < static_cast<unsigned>(d2));
234 }
235
236 //***********************************************************************
238 //***********************************************************************
239 inline ETL_CONSTEXPR14 bool operator <=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
240 {
241 return (static_cast<unsigned>(d1) <= static_cast<unsigned>(d2));
242 }
243
244 //***********************************************************************
246 //***********************************************************************
247 inline ETL_CONSTEXPR14 bool operator >(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
248 {
249 return (static_cast<unsigned>(d1) > static_cast<unsigned>(d2));
250 }
251
252 //***********************************************************************
254 //***********************************************************************
255 inline ETL_CONSTEXPR14 bool operator >=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
256 {
257 return (static_cast<unsigned>(d1) >= static_cast<unsigned>(d2));
258 }
259
260 //***********************************************************************
262 //***********************************************************************
263#if ETL_USING_CPP20
264 [[nodiscard]] inline constexpr auto operator <=>(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
265 {
266 return (static_cast<unsigned>(d1) <=> static_cast<unsigned>(d2));
267 }
268#endif
269
270 //***********************************************************************
273 //***********************************************************************
274 inline ETL_CONSTEXPR14 etl::chrono::month operator +(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT
275 {
276 unsigned int value = static_cast<unsigned int>(m);
277
278 value = value % 12U;
279
280 if (value == 0U)
281 {
282 value = 12U;
283 }
284
285 int delta = ms.count() % 12;
286
287 // Adjust to allow a limited +-11 month delta
288 value += 11U;
289 value += delta;
290 value %= 12U;
291 ++value;
292
293 return etl::chrono::month(value);
294 }
295
296 //***********************************************************************
299 //***********************************************************************
300 inline ETL_CONSTEXPR14 etl::chrono::month operator +(const etl::chrono::months& ms, const etl::chrono::month& m) ETL_NOEXCEPT
301 {
302 return m + ms;
303 }
304
305 //***********************************************************************
308 //***********************************************************************
309 inline ETL_CONSTEXPR14 etl::chrono::month operator -(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT
310 {
311 return m + etl::chrono::months(-ms.count());
312 }
313
314 //***********************************************************************
317 //***********************************************************************
318 inline ETL_CONSTEXPR14 etl::chrono::months operator -(const etl::chrono::month& m1, const etl::chrono::month& m2) ETL_NOEXCEPT
319 {
320 if (m1.ok() && m2.ok())
321 {
322 // Calculate the signed difference.
323 int difference = static_cast<int>(static_cast<unsigned>(m1)) - static_cast<int>(static_cast<unsigned>(m2));
324
325 // Adjust for wrap-around.
326 if (difference < 0)
327 {
328 difference += 12;
329 }
330
331 etl::chrono::months ms(difference);
332
333 // Check for validity.
334 if (m1 == (m2 + ms))
335 {
336 return ms;
337 }
338 }
339
340 return etl::chrono::months();
341 }
342
343#if ETL_USING_CPP17
344 inline constexpr etl::chrono::month January{ 1 };
345 inline constexpr etl::chrono::month February{ 2 };
346 inline constexpr etl::chrono::month March{ 3 };
347 inline constexpr etl::chrono::month April{ 4 };
348 inline constexpr etl::chrono::month May{ 5 };
349 inline constexpr etl::chrono::month June{ 6 };
350 inline constexpr etl::chrono::month July{ 7 };
351 inline constexpr etl::chrono::month August{ 8 };
352 inline constexpr etl::chrono::month September{ 9 };
353 inline constexpr etl::chrono::month October{ 10 };
354 inline constexpr etl::chrono::month November{ 11 };
355 inline constexpr etl::chrono::month December{ 12 };
356#else
357 static ETL_CONSTANT etl::chrono::month January{ 1 };
358 static ETL_CONSTANT etl::chrono::month February{ 2 };
359 static ETL_CONSTANT etl::chrono::month March{ 3 };
360 static ETL_CONSTANT etl::chrono::month April{ 4 };
361 static ETL_CONSTANT etl::chrono::month May{ 5 };
362 static ETL_CONSTANT etl::chrono::month June{ 6 };
363 static ETL_CONSTANT etl::chrono::month July{ 7 };
364 static ETL_CONSTANT etl::chrono::month August{ 8 };
365 static ETL_CONSTANT etl::chrono::month September{ 9 };
366 static ETL_CONSTANT etl::chrono::month October{ 10 };
367 static ETL_CONSTANT etl::chrono::month November{ 11 };
368 static ETL_CONSTANT etl::chrono::month December{ 12 };
369#endif
370 }
371
372 //*************************************************************************
374 //*************************************************************************
375#if ETL_USING_8BIT_TYPES
376 template <>
377 struct hash<etl::chrono::month>
378 {
379 size_t operator()(const etl::chrono::month& m) const
380 {
381 etl::chrono::month::rep value = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(m));
382 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
383
384 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
385 }
386 };
387#endif
388
389
390}
month
Definition month.h:54
ETL_CONSTEXPR month() ETL_NOEXCEPT
Default constructor.
Definition month.h:62
ETL_CONSTEXPR14 etl::chrono::month & operator--() ETL_NOEXCEPT
Pre-decrement operator.
Definition month.h:118
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::month max() ETL_NOEXCEPT
The maximum month value for which ok() will return true.
Definition month.h:194
ETL_CONSTEXPR14 etl::chrono::month & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition month.h:96
ETL_CONSTEXPR14 etl::chrono::month & operator=(const etl::chrono::month &rhs) ETL_NOEXCEPT
Assignment operator.
Definition month.h:86
ETL_CONSTEXPR14 etl::chrono::month & operator+=(const etl::chrono::months &ms) ETL_NOEXCEPT
Plus-equals operator adding etl::chrono::months.
Definition month.h:140
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::month min() ETL_NOEXCEPT
The minimum month value for which ok() will return true.
Definition month.h:185
ETL_CONSTEXPR14 month(const etl::chrono::month &other) ETL_NOEXCEPT
Copy constructor.
Definition month.h:78
ETL_CONSTEXPR month(unsigned value_) ETL_NOEXCEPT
Construct from unsigned.
Definition month.h:70
ETL_CONSTEXPR14 etl::chrono::month & operator-=(const etl::chrono::months &ms) ETL_NOEXCEPT
Minus-equals operator subtracting etl::chrono::months.
Definition month.h:150
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the month is within the valid 1 to 31 range.
Definition month.h:161
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month &other) const ETL_NOEXCEPT
Definition month.h:173
bitset_ext
Definition absolute.h:39