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(int value_) ETL_NOEXCEPT
60 : value(value_)
61 {
62 }
63
64 //***********************************************************************
66 //***********************************************************************
67 ETL_CONSTEXPR14 etl::chrono::year& operator++() ETL_NOEXCEPT
68 {
69 ++value;
70
71 return *this;
72 }
73
74 //***********************************************************************
76 //***********************************************************************
77 ETL_CONSTEXPR14 etl::chrono::year operator++(int) ETL_NOEXCEPT
78 {
79 etl::chrono::year temp = *this;
80 ++value;
81
82 return temp;
83 }
84
85 //***********************************************************************
87 //***********************************************************************
88 ETL_CONSTEXPR14 etl::chrono::year& operator--() ETL_NOEXCEPT
89 {
90 --value;
91
92 return *this;
93 }
94
95 //***********************************************************************
97 //***********************************************************************
98 ETL_CONSTEXPR14 etl::chrono::year operator--(int) ETL_NOEXCEPT
99 {
100 etl::chrono::year temp = *this;
101 --value;
102
103 return temp;
104 }
105
106 //***********************************************************************
108 //***********************************************************************
109 ETL_CONSTEXPR14 etl::chrono::year& operator+=(const etl::chrono::years& ys) ETL_NOEXCEPT
110 {
111 value += ys.count();
112
113 return *this;
114 }
115
116 //***********************************************************************
118 //***********************************************************************
119 ETL_CONSTEXPR14 etl::chrono::year& operator-=(const etl::chrono::years& ys) ETL_NOEXCEPT
120 {
121 value -= ys.count();
122
123 return *this;
124 }
125
126 //***********************************************************************
129 //***********************************************************************
130 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
131 {
132 return (value != etl::integral_limits<int16_t>::min);
133 }
134
135 //***********************************************************************
137 //***********************************************************************
138 ETL_NODISCARD
139 static ETL_CONSTEXPR14 etl::chrono::year min() ETL_NOEXCEPT
140 {
141 return etl::chrono::year(-32767);
142 }
143
144 //***********************************************************************
146 //***********************************************************************
147 ETL_NODISCARD
148 static ETL_CONSTEXPR14 etl::chrono::year max() ETL_NOEXCEPT
149 {
150 return etl::chrono::year(32767);
151 }
152
153 //***********************************************************************
155 //***********************************************************************
156 ETL_NODISCARD ETL_CONSTEXPR14 bool is_leap() const ETL_NOEXCEPT
157 {
158 return ((value % 4) == 0) && // Divisible by 4
159 (((value % 100) != 0) || // but not divisible by 100
160 ((value % 400) == 0)); // unless divisible by 400
161 }
162
163 //***********************************************************************
165 //***********************************************************************
166 ETL_CONSTEXPR14 /*explicit*/ operator int() const ETL_NOEXCEPT
167 {
168 return value;
169 }
170
171 //***********************************************************************
176 //***********************************************************************
177 ETL_NODISCARD ETL_CONSTEXPR14 int compare(const year& other) const ETL_NOEXCEPT
178 {
179 if (value < other.value)
180 return -1;
181 if (value > other.value)
182 return 1;
183
184 return 0;
185 }
186
187 private:
188
189 rep value;
190 };
191
192 //***********************************************************************
194 //***********************************************************************
195 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
196 {
197 return (static_cast<int>(y1) == static_cast<int>(y2));
198 }
199
200 //***********************************************************************
202 //***********************************************************************
203 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
204 {
205 return !(y1 == y2);
206 }
207
208 //***********************************************************************
210 //***********************************************************************
211 inline ETL_CONSTEXPR14 bool operator<(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
212 {
213 return (static_cast<int>(y1) < static_cast<int>(y2));
214 }
215
216 //***********************************************************************
218 //***********************************************************************
219 inline ETL_CONSTEXPR14 bool operator<=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
220 {
221 return (static_cast<int>(y1) <= static_cast<int>(y2));
222 }
223
224 //***********************************************************************
226 //***********************************************************************
227 inline ETL_CONSTEXPR14 bool operator>(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
228 {
229 return (static_cast<int>(y1) > static_cast<int>(y2));
230 }
231
232 //***********************************************************************
234 //***********************************************************************
235 inline ETL_CONSTEXPR14 bool operator>=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
236 {
237 return (static_cast<int>(y1) >= static_cast<int>(y2));
238 }
239
240 //***********************************************************************
242 //***********************************************************************
243#if ETL_USING_CPP20
244 [[nodiscard]]
245 inline constexpr auto operator<=>(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
246 {
247 return (static_cast<int>(y1) <=> static_cast<int>(y2));
248 }
249#endif
250
251 //***********************************************************************
254 //***********************************************************************
255 inline ETL_CONSTEXPR14 etl::chrono::year operator+(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT
256 {
257 etl::chrono::year result(y);
258
259 result += ys;
260
261 return result;
262 }
263
264 //***********************************************************************
267 //***********************************************************************
268 inline ETL_CONSTEXPR14 etl::chrono::year operator+(const etl::chrono::years& ys, const etl::chrono::year& y) ETL_NOEXCEPT
269 {
270 etl::chrono::year result(y);
271
272 result += ys;
273
274 return result;
275 }
276
277 //***********************************************************************
280 //***********************************************************************
281 inline ETL_CONSTEXPR14 etl::chrono::year operator-(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT
282 {
283 etl::chrono::year result(y);
284
285 result -= ys;
286
287 return result;
288 }
289
290 //***********************************************************************
293 //***********************************************************************
294 inline ETL_CONSTEXPR14 etl::chrono::years operator-(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
295 {
296 return etl::chrono::years(static_cast<int>(y1) - static_cast<int>(y2));
297 }
298 } // namespace chrono
299
300 //*************************************************************************
302 //*************************************************************************
303#if ETL_USING_8BIT_TYPES
304 template <>
305 struct hash<etl::chrono::year>
306 {
307 size_t operator()(const etl::chrono::year& y) const
308 {
309 etl::chrono::year::rep value = static_cast<etl::chrono::year::rep>(static_cast<int>(y));
310 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
311
312 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
313 }
314 };
315#endif
316} // namespace etl
317
318#if ETL_HAS_CHRONO_LITERALS_YEAR
319namespace etl
320{
321 inline namespace literals
322 {
323 inline namespace chrono_literals
324 {
325 //***********************************************************************
327 //***********************************************************************
328 #if ETL_USING_VERBOSE_CHRONO_LITERALS
329 inline ETL_CONSTEXPR14 etl::chrono::year operator""_year(unsigned long long y) ETL_NOEXCEPT
330 #else
331 inline ETL_CONSTEXPR14 etl::chrono::year operator""_y(unsigned long long y) ETL_NOEXCEPT
332 #endif
333 {
334 return etl::chrono::year(static_cast<int>(y));
335 }
336 } // namespace chrono_literals
337 } // namespace literals
338} // namespace etl
339#endif
year
Definition year.h:43
ETL_CONSTEXPR14 etl::chrono::year operator--(int) ETL_NOEXCEPT
Post-decrement operator.
Definition year.h:98
ETL_NODISCARD ETL_CONSTEXPR14 bool is_leap() const ETL_NOEXCEPT
Returns true if the year is a leap year.
Definition year.h:156
ETL_CONSTEXPR year() ETL_NOEXCEPT
Default constructor.
Definition year.h:51
ETL_CONSTEXPR14 etl::chrono::year & operator--() ETL_NOEXCEPT
Pre-decrement operator.
Definition year.h:88
ETL_CONSTEXPR year(int value_) ETL_NOEXCEPT
Construct from int.
Definition year.h:59
ETL_CONSTEXPR14 etl::chrono::year & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition year.h:67
ETL_CONSTEXPR14 etl::chrono::year & operator-=(const etl::chrono::years &ys) ETL_NOEXCEPT
Minus-equals operator subtracting etl::chrono::years.
Definition year.h:119
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year min() ETL_NOEXCEPT
The minimum year value for which ok() will return true.
Definition year.h:139
ETL_CONSTEXPR14 etl::chrono::year & operator+=(const etl::chrono::years &ys) ETL_NOEXCEPT
Plus-equals operator adding etl::chrono::years.
Definition year.h:109
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const year &other) const ETL_NOEXCEPT
Definition year.h:177
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Definition year.h:130
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year max() ETL_NOEXCEPT
The maximum year value for which ok() will return true.
Definition year.h:148
ETL_CONSTEXPR14 etl::chrono::year operator++(int) ETL_NOEXCEPT
Post-increment operator.
Definition year.h:77
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
Definition integral_limits.h:518
bitset_ext
Definition absolute.h:40