Embedded Template Library 1.0
Loading...
Searching...
No Matches
month_day.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 {
40 {
41 public:
42
43 //*************************************************************************
45 //*************************************************************************
46 month_day() = default;
47
48 //*************************************************************************
50 //*************************************************************************
51 ETL_CONSTEXPR14 month_day(const etl::chrono::month& m_,
52 const etl::chrono::day& d_) ETL_NOEXCEPT
53 : m(m_)
54 , d(d_)
55 {
56 }
57
58 //*************************************************************************
60 //*************************************************************************
61 ETL_NODISCARD
62 ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
63 {
64 return m;
65 }
66
67 //*************************************************************************
69 //*************************************************************************
70 ETL_NODISCARD
71 ETL_CONSTEXPR14 etl::chrono::day day() const ETL_NOEXCEPT
72 {
73 return d;
74 }
75
76 //*************************************************************************
78 //*************************************************************************
79 ETL_NODISCARD
80 ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
81 {
82 if (!m.ok() || !d.ok())
83 {
84 return false;
85 }
86
87 unsigned max_day = 0;
88
89 unsigned m_v = static_cast<unsigned>(m);
90 max_day = private_chrono::days_in_month[m_v];
91
92 return (max_day > 0) &&
93 (static_cast<unsigned>(d) >= 1) &&
94 (static_cast<unsigned>(d) <= max_day);
95 }
96
97 //***********************************************************************
104 //***********************************************************************
105 ETL_NODISCARD
106 ETL_CONSTEXPR14 int compare(const month_day& other) const ETL_NOEXCEPT
107 {
108 if (m < other.m) return -1;
109 if (m > other.m) return 1;
110 if (d < other.d) return -1;
111 if (d > other.d) return 1;
112
113 return 0;
114 }
115
116 private:
117
120 };
121
122 //*************************************************************************
124 //*************************************************************************
125 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::month_day& lhs,
126 const etl::chrono::month_day& rhs) ETL_NOEXCEPT
127 {
128 return (lhs.day() == rhs.day()) && (lhs.month() == rhs.month());
129 }
130
131 //*************************************************************************
133 //*************************************************************************
134 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::month_day& lhs,
135 const etl::chrono::month_day& rhs) ETL_NOEXCEPT
136 {
137 return !(lhs == rhs);
138 }
139
140 //*************************************************************************
142 //*************************************************************************
143 ETL_NODISCARD ETL_CONSTEXPR14
144 inline bool operator <(const etl::chrono::month_day& lhs,
145 const etl::chrono::month_day& rhs) ETL_NOEXCEPT
146 {
147 if (lhs.month() < rhs.month())
148 {
149 return true;
150 }
151 else if (lhs.month() == rhs.month())
152 {
153 return lhs.day() < rhs.day();
154 }
155 else
156 {
157 return false;
158 }
159 }
160
161 //*************************************************************************
163 //*************************************************************************
164 ETL_NODISCARD ETL_CONSTEXPR14
165 inline bool operator <=(const etl::chrono::month_day& lhs,
166 const etl::chrono::month_day& rhs) ETL_NOEXCEPT
167 {
168 return !(rhs < lhs);
169 }
170
171 //*************************************************************************
173 //*************************************************************************
174 ETL_NODISCARD ETL_CONSTEXPR14
175 inline bool operator >(const etl::chrono::month_day& lhs,
176 const etl::chrono::month_day& rhs) ETL_NOEXCEPT
177 {
178 return rhs < lhs;
179 }
180
181 //*************************************************************************
183 //*************************************************************************
184 ETL_NODISCARD ETL_CONSTEXPR14
185 inline bool operator >=(const etl::chrono::month_day& lhs,
186 const etl::chrono::month_day& rhs) ETL_NOEXCEPT
187 {
188 return !(lhs < rhs);
189 }
190
191 //***********************************************************************
193 //***********************************************************************
194#if ETL_USING_CPP20
195 [[nodiscard]] inline constexpr auto operator <=>(const etl::chrono::month_day& lhs,
196 const etl::chrono::month_day& rhs) ETL_NOEXCEPT
197 {
198 auto cmp = lhs.month() <=> rhs.month();
199
200 if (cmp != 0)
201 {
202 return cmp;
203 }
204 else
205 {
206 return lhs.day() <=> rhs.day();
207 }
208 }
209#endif
210
211 //*************************************************************************
213 //*************************************************************************
215 {
216 public:
217
218 //*************************************************************************
220 //*************************************************************************
221 ETL_CONSTEXPR14 explicit month_day_last(const etl::chrono::month& m_) ETL_NOEXCEPT
222 : m(m_)
223 {
224 }
225
226 //*************************************************************************
228 //*************************************************************************
229 ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
230 {
231 return m;
232 }
233
234 //*************************************************************************
236 //*************************************************************************
237 bool ok() const ETL_NOEXCEPT
238 {
239 return m.ok();
240 }
241
242 //***********************************************************************
247 //***********************************************************************
248 ETL_NODISCARD
249 ETL_CONSTEXPR14 int compare(const month_day& other) const ETL_NOEXCEPT
250 {
251 if (m < other.month()) return -1;
252 if (m > other.month()) return 1;
253
254 return 0;
255 }
256
257 private:
258
260 };
261
262 //***********************************************************************
264 //***********************************************************************
265 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::month_day_last& mdl1,
266 const etl::chrono::month_day_last& mdl2) ETL_NOEXCEPT
267 {
268 return (static_cast<unsigned>(mdl1.month()) == static_cast<unsigned>(mdl2.month()));
269 }
270
271 //***********************************************************************
273 //***********************************************************************
274 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::month_day_last& mdl1,
275 const etl::chrono::month_day_last& mdl2) ETL_NOEXCEPT
276 {
277 return !(mdl1 == mdl2);
278 }
279
280 //*************************************************************************
282 //*************************************************************************
283 ETL_NODISCARD ETL_CONSTEXPR14
284 inline bool operator <(const etl::chrono::month_day_last& lhs,
285 const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
286 {
287 return (lhs.month() < rhs.month());
288 }
289
290 //*************************************************************************
292 //*************************************************************************
293 ETL_NODISCARD ETL_CONSTEXPR14
294 inline bool operator <=(const etl::chrono::month_day_last& lhs,
295 const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
296 {
297 return !(rhs < lhs);
298 }
299
300 //*************************************************************************
302 //*************************************************************************
303 ETL_NODISCARD ETL_CONSTEXPR14
304 inline bool operator >(const etl::chrono::month_day_last& lhs,
305 const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
306 {
307 return rhs < lhs;
308 }
309
310 //*************************************************************************
312 //*************************************************************************
313 ETL_NODISCARD ETL_CONSTEXPR14
314 inline bool operator >=(const etl::chrono::month_day_last& lhs,
315 const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
316 {
317 return !(lhs < rhs);
318 }
319
320 //***********************************************************************
322 //***********************************************************************
323#if ETL_USING_CPP20
324 [[nodiscard]] inline constexpr auto operator <=>(const etl::chrono::month_day_last& mdl1, const etl::chrono::month_day_last& mdl2) ETL_NOEXCEPT
325 {
326 return (static_cast<unsigned>(mdl1.month()) <=> static_cast<unsigned>(mdl2.month()));
327 }
328#endif
329 }
330
331 //*************************************************************************
333 //*************************************************************************
334#if ETL_USING_8BIT_TYPES
335 template <>
336 struct hash<etl::chrono::month_day>
337 {
338 size_t operator()(const etl::chrono::month_day& md) const
339 {
340 etl::chrono::month::rep m = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(md.month()));
341 etl::chrono::day::rep d = static_cast<etl::chrono::day::rep>(static_cast<unsigned>(md.day()));
342
343 uint8_t buffer[sizeof(m) + sizeof(d)];
344
345 memcpy(buffer, &m, sizeof(m));
346 memcpy(buffer + sizeof(m), &d, sizeof(d));
347
348 return etl::private_hash::generic_hash<size_t>(buffer, buffer + sizeof(m) + sizeof(d));
349 }
350 };
351#endif
352
353 //*************************************************************************
355 //*************************************************************************
356#if ETL_USING_8BIT_TYPES
357 template <>
358 struct hash<etl::chrono::month_day_last>
359 {
360 size_t operator()(const etl::chrono::month_day_last& mdl) const
361 {
362 etl::chrono::month::rep value = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(mdl.month()));
363 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
364
365 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
366 }
367 };
368#endif
369}
370
day
Definition day.h:43
Spaceship operator.
Definition month_day.h:215
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month_day &other) const ETL_NOEXCEPT
Definition month_day.h:249
ETL_CONSTEXPR14 month_day_last(const etl::chrono::month &m_) ETL_NOEXCEPT
Construct from month.
Definition month_day.h:221
ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
Get the month.
Definition month_day.h:229
bool ok() const ETL_NOEXCEPT
Is the contained month OK?
Definition month_day.h:237
Definition month_day.h:40
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
Returns the month.
Definition month_day.h:62
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::day day() const ETL_NOEXCEPT
Returns the day.
Definition month_day.h:71
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the month/day is valid.
Definition month_day.h:80
ETL_CONSTEXPR14 month_day(const etl::chrono::month &m_, const etl::chrono::day &d_) ETL_NOEXCEPT
Construct from month and day.
Definition month_day.h:51
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month_day &other) const ETL_NOEXCEPT
Definition month_day.h:106
month_day()=default
Default constructor.
month
Definition month.h:54
bitset_ext
Definition absolute.h:39