Embedded Template Library 1.0
Loading...
Searching...
No Matches
year_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 {
40 {
41 public:
42
43 //*************************************************************************
45 //*************************************************************************
46 ETL_CONSTEXPR year_month()
47 : y()
48 , m()
49 {
50 }
51
52 //*************************************************************************
54 //*************************************************************************
55 ETL_CONSTEXPR14 year_month(const etl::chrono::year& y_,
56 const etl::chrono::month& m_) ETL_NOEXCEPT
57 : y(y_)
58 , m(m_)
59 {
60 }
61
62 //*************************************************************************
64 //*************************************************************************
65 ETL_NODISCARD
66 ETL_CONSTEXPR14 etl::chrono::year year() const ETL_NOEXCEPT
67 {
68 return y;
69 }
70
71 //*************************************************************************
73 //*************************************************************************
74 ETL_NODISCARD
75 ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
76 {
77 return m;
78 }
79
80 //*************************************************************************
82 //*************************************************************************
83 ETL_NODISCARD
84 ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
85 {
86 return y.ok() && m.ok();
87 }
88
89 //***********************************************************************
96 //***********************************************************************
97 ETL_NODISCARD
98 ETL_CONSTEXPR14 int compare(const year_month& other) const ETL_NOEXCEPT
99 {
100 if (y < other.y) return -1;
101 if (y > other.y) return 1;
102 if (m < other.m) return -1;
103 if (m > other.m) return 1;
104
105 return 0;
106 }
107
108 private:
109
112 };
113
114 //*************************************************************************
116 //*************************************************************************
117 inline ETL_CONSTEXPR14 etl::chrono::year_month operator +(const etl::chrono::year_month& ym,
118 const etl::chrono::years& dy) ETL_NOEXCEPT
119 {
120 return etl::chrono::year_month(ym.year() + dy, ym.month());
121 }
122
123 //*************************************************************************
125 //*************************************************************************
126 inline ETL_CONSTEXPR14 etl::chrono::year_month operator +(const etl::chrono::years& dy,
127 const etl::chrono::year_month& ym) ETL_NOEXCEPT
128 {
129 return etl::chrono::year_month(ym.year() + dy, ym.month());
130 }
131
132 //*************************************************************************
134 //*************************************************************************
135 inline ETL_CONSTEXPR14 etl::chrono::year_month operator +(const etl::chrono::year_month& ym,
136 const etl::chrono::months& dm) ETL_NOEXCEPT
137 {
138 return etl::chrono::year_month(ym.year(), ym.month() + dm);
139 }
140
141 //*************************************************************************
143 //*************************************************************************
144 inline ETL_CONSTEXPR14 etl::chrono::year_month operator +(const etl::chrono::months& dm,
145 const etl::chrono::year_month& ym) ETL_NOEXCEPT
146 {
147 return etl::chrono::year_month(ym.year(), ym.month() + dm);
148 }
149
150 //*************************************************************************
152 //*************************************************************************
153 inline ETL_CONSTEXPR14 etl::chrono::year_month operator -(const etl::chrono::year_month& ym,
154 const etl::chrono::years& dy) ETL_NOEXCEPT
155 {
156 return etl::chrono::year_month(ym.year() - dy, ym.month());
157 }
158
159 //*************************************************************************
161 //*************************************************************************
162 inline ETL_CONSTEXPR14 etl::chrono::year_month operator -(const etl::chrono::year_month& ym,
163 const etl::chrono::months& dm) ETL_NOEXCEPT
164 {
165 return etl::chrono::year_month(ym.year(), ym.month() - dm);
166 }
167
168 //*************************************************************************
170 //*************************************************************************
171 inline ETL_CONSTEXPR14 etl::chrono::months operator -(const etl::chrono::year_month& ym1,
172 const etl::chrono::year_month& ym2) ETL_NOEXCEPT
173 {
174 return etl::chrono::months(static_cast<int>(((int(ym1.year()) - int(ym2.year())) * 12) + (unsigned(ym1.month()) - unsigned(ym2.month()))));
175 }
176
177 //*************************************************************************
179 //*************************************************************************
180 inline ETL_CONSTEXPR14 bool operator ==(const etl::chrono::year_month& lhs,
181 const etl::chrono::year_month& rhs) ETL_NOEXCEPT
182 {
183 return (lhs.year() == rhs.year()) && (lhs.month() == rhs.month());
184 }
185
186 //*************************************************************************
188 //*************************************************************************
189 inline ETL_CONSTEXPR14 bool operator !=(const etl::chrono::year_month& lhs,
190 const etl::chrono::year_month& rhs) ETL_NOEXCEPT
191 {
192 return !(lhs == rhs);
193 }
194
195 //*************************************************************************
197 //*************************************************************************
198 ETL_NODISCARD ETL_CONSTEXPR14
199 inline bool operator <(const etl::chrono::year_month& lhs,
200 const etl::chrono::year_month& rhs) ETL_NOEXCEPT
201 {
202 if (lhs.year() < rhs.year())
203 {
204 return true;
205 }
206 else if (lhs.year() == rhs.year())
207 {
208 return lhs.month() < rhs.month();
209 }
210 else
211 {
212 return false;
213 }
214 }
215
216 //*************************************************************************
218 //*************************************************************************
219 ETL_NODISCARD ETL_CONSTEXPR14
220 inline bool operator <=(const etl::chrono::year_month& lhs,
221 const etl::chrono::year_month& rhs) ETL_NOEXCEPT
222 {
223 return !(rhs < lhs);
224 }
225
226 //*************************************************************************
228 //*************************************************************************
229 ETL_NODISCARD ETL_CONSTEXPR14
230 inline bool operator >(const etl::chrono::year_month& lhs,
231 const etl::chrono::year_month& rhs) ETL_NOEXCEPT
232 {
233 return rhs < lhs;
234 }
235
236 //*************************************************************************
238 //*************************************************************************
239 ETL_NODISCARD ETL_CONSTEXPR14
240 inline bool operator >=(const etl::chrono::year_month& lhs,
241 const etl::chrono::year_month& rhs) ETL_NOEXCEPT
242 {
243 return !(lhs < rhs);
244 }
245
246 //***********************************************************************
248 //***********************************************************************
249#if ETL_USING_CPP20
250 [[nodiscard]] inline constexpr auto operator <=>(const etl::chrono::year_month& lhs,
251 const etl::chrono::year_month& rhs) ETL_NOEXCEPT
252 {
253 auto cmp = lhs.year()<=> rhs.year();
254
255 if (cmp != 0)
256 {
257 return cmp;
258 }
259 else
260 {
261 return lhs.month() <=> rhs.month();
262 }
263 }
264#endif
265 }
266
267 //*************************************************************************
269 //*************************************************************************
270#if ETL_USING_8BIT_TYPES
271 template <>
272 struct hash<etl::chrono::year_month>
273 {
274 size_t operator()(const etl::chrono::year_month& ym) const
275 {
276 etl::chrono::year::rep y = static_cast<etl::chrono::year::rep>(static_cast<unsigned>(ym.year()));
277 etl::chrono::month::rep m = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(ym.month()));
278
279 uint8_t buffer[sizeof(y) + sizeof(m)];
280
281 memcpy(buffer, &y, sizeof(y));
282 memcpy(buffer + sizeof(y), &m, sizeof(m));
283
284 return etl::private_hash::generic_hash<size_t>(buffer, buffer + sizeof(y) + sizeof(m));
285 }
286 };
287#endif
288}
289
month
Definition month.h:54
Definition year_month.h:40
ETL_CONSTEXPR year_month()
Default constructor.
Definition year_month.h:46
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
Returns the month.
Definition year_month.h:75
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year year() const ETL_NOEXCEPT
Returns the year.
Definition year_month.h:66
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the month/day is valid.
Definition year_month.h:84
ETL_CONSTEXPR14 year_month(const etl::chrono::year &y_, const etl::chrono::month &m_) ETL_NOEXCEPT
Construct from month and day.
Definition year_month.h:55
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const year_month &other) const ETL_NOEXCEPT
Definition year_month.h:98
year
Definition year.h:43
bitset_ext
Definition absolute.h:39