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_, const etl::chrono::day& d_) ETL_NOEXCEPT
52 : m(m_)
53 , d(d_)
54 {
55 }
56
57 //*************************************************************************
59 //*************************************************************************
60 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
61 {
62 return m;
63 }
64
65 //*************************************************************************
67 //*************************************************************************
68 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::day day() const ETL_NOEXCEPT
69 {
70 return d;
71 }
72
73 //*************************************************************************
75 //*************************************************************************
76 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
77 {
78 if (!m.ok() || !d.ok())
79 {
80 return false;
81 }
82
83 unsigned max_day = 0;
84
85 unsigned m_v = static_cast<unsigned>(m);
86 max_day = private_chrono::days_in_month[m_v];
87
88 return (max_day > 0) && (static_cast<unsigned>(d) >= 1) && (static_cast<unsigned>(d) <= max_day);
89 }
90
91 //***********************************************************************
98 //***********************************************************************
99 ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month_day& other) const ETL_NOEXCEPT
100 {
101 if (m < other.m)
102 return -1;
103 if (m > other.m)
104 return 1;
105 if (d < other.d)
106 return -1;
107 if (d > other.d)
108 return 1;
109
110 return 0;
111 }
112
113 private:
114
117 };
118
119 //*************************************************************************
121 //*************************************************************************
122 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::month_day& lhs, const etl::chrono::month_day& rhs) ETL_NOEXCEPT
123 {
124 return (lhs.day() == rhs.day()) && (lhs.month() == rhs.month());
125 }
126
127 //*************************************************************************
129 //*************************************************************************
130 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::month_day& lhs, const etl::chrono::month_day& rhs) ETL_NOEXCEPT
131 {
132 return !(lhs == rhs);
133 }
134
135 //*************************************************************************
137 //*************************************************************************
138 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator<(const etl::chrono::month_day& lhs, const etl::chrono::month_day& rhs) ETL_NOEXCEPT
139 {
140 if (lhs.month() < rhs.month())
141 {
142 return true;
143 }
144 else if (lhs.month() == rhs.month())
145 {
146 return lhs.day() < rhs.day();
147 }
148 else
149 {
150 return false;
151 }
152 }
153
154 //*************************************************************************
156 //*************************************************************************
157 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator<=(const etl::chrono::month_day& lhs, const etl::chrono::month_day& rhs) ETL_NOEXCEPT
158 {
159 return !(rhs < lhs);
160 }
161
162 //*************************************************************************
164 //*************************************************************************
165 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator>(const etl::chrono::month_day& lhs, const etl::chrono::month_day& rhs) ETL_NOEXCEPT
166 {
167 return rhs < lhs;
168 }
169
170 //*************************************************************************
172 //*************************************************************************
173 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator>=(const etl::chrono::month_day& lhs, const etl::chrono::month_day& rhs) ETL_NOEXCEPT
174 {
175 return !(lhs < rhs);
176 }
177
178 //***********************************************************************
180 //***********************************************************************
181#if ETL_USING_CPP20
182 [[nodiscard]]
183 inline constexpr auto operator<=>(const etl::chrono::month_day& lhs, const etl::chrono::month_day& rhs) ETL_NOEXCEPT
184 {
185 auto cmp = lhs.month() <=> rhs.month();
186
187 if (cmp != 0)
188 {
189 return cmp;
190 }
191 else
192 {
193 return lhs.day() <=> rhs.day();
194 }
195 }
196#endif
197
198 //*************************************************************************
200 //*************************************************************************
202 {
203 public:
204
205 //*************************************************************************
207 //*************************************************************************
208 ETL_CONSTEXPR14 explicit month_day_last(const etl::chrono::month& m_) ETL_NOEXCEPT
209 : m(m_)
210 {
211 }
212
213 //*************************************************************************
215 //*************************************************************************
216 ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
217 {
218 return m;
219 }
220
221 //*************************************************************************
223 //*************************************************************************
224 bool ok() const ETL_NOEXCEPT
225 {
226 return m.ok();
227 }
228
229 //***********************************************************************
234 //***********************************************************************
235 ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month_day& other) const ETL_NOEXCEPT
236 {
237 if (m < other.month())
238 return -1;
239 if (m > other.month())
240 return 1;
241
242 return 0;
243 }
244
245 private:
246
248 };
249
250 //***********************************************************************
252 //***********************************************************************
253 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::month_day_last& mdl1, const etl::chrono::month_day_last& mdl2) ETL_NOEXCEPT
254 {
255 return (static_cast<unsigned>(mdl1.month()) == static_cast<unsigned>(mdl2.month()));
256 }
257
258 //***********************************************************************
260 //***********************************************************************
261 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::month_day_last& mdl1, const etl::chrono::month_day_last& mdl2) ETL_NOEXCEPT
262 {
263 return !(mdl1 == mdl2);
264 }
265
266 //*************************************************************************
268 //*************************************************************************
269 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator<(const etl::chrono::month_day_last& lhs, const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
270 {
271 return (lhs.month() < rhs.month());
272 }
273
274 //*************************************************************************
276 //*************************************************************************
277 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator<=(const etl::chrono::month_day_last& lhs, const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
278 {
279 return !(rhs < lhs);
280 }
281
282 //*************************************************************************
284 //*************************************************************************
285 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator>(const etl::chrono::month_day_last& lhs, const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
286 {
287 return rhs < lhs;
288 }
289
290 //*************************************************************************
292 //*************************************************************************
293 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator>=(const etl::chrono::month_day_last& lhs, const etl::chrono::month_day_last& rhs) ETL_NOEXCEPT
294 {
295 return !(lhs < rhs);
296 }
297
298 //***********************************************************************
300 //***********************************************************************
301#if ETL_USING_CPP20
302 [[nodiscard]]
303 inline constexpr auto operator<=>(const etl::chrono::month_day_last& mdl1, const etl::chrono::month_day_last& mdl2) ETL_NOEXCEPT
304 {
305 return (static_cast<unsigned>(mdl1.month()) <=> static_cast<unsigned>(mdl2.month()));
306 }
307#endif
308 } // namespace chrono
309
310 //*************************************************************************
312 //*************************************************************************
313#if ETL_USING_8BIT_TYPES
314 template <>
315 struct hash<etl::chrono::month_day>
316 {
317 size_t operator()(const etl::chrono::month_day& md) const
318 {
319 etl::chrono::month::rep m = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(md.month()));
320 etl::chrono::day::rep d = static_cast<etl::chrono::day::rep>(static_cast<unsigned>(md.day()));
321
322 uint8_t buffer[sizeof(m) + sizeof(d)];
323
324 memcpy(buffer, &m, sizeof(m));
325 memcpy(buffer + sizeof(m), &d, sizeof(d));
326
327 return etl::private_hash::generic_hash<size_t>(buffer, buffer + sizeof(m) + sizeof(d));
328 }
329 };
330#endif
331
332 //*************************************************************************
334 //*************************************************************************
335#if ETL_USING_8BIT_TYPES
336 template <>
337 struct hash<etl::chrono::month_day_last>
338 {
339 size_t operator()(const etl::chrono::month_day_last& mdl) const
340 {
341 etl::chrono::month::rep value = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(mdl.month()));
342 const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
343
344 return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
345 }
346 };
347#endif
348} // namespace etl
day
Definition day.h:43
Spaceship operator.
Definition month_day.h:202
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const month_day &other) const ETL_NOEXCEPT
Definition month_day.h:235
ETL_CONSTEXPR14 month_day_last(const etl::chrono::month &m_) ETL_NOEXCEPT
Construct from month.
Definition month_day.h:208
ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
Get the month.
Definition month_day.h:216
bool ok() const ETL_NOEXCEPT
Is the contained month OK?
Definition month_day.h:224
Definition month_day.h:40
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
Returns the month.
Definition month_day.h:60
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::day day() const ETL_NOEXCEPT
Returns the day.
Definition month_day.h:68
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the month/day is valid.
Definition month_day.h:76
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:99
month_day()=default
Default constructor.
month
Definition month.h:54
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