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 etl::chrono::month& operator++() ETL_NOEXCEPT
79 {
80 *this += etl::chrono::months(1);
81
82 return *this;
83 }
84
85 //***********************************************************************
87 //***********************************************************************
88 ETL_CONSTEXPR14 etl::chrono::month operator++(int) ETL_NOEXCEPT
89 {
90 etl::chrono::month temp = *this;
91
92 ++*this;
93
94 return temp;
95 }
96
97 //***********************************************************************
99 //***********************************************************************
100 ETL_CONSTEXPR14 etl::chrono::month& operator--() ETL_NOEXCEPT
101 {
102 *this -= etl::chrono::months(1);
103
104 return *this;
105 }
106
107 //***********************************************************************
109 //***********************************************************************
110 ETL_CONSTEXPR14 etl::chrono::month operator--(int) ETL_NOEXCEPT
111 {
112 etl::chrono::month temp = *this;
113
114 --*this;
115
116 return temp;
117 }
118
119 //***********************************************************************
121 //***********************************************************************
122 ETL_CONSTEXPR14 etl::chrono::month& operator+=(const etl::chrono::months& ms) ETL_NOEXCEPT
123 {
124 *this = *this + ms;
125
126 return *this;
127 }
128
129 //***********************************************************************
131 //***********************************************************************
132 ETL_CONSTEXPR14 etl::chrono::month& operator-=(const etl::chrono::months& ms) ETL_NOEXCEPT
133 {
134 *this = *this - ms;
135
136 return *this;
137 }
138
139 //***********************************************************************
141 //***********************************************************************
142 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
143 {
144 return (value >= 1U) && (value <= 12U);
145 }
146
147 //***********************************************************************
152 //***********************************************************************
153 ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month& other) const ETL_NOEXCEPT
154 {
155 if (value < other.value)
156 return -1;
157 if (value > other.value)
158 return 1;
159
160 return 0;
161 }
162
163 //***********************************************************************
165 //***********************************************************************
166 ETL_CONSTEXPR14 /*explicit*/ operator unsigned() const ETL_NOEXCEPT
167 {
168 return value;
169 }
170
171 private:
172
173 rep value;
174 };
175
176 //***********************************************************************
178 //***********************************************************************
179 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
180 {
181 return (static_cast<unsigned>(d1) == static_cast<unsigned>(d2));
182 }
183
184 //***********************************************************************
186 //***********************************************************************
187 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
188 {
189 return !(d1 == d2);
190 }
191
192 //***********************************************************************
194 //***********************************************************************
195 inline ETL_CONSTEXPR14 bool operator<(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
196 {
197 return (static_cast<unsigned>(d1) < static_cast<unsigned>(d2));
198 }
199
200 //***********************************************************************
202 //***********************************************************************
203 inline ETL_CONSTEXPR14 bool operator<=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
204 {
205 return (static_cast<unsigned>(d1) <= static_cast<unsigned>(d2));
206 }
207
208 //***********************************************************************
210 //***********************************************************************
211 inline ETL_CONSTEXPR14 bool operator>(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
212 {
213 return (static_cast<unsigned>(d1) > static_cast<unsigned>(d2));
214 }
215
216 //***********************************************************************
218 //***********************************************************************
219 inline ETL_CONSTEXPR14 bool operator>=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
220 {
221 return (static_cast<unsigned>(d1) >= static_cast<unsigned>(d2));
222 }
223
224 //***********************************************************************
226 //***********************************************************************
227#if ETL_USING_CPP20
228 [[nodiscard]]
229 inline constexpr auto operator<=>(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
230 {
231 return (static_cast<unsigned>(d1) <=> static_cast<unsigned>(d2));
232 }
233#endif
234
235 //***********************************************************************
238 //***********************************************************************
239 inline ETL_CONSTEXPR14 etl::chrono::month operator+(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT
240 {
241 unsigned int value = static_cast<unsigned int>(m);
242
243 value = value % 12U;
244
245 if (value == 0U)
246 {
247 value = 12U;
248 }
249
250 int delta = ms.count() % 12;
251
252 // Adjust to allow a limited +-11 month delta
253 value += 11U;
254 value += static_cast<unsigned int>(delta);
255 value %= 12U;
256 ++value;
257
258 return etl::chrono::month(value);
259 }
260
261 //***********************************************************************
264 //***********************************************************************
265 inline ETL_CONSTEXPR14 etl::chrono::month operator+(const etl::chrono::months& ms, const etl::chrono::month& m) ETL_NOEXCEPT
266 {
267 return m + ms;
268 }
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 return m + etl::chrono::months(-ms.count());
277 }
278
279 //***********************************************************************
282 //***********************************************************************
283 inline ETL_CONSTEXPR14 etl::chrono::months operator-(const etl::chrono::month& m1, const etl::chrono::month& m2) ETL_NOEXCEPT
284 {
285 if (m1.ok() && m2.ok())
286 {
287 // Calculate the signed difference.
288 int difference = static_cast<int>(static_cast<unsigned>(m1)) - static_cast<int>(static_cast<unsigned>(m2));
289
290 // Adjust for wrap-around.
291 if (difference < 0)
292 {
293 difference += 12;
294 }
295
296 etl::chrono::months ms(difference);
297
298 // Check for validity.
299 assert(m1 == (m2 + ms));
300 return ms;
301 }
302
303 return etl::chrono::months();
304 }
305
306#if ETL_USING_CPP17
307 inline constexpr etl::chrono::month January{1};
308 inline constexpr etl::chrono::month February{2};
309 inline constexpr etl::chrono::month March{3};
310 inline constexpr etl::chrono::month April{4};
311 inline constexpr etl::chrono::month May{5};
312 inline constexpr etl::chrono::month June{6};
313 inline constexpr etl::chrono::month July{7};
314 inline constexpr etl::chrono::month August{8};
315 inline constexpr etl::chrono::month September{9};
316 inline constexpr etl::chrono::month October{10};
317 inline constexpr etl::chrono::month November{11};
318 inline constexpr etl::chrono::month December{12};
319#else
320 static ETL_CONSTANT etl::chrono::month January{1};
321 static ETL_CONSTANT etl::chrono::month February{2};
322 static ETL_CONSTANT etl::chrono::month March{3};
323 static ETL_CONSTANT etl::chrono::month April{4};
324 static ETL_CONSTANT etl::chrono::month May{5};
325 static ETL_CONSTANT etl::chrono::month June{6};
326 static ETL_CONSTANT etl::chrono::month July{7};
327 static ETL_CONSTANT etl::chrono::month August{8};
328 static ETL_CONSTANT etl::chrono::month September{9};
329 static ETL_CONSTANT etl::chrono::month October{10};
330 static ETL_CONSTANT etl::chrono::month November{11};
331 static ETL_CONSTANT etl::chrono::month December{12};
332#endif
333 } // namespace chrono
334
335 //*************************************************************************
337 //*************************************************************************
338#if ETL_USING_8BIT_TYPES
339 template <>
340 struct hash<etl::chrono::month>
341 {
342 size_t operator()(const etl::chrono::month& m) const
343 {
344 etl::chrono::month::rep value = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(m));
345 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
346
347 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
348 }
349 };
350#endif
351
352} // namespace etl
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:100
ETL_CONSTEXPR14 etl::chrono::month & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition month.h:78
ETL_CONSTEXPR14 etl::chrono::month & operator+=(const etl::chrono::months &ms) ETL_NOEXCEPT
Plus-equals operator adding etl::chrono::months.
Definition month.h:122
ETL_CONSTEXPR14 etl::chrono::month operator++(int) ETL_NOEXCEPT
Post-increment operator.
Definition month.h:88
ETL_CONSTEXPR14 etl::chrono::month operator--(int) ETL_NOEXCEPT
Post-decrement operator.
Definition month.h:110
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:132
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the month is within the valid 1 to 12 range.
Definition month.h:142
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month &other) const ETL_NOEXCEPT
Definition month.h:153
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