Embedded Template Library 1.0
Loading...
Searching...
No Matches
time_point.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) 2025 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 //***************************************************************************
42 //***************************************************************************
43 template <typename TClock, typename TDuration = typename TClock::duration>
45 {
46 public:
47
48 using clock = TClock;
49 using duration = TDuration;
50 using rep = typename TDuration::rep;
51 using period = typename TDuration::period;
52
53 //***************************************************************************
55 //***************************************************************************
56 ETL_CONSTEXPR time_point() ETL_NOEXCEPT
57 : dur(duration::zero())
58 {
59 }
60
61 //***************************************************************************
63 //***************************************************************************
64 ETL_CONSTEXPR14 explicit time_point(const duration& dur_) ETL_NOEXCEPT
65 : dur(dur_)
66 {
67 }
68
69 //***************************************************************************
71 //***************************************************************************
72 ETL_CONSTEXPR14 time_point(const time_point& rhs) ETL_NOEXCEPT
73 : dur(rhs.dur)
74 {
75 }
76
77 //***************************************************************************
79 //***************************************************************************
80 template <typename TDuration2>
81 ETL_CONSTEXPR14 explicit time_point(const time_point<clock, TDuration2>& rhs) ETL_NOEXCEPT
82 : dur(rhs.time_since_epoch())
83 {
84 }
85
86 //***************************************************************************
88 //***************************************************************************
89 ETL_CONSTEXPR14 time_point& operator=(const time_point& rhs) ETL_NOEXCEPT
90 {
91 dur = rhs.dur;
92
93 return *this;
94 }
95
96 //***************************************************************************
99 //***************************************************************************
100 ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
101 {
102 return dur;
103 }
104
105 //***************************************************************************
107 //***************************************************************************
108 ETL_CONSTEXPR14 time_point& operator+=(const duration& rhs) ETL_NOEXCEPT
109 {
110 dur += rhs;
111
112 return *this;
113 }
114
115 //***************************************************************************
117 //***************************************************************************
118 ETL_CONSTEXPR14 time_point& operator-=(const duration& rhs) ETL_NOEXCEPT
119 {
120 dur -= rhs;
121
122 return *this;
123 }
124
125 //***************************************************************************
127 //***************************************************************************
128 ETL_CONSTEXPR14 time_point& operator++() ETL_NOEXCEPT
129 {
130 ++dur;
131
132 return *this;
133 }
134
135 //***************************************************************************
137 //***************************************************************************
138 ETL_CONSTEXPR14 time_point operator++(int) ETL_NOEXCEPT
139 {
140 time_point temp(*this);
141 ++dur;
142
143 return temp;
144 }
145
146 //***************************************************************************
148 //***************************************************************************
149 ETL_CONSTEXPR14 time_point& operator--() ETL_NOEXCEPT
150 {
151 --dur;
152
153 return *this;
154 }
155
156 //***************************************************************************
158 //***************************************************************************
159 ETL_CONSTEXPR14 time_point operator--(int) ETL_NOEXCEPT
160 {
161 time_point temp(*this);
162 --dur;
163
164 return temp;
165 }
166
167 //***************************************************************************
169 //***************************************************************************
170 ETL_NODISCARD
171 static ETL_CONSTEXPR14 time_point min() ETL_NOEXCEPT
172 {
173 return time_point(duration::min());
174 }
175
176 //***************************************************************************
178 //***************************************************************************
179 ETL_NODISCARD
180 static ETL_CONSTEXPR14 time_point max() ETL_NOEXCEPT
181 {
182 return time_point(duration::max());
183 }
184
185 //***********************************************************************
190 //***********************************************************************
191 ETL_NODISCARD ETL_CONSTEXPR14 int compare(const time_point& other) const ETL_NOEXCEPT
192 {
193 if (dur < other.dur)
194 return -1;
195 if (dur > other.dur)
196 return 1;
197
198 return 0;
199 }
200
201 private:
202
203 duration dur;
204 };
205
206 //***********************************************************************
208 //***********************************************************************
209 template <typename TToDuration, typename TClock, typename TDuration>
211 ETL_NOEXCEPT
212 {
214 }
215
216 //***********************************************************************
218 //***********************************************************************
219 template <typename TToDuration, typename TClock, typename TDuration>
221 {
223 }
224
225 //***********************************************************************
228 //***********************************************************************
229 template <typename TToDuration, typename TClock, typename TDuration>
231 ETL_NOEXCEPT
232 {
234 }
235
236 template <typename TToDuration, typename TClock, typename TDuration>
237 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point<TClock, TToDuration> time_point_cast(const etl::chrono::time_point<TClock, TDuration>& tp)
238 ETL_NOEXCEPT
239 {
240 TToDuration dur = etl::chrono::duration_cast<TToDuration>(tp.time_since_epoch());
241
243 }
244
245 //***************************************************************************
248 //***************************************************************************
249 template <typename TClock, typename TDuration1, typename TRep2, typename TPeriod2>
252 {
253 using common_duration = typename etl::common_type<TDuration1, etl::chrono::duration<TRep2, TPeriod2> >::type;
254 using result_time_point = etl::chrono::time_point<TClock, common_duration>;
255
256 return result_time_point(lhs.time_since_epoch() + rhs);
257 }
258
259 //***************************************************************************
262 //***************************************************************************
263 template <typename TRep1, typename TPeriod1, typename TClock, typename TDuration2>
266 {
267 using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, TDuration2>::type;
268 using result_time_point = etl::chrono::time_point<TClock, common_duration>;
269
270 return result_time_point(lhs + rhs.time_since_epoch());
271 }
272
273 //***************************************************************************
276 //***************************************************************************
277 template <typename TClock, typename TDuration1, typename TRep2, typename TPeriod2>
280 {
281 using common_duration = typename etl::common_type<TDuration1, etl::chrono::duration<TRep2, TPeriod2> >::type;
282 using result_time_point = etl::chrono::time_point<TClock, common_duration>;
283
284 return result_time_point(lhs.time_since_epoch() - rhs);
285 }
286
287 //***************************************************************************
290 //***************************************************************************
291 template <typename TClock, typename TDuration1, typename TDuration2>
292 ETL_CONSTEXPR14 typename etl::common_type<TDuration1, TDuration2>::type operator-(const time_point<TClock, TDuration1>& lhs,
293 const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
294 {
295 return lhs.time_since_epoch() - rhs.time_since_epoch();
296 }
297
298 //***************************************************************************
300 //***************************************************************************
301 template <typename TClock, typename TDuration1, typename TDuration2>
302 ETL_CONSTEXPR14 bool operator==(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
303 {
304 return lhs.time_since_epoch() == rhs.time_since_epoch();
305 }
306
307 //***************************************************************************
309 //***************************************************************************
310 template <typename TClock, typename TDuration1, typename TDuration2>
311 ETL_CONSTEXPR14 bool operator!=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
312 {
313 return !(lhs == rhs);
314 }
315
316 //***************************************************************************
318 //***************************************************************************
319 template <typename TClock, typename TDuration1, typename TDuration2>
320 ETL_CONSTEXPR14 bool operator<(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
321 {
322 return lhs.time_since_epoch() < rhs.time_since_epoch();
323 }
324
325 //***************************************************************************
327 //***************************************************************************
328 template <typename TClock, typename TDuration1, typename TDuration2>
329 ETL_CONSTEXPR14 bool operator<=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
330 {
331 return !(rhs < lhs);
332 }
333
334 //***************************************************************************
336 //***************************************************************************
337 template <typename TClock, typename TDuration1, typename TDuration2>
338 ETL_CONSTEXPR14 bool operator>(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
339 {
340 return rhs < lhs;
341 }
342
343 //***************************************************************************
345 //***************************************************************************
346 template <typename TClock, typename TDuration1, typename TDuration2>
347 ETL_CONSTEXPR14 bool operator>=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
348 {
349 return !(lhs < rhs);
350 }
351 } // namespace chrono
352
353 //***********************************************************************
355 //***********************************************************************
356#if ETL_USING_CPP20
357 template <typename TClock, typename TDuration1, typename TDuration2>
358 [[nodiscard]]
360 ETL_NOEXCEPT
361 {
362 return (lhs.time_since_epoch() <=> rhs.time_since_epoch());
363 }
364#endif
365
366 //***************************************************************************
368 //***************************************************************************
369 template <typename TClock, typename TDuration1, typename TDuration2>
370 struct common_type<etl::chrono::time_point<TClock, TDuration1>, etl::chrono::time_point<TClock, TDuration2>>
371 {
373 };
374} // namespace etl
duration
Definition duration.h:108
Definition time_point.h:45
ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
Definition time_point.h:100
ETL_CONSTEXPR14 time_point(const duration &dur_) ETL_NOEXCEPT
Construct from a duration.
Definition time_point.h:64
ETL_CONSTEXPR14 time_point & operator=(const time_point &rhs) ETL_NOEXCEPT
Assignment operator.
Definition time_point.h:89
ETL_CONSTEXPR time_point() ETL_NOEXCEPT
Default constructor.
Definition time_point.h:56
ETL_CONSTEXPR14 time_point & operator++() ETL_NOEXCEPT
Pre-increments the stored duration by one tick.
Definition time_point.h:128
ETL_CONSTEXPR14 time_point operator--(int) ETL_NOEXCEPT
Post-decrements the stored duration by one tick.
Definition time_point.h:159
static ETL_NODISCARD ETL_CONSTEXPR14 time_point min() ETL_NOEXCEPT
Returns a time_point with the smallest possible duration.
Definition time_point.h:171
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const time_point &other) const ETL_NOEXCEPT
Definition time_point.h:191
static ETL_NODISCARD ETL_CONSTEXPR14 time_point max() ETL_NOEXCEPT
Returns a time_point with the largest possible duration.
Definition time_point.h:180
ETL_CONSTEXPR14 time_point(const time_point< clock, TDuration2 > &rhs) ETL_NOEXCEPT
Copy construct from another time_point with a different duration type.
Definition time_point.h:81
ETL_CONSTEXPR14 time_point(const time_point &rhs) ETL_NOEXCEPT
Copy constructor.
Definition time_point.h:72
ETL_CONSTEXPR14 time_point & operator--() ETL_NOEXCEPT
Pre-decrements the stored duration by one tick.
Definition time_point.h:149
ETL_CONSTEXPR14 time_point & operator-=(const duration &rhs) ETL_NOEXCEPT
Subtracts a duration.
Definition time_point.h:118
ETL_CONSTEXPR14 time_point & operator+=(const duration &rhs) ETL_NOEXCEPT
Adds a duration.
Definition time_point.h:108
ETL_CONSTEXPR14 time_point operator++(int) ETL_NOEXCEPT
Post-increments the stored duration by one tick.
Definition time_point.h:138
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
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:343
Definition absolute.h:40
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point< TClock, TToDuration > round(const etl::chrono::time_point< TClock, TDuration > &tp) ETL_NOEXCEPT
Definition time_point.h:230
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point< TClock, TToDuration > floor(const etl::chrono::time_point< TClock, TDuration > &tp) ETL_NOEXCEPT
Rounds down a duration to the nearest lower precision.
Definition time_point.h:210
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point< TClock, TToDuration > ceil(const etl::chrono::time_point< TClock, TDuration > &tp) ETL_NOEXCEPT
Rounds up a duration to the nearest higher precision.
Definition time_point.h:220