Embedded Template Library 1.0
Loading...
Searching...
No Matches
year.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 year
43 {
44 public:
45
46 using rep = int16_t;
47
48 //***********************************************************************
50 //***********************************************************************
51 ETL_CONSTEXPR year() ETL_NOEXCEPT
52 : value(0)
53 {
54 }
55
56 //***********************************************************************
58 //***********************************************************************
59 ETL_CONSTEXPR explicit year(unsigned value_) ETL_NOEXCEPT
60 : value(value_)
61 {
62 }
63
64 //***********************************************************************
66 //***********************************************************************
67 ETL_CONSTEXPR14 year(const etl::chrono::year& other) ETL_NOEXCEPT
68 : value(other.value)
69 {
70 }
71
72 //***********************************************************************
74 //***********************************************************************
75 ETL_CONSTEXPR14 etl::chrono::year& operator =(const etl::chrono::year& rhs) ETL_NOEXCEPT
76 {
77 value = rhs.value;
78
79 return *this;
80 }
81
82 //***********************************************************************
84 //***********************************************************************
85 ETL_CONSTEXPR14 etl::chrono::year& operator ++() ETL_NOEXCEPT
86 {
87 ++value;
88
89 return *this;
90 }
91
92 //***********************************************************************
94 //***********************************************************************
95 ETL_CONSTEXPR14 etl::chrono::year operator ++(int) ETL_NOEXCEPT
96 {
97 const etl::chrono::year temp = *this;
98 ++value;
99
100 return temp;
101 }
102
103 //***********************************************************************
105 //***********************************************************************
106 ETL_CONSTEXPR14 etl::chrono::year& operator --() ETL_NOEXCEPT
107 {
108 --value;
109
110 return *this;
111 }
112
113 //***********************************************************************
115 //***********************************************************************
116 ETL_CONSTEXPR14 etl::chrono::year operator --(int) ETL_NOEXCEPT
117 {
118 const etl::chrono::year temp = *this;
119 --value;
120
121 return temp;
122 }
123
124 //***********************************************************************
126 //***********************************************************************
127 ETL_CONSTEXPR14 etl::chrono::year& operator +=(const etl::chrono::years& ys) ETL_NOEXCEPT
128 {
129 value += static_cast<unsigned char>(ys.count());
130
131 return *this;
132 }
133
134 //***********************************************************************
136 //***********************************************************************
137 ETL_CONSTEXPR14 etl::chrono::year& operator -=(const etl::chrono::years& ys) ETL_NOEXCEPT
138 {
139 value -= static_cast<unsigned char>(ys.count());
140
141 return *this;
142 }
143
144 //***********************************************************************
146 //***********************************************************************
147 ETL_NODISCARD
148 ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
149 {
150 return (value != etl::integral_limits<int16_t>::min);
151 }
152
153 //***********************************************************************
155 //***********************************************************************
156 ETL_NODISCARD
157 static ETL_CONSTEXPR14 etl::chrono::year min() ETL_NOEXCEPT
158 {
159 return etl::chrono::year(-32767);
160 }
161
162 //***********************************************************************
164 //***********************************************************************
165 ETL_NODISCARD
166 static ETL_CONSTEXPR14 etl::chrono::year max() ETL_NOEXCEPT
167 {
168 return etl::chrono::year(32767);
169 }
170
171 //***********************************************************************
173 //***********************************************************************
174 ETL_NODISCARD
175 ETL_CONSTEXPR14 bool is_leap() const ETL_NOEXCEPT
176 {
177 return ((value % 4) == 0) && // Divisible by 4
178 (((value % 100) != 0) || // but not divisible by 100
179 ((value % 400) == 0)); // unless divisible by 400
180 }
181
182 //***********************************************************************
184 //***********************************************************************
185 ETL_CONSTEXPR14 operator int() const ETL_NOEXCEPT
186 {
187 return static_cast<int>(value);
188 }
189
190 //***********************************************************************
195 //***********************************************************************
196 ETL_NODISCARD
197 ETL_CONSTEXPR14 int compare(const year& other) const ETL_NOEXCEPT
198 {
199 if (value < other.value) return -1;
200 if (value > other.value) return 1;
201
202 return 0;
203 }
204
205 private:
206
207 rep value;
208 };
209
210 //***********************************************************************
212 //***********************************************************************
213 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
214 {
215 return (static_cast<unsigned>(y1) == static_cast<unsigned>(y2));
216 }
217
218 //***********************************************************************
220 //***********************************************************************
221 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
222 {
223 return !(y1 == y2);
224 }
225
226 //***********************************************************************
228 //***********************************************************************
229 inline ETL_CONSTEXPR14 bool operator <(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
230 {
231 return (static_cast<unsigned>(y1) < static_cast<unsigned>(y2));
232 }
233
234 //***********************************************************************
236 //***********************************************************************
237 inline ETL_CONSTEXPR14 bool operator <=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
238 {
239 return (static_cast<unsigned>(y1) <= static_cast<unsigned>(y2));
240 }
241
242 //***********************************************************************
244 //***********************************************************************
245 inline ETL_CONSTEXPR14 bool operator >(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
246 {
247 return (static_cast<unsigned>(y1) > static_cast<unsigned>(y2));
248 }
249
250 //***********************************************************************
252 //***********************************************************************
253 inline ETL_CONSTEXPR14 bool operator >=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
254 {
255 return (static_cast<unsigned>(y1) >= static_cast<unsigned>(y2));
256 }
257
258 //***********************************************************************
260 //***********************************************************************
261#if ETL_USING_CPP20
262 [[nodiscard]] inline constexpr auto operator <=>(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
263 {
264 return (static_cast<unsigned>(y1) <=> static_cast<unsigned>(y2));
265 }
266#endif
267
268 //***********************************************************************
271 //***********************************************************************
272 inline ETL_CONSTEXPR14 etl::chrono::year operator +(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT
273 {
274 etl::chrono::year result(y);
275
276 result += ys;
277
278 return result;
279 }
280
281 //***********************************************************************
284 //***********************************************************************
285 inline ETL_CONSTEXPR14 etl::chrono::year operator +(const etl::chrono::years& ys, const etl::chrono::year& y) ETL_NOEXCEPT
286 {
287 etl::chrono::year result(y);
288
289 result += ys;
290
291 return result;
292 }
293
294 //***********************************************************************
297 //***********************************************************************
298 inline ETL_CONSTEXPR14 etl::chrono::year operator -(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT
299 {
300 etl::chrono::year result(y);
301
302 result -= ys;
303
304 return result;
305 }
306
307 //***********************************************************************
310 //***********************************************************************
311 inline ETL_CONSTEXPR14 etl::chrono::year operator -(const etl::chrono::years& ys, const etl::chrono::year& y) ETL_NOEXCEPT
312 {
313 etl::chrono::year result(y);
314
315 result -= ys;
316
317 return result;
318 }
319
320 //***********************************************************************
323 //***********************************************************************
324 inline ETL_CONSTEXPR14 etl::chrono::years operator -(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
325 {
326 return etl::chrono::years(static_cast<int>(static_cast<unsigned>(y1)) -
327 static_cast<int>(static_cast<unsigned>(y2)));
328 }
329 }
330
331 //*************************************************************************
333 //*************************************************************************
334#if ETL_USING_8BIT_TYPES
335 template <>
336 struct hash<etl::chrono::year>
337 {
338 size_t operator()(const etl::chrono::year& y) const
339 {
340 etl::chrono::year::rep value = static_cast<etl::chrono::year::rep>(static_cast<unsigned>(y));
341 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
342
343 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
344 }
345 };
346#endif
347}
348
349#if ETL_HAS_CHRONO_LITERALS_YEAR
350namespace etl
351{
352 inline namespace literals
353 {
354 inline namespace chrono_literals
355 {
356 //***********************************************************************
358 //***********************************************************************
359#if ETL_USING_VERBOSE_CHRONO_LITERALS
360 inline ETL_CONSTEXPR14 etl::chrono::year operator ""_year(unsigned long long y) ETL_NOEXCEPT
361#else
362 inline ETL_CONSTEXPR14 etl::chrono::year operator ""_y(unsigned long long y) ETL_NOEXCEPT
363#endif
364 {
365 return etl::chrono::year(static_cast<int16_t>(y));
366 }
367 }
368 }
369}
370#endif
year
Definition year.h:43
ETL_NODISCARD ETL_CONSTEXPR14 bool is_leap() const ETL_NOEXCEPT
Returns true if the year is a leap year.
Definition year.h:175
ETL_CONSTEXPR year() ETL_NOEXCEPT
Default constructor.
Definition year.h:51
ETL_CONSTEXPR14 etl::chrono::year & operator=(const etl::chrono::year &rhs) ETL_NOEXCEPT
Assignment operator.
Definition year.h:75
ETL_CONSTEXPR14 etl::chrono::year & operator--() ETL_NOEXCEPT
Pre-decrement operator.
Definition year.h:106
ETL_CONSTEXPR14 year(const etl::chrono::year &other) ETL_NOEXCEPT
Copy constructor.
Definition year.h:67
ETL_CONSTEXPR14 etl::chrono::year & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition year.h:85
ETL_CONSTEXPR year(unsigned value_) ETL_NOEXCEPT
Construct from unsigned.
Definition year.h:59
ETL_CONSTEXPR14 etl::chrono::year & operator-=(const etl::chrono::years &ys) ETL_NOEXCEPT
Minus-equals operator subtracting etl::chrono::years.
Definition year.h:137
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year min() ETL_NOEXCEPT
The minimum year value for which ok() will return true.
Definition year.h:157
ETL_CONSTEXPR14 etl::chrono::year & operator+=(const etl::chrono::years &ys) ETL_NOEXCEPT
Plus-equals operator adding etl::chrono::years.
Definition year.h:127
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const year &other) const ETL_NOEXCEPT
Definition year.h:197
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the year is within the valid -32767 to 32767 range.
Definition year.h:148
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year max() ETL_NOEXCEPT
The maximum year value for which ok() will return true.
Definition year.h:166
Definition integral_limits.h:516
bitset_ext
Definition absolute.h:39