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_, const etl::chrono::month& m_) ETL_NOEXCEPT
56 : y(y_)
57 , m(m_)
58 {
59 }
60
61 //*************************************************************************
63 //*************************************************************************
64 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year year() const ETL_NOEXCEPT
65 {
66 return y;
67 }
68
69 //*************************************************************************
71 //*************************************************************************
72 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::month month() const ETL_NOEXCEPT
73 {
74 return m;
75 }
76
77 //*************************************************************************
79 //*************************************************************************
80 ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
81 {
82 return y.ok() && m.ok();
83 }
84
85 //***********************************************************************
92 //***********************************************************************
93 ETL_NODISCARD ETL_CONSTEXPR14 int compare(const year_month& other) const ETL_NOEXCEPT
94 {
95 if (y < other.y)
96 return -1;
97 if (y > other.y)
98 return 1;
99 if (m < other.m)
100 return -1;
101 if (m > other.m)
102 return 1;
103
104 return 0;
105 }
106
107 private:
108
111 };
112
113 //*************************************************************************
115 //*************************************************************************
116 inline ETL_CONSTEXPR14 etl::chrono::year_month operator+(const etl::chrono::year_month& ym, const etl::chrono::years& dy) ETL_NOEXCEPT
117 {
118 return etl::chrono::year_month(ym.year() + dy, ym.month());
119 }
120
121 //*************************************************************************
123 //*************************************************************************
124 inline ETL_CONSTEXPR14 etl::chrono::year_month operator+(const etl::chrono::years& dy, const etl::chrono::year_month& ym) ETL_NOEXCEPT
125 {
126 return etl::chrono::year_month(ym.year() + dy, ym.month());
127 }
128
129 //*************************************************************************
131 //*************************************************************************
132 inline ETL_CONSTEXPR14 etl::chrono::year_month operator+(const etl::chrono::year_month& ym, const etl::chrono::months& dm) ETL_NOEXCEPT
133 {
134 int dmonths = static_cast<int>(static_cast<unsigned>(ym.month())) - 1 + dm.count();
135 int dyears = (dmonths - 11 * (dmonths < 0)) / 12;
136 dmonths -= dyears * 12;
137 ++dmonths;
138 return etl::chrono::year_month((ym.year() + etl::chrono::years(dyears)), etl::chrono::month(static_cast<unsigned>(dmonths)));
139 }
140
141 //*************************************************************************
143 //*************************************************************************
144 inline ETL_CONSTEXPR14 etl::chrono::year_month operator+(const etl::chrono::months& dm, const etl::chrono::year_month& ym) ETL_NOEXCEPT
145 {
146 return ym + dm;
147 }
148
149 //*************************************************************************
151 //*************************************************************************
152 inline ETL_CONSTEXPR14 etl::chrono::year_month operator-(const etl::chrono::year_month& ym, const etl::chrono::years& dy) ETL_NOEXCEPT
153 {
154 return etl::chrono::year_month(ym.year() - dy, ym.month());
155 }
156
157 //*************************************************************************
159 //*************************************************************************
160 inline ETL_CONSTEXPR14 etl::chrono::year_month operator-(const etl::chrono::year_month& ym, const etl::chrono::months& dm) ETL_NOEXCEPT
161 {
162 return ym + -dm;
163 }
164
165 //*************************************************************************
167 //*************************************************************************
168 inline ETL_CONSTEXPR14 etl::chrono::months operator-(const etl::chrono::year_month& ym1, const etl::chrono::year_month& ym2) ETL_NOEXCEPT
169 {
170 return etl::chrono::months(
171 (ym1.year() - ym2.year())
172 + etl::chrono::months(static_cast<int>(static_cast<unsigned>(ym1.month())) - static_cast<int>(static_cast<unsigned>(ym2.month()))));
173 }
174
175 //*************************************************************************
177 //*************************************************************************
178 inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::year_month& lhs, const etl::chrono::year_month& rhs) ETL_NOEXCEPT
179 {
180 return (lhs.year() == rhs.year()) && (lhs.month() == rhs.month());
181 }
182
183 //*************************************************************************
185 //*************************************************************************
186 inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::year_month& lhs, const etl::chrono::year_month& rhs) ETL_NOEXCEPT
187 {
188 return !(lhs == rhs);
189 }
190
191 //*************************************************************************
193 //*************************************************************************
194 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator<(const etl::chrono::year_month& lhs, const etl::chrono::year_month& rhs) ETL_NOEXCEPT
195 {
196 if (lhs.year() < rhs.year())
197 {
198 return true;
199 }
200 else if (lhs.year() == rhs.year())
201 {
202 return lhs.month() < rhs.month();
203 }
204 else
205 {
206 return false;
207 }
208 }
209
210 //*************************************************************************
212 //*************************************************************************
213 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator<=(const etl::chrono::year_month& lhs, const etl::chrono::year_month& rhs) ETL_NOEXCEPT
214 {
215 return !(rhs < lhs);
216 }
217
218 //*************************************************************************
220 //*************************************************************************
221 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator>(const etl::chrono::year_month& lhs, const etl::chrono::year_month& rhs) ETL_NOEXCEPT
222 {
223 return rhs < lhs;
224 }
225
226 //*************************************************************************
228 //*************************************************************************
229 ETL_NODISCARD ETL_CONSTEXPR14 inline bool operator>=(const etl::chrono::year_month& lhs, const etl::chrono::year_month& rhs) ETL_NOEXCEPT
230 {
231 return !(lhs < rhs);
232 }
233
234 //***********************************************************************
236 //***********************************************************************
237#if ETL_USING_CPP20
238 [[nodiscard]]
239 inline constexpr auto operator<=>(const etl::chrono::year_month& lhs, const etl::chrono::year_month& rhs) ETL_NOEXCEPT
240 {
241 auto cmp = lhs.year() <=> rhs.year();
242
243 if (cmp != 0)
244 {
245 return cmp;
246 }
247 else
248 {
249 return lhs.month() <=> rhs.month();
250 }
251 }
252#endif
253 } // namespace chrono
254
255 //*************************************************************************
257 //*************************************************************************
258#if ETL_USING_8BIT_TYPES
259 template <>
260 struct hash<etl::chrono::year_month>
261 {
262 size_t operator()(const etl::chrono::year_month& ym) const
263 {
264 etl::chrono::year::rep y = static_cast<etl::chrono::year::rep>(static_cast<int>(ym.year()));
265 etl::chrono::month::rep m = static_cast<etl::chrono::month::rep>(static_cast<unsigned>(ym.month()));
266
267 uint8_t buffer[sizeof(y) + sizeof(m)];
268
269 memcpy(buffer, &y, sizeof(y));
270 memcpy(buffer + sizeof(y), &m, sizeof(m));
271
272 return etl::private_hash::generic_hash<size_t>(buffer, buffer + sizeof(y) + sizeof(m));
273 }
274 };
275#endif
276} // namespace etl
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:72
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year year() const ETL_NOEXCEPT
Returns the year.
Definition year_month.h:64
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Returns true if the month/day is valid.
Definition year_month.h:80
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:93
year
Definition year.h:43
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
bitset_ext
Definition absolute.h:40