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 //***************************************************************************
98 //***************************************************************************
99 ETL_NODISCARD
100 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_NODISCARD
129 static ETL_CONSTEXPR14 time_point min() ETL_NOEXCEPT
130 {
131 return time_point(duration::min());
132 }
133
134 //***************************************************************************
136 //***************************************************************************
137 ETL_NODISCARD
138 static ETL_CONSTEXPR14 time_point max() ETL_NOEXCEPT
139 {
140 return time_point(duration::max());
141 }
142
143 //***********************************************************************
148 //***********************************************************************
149 ETL_NODISCARD
150 ETL_CONSTEXPR14 int compare(const time_point& other) const ETL_NOEXCEPT
151 {
152 if (dur < other.dur) return -1;
153 if (dur > other.dur) return 1;
154
155 return 0;
156 }
157
158 private:
159
160 duration dur;
161 };
162
163 //***********************************************************************
165 //***********************************************************************
166 template <typename TToDuration, typename TClock, typename TDuration>
167 ETL_NODISCARD
168 ETL_CONSTEXPR14
171 {
173 }
174
175 //***********************************************************************
177 //***********************************************************************
178 template <typename TToDuration, typename TClock, typename TDuration>
179 ETL_NODISCARD
180 ETL_CONSTEXPR14
183 {
185 }
186
187 //***********************************************************************
190 //***********************************************************************
191 template <typename TToDuration, typename TClock, typename TDuration>
192 ETL_NODISCARD
193 ETL_CONSTEXPR14
196 {
198 }
199
200 template <typename TToDuration, typename TClock, typename TDuration>
201 ETL_NODISCARD
202 ETL_CONSTEXPR14
204 time_point_cast(const etl::chrono::time_point<TClock, TDuration>& tp) ETL_NOEXCEPT
205 {
206 TToDuration dur = etl::chrono::duration_cast<TToDuration>(tp.time_since_epoch());
207
209 }
210
211 //***************************************************************************
213 //***************************************************************************
214 template <typename TClock, typename TDuration1, typename TDuration2>
215 ETL_CONSTEXPR14 bool operator ==(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
216 {
217 return lhs.time_since_epoch() == rhs.time_since_epoch();
218 }
219
220 //***************************************************************************
222 //***************************************************************************
223 template <typename TClock, typename TDuration1, typename TDuration2>
224 ETL_CONSTEXPR14 bool operator !=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
225 {
226 return !(lhs == rhs);
227 }
228
229 //***************************************************************************
231 //***************************************************************************
232 template <typename TClock, typename TDuration1, typename TDuration2>
233 ETL_CONSTEXPR14 bool operator <(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
234 {
235 return lhs.time_since_epoch() < rhs.time_since_epoch();
236 }
237
238 //***************************************************************************
240 //***************************************************************************
241 template <typename TClock, typename TDuration1, typename TDuration2>
242 ETL_CONSTEXPR14 bool operator <=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
243 {
244 return !(rhs < lhs);
245 }
246
247 //***************************************************************************
249 //***************************************************************************
250 template <typename TClock, typename TDuration1, typename TDuration2>
251 ETL_CONSTEXPR14 bool operator >(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
252 {
253 return rhs < lhs;
254 }
255
256 //***************************************************************************
258 //***************************************************************************
259 template <typename TClock, typename TDuration1, typename TDuration2>
260 ETL_CONSTEXPR14 bool operator >=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
261 {
262 return !(lhs < rhs);
263 }
264 }
265
266 //***********************************************************************
268 //***********************************************************************
269#if ETL_USING_CPP20
270 template <typename TClock, typename TDuration1, typename TDuration2>
271 [[nodiscard]] constexpr auto operator <=>(const etl::chrono::time_point<TClock, TDuration1>& lhs, const etl::chrono::time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
272 {
273 return (lhs.time_since_epoch() <=> rhs.time_since_epoch());
274 }
275#endif
276
277 //***************************************************************************
279 //***************************************************************************
280 template <typename TClock, typename TDuration1, typename TDuration2>
281 struct common_type<etl::chrono::time_point<TClock, TDuration1>, etl::chrono::time_point<TClock, TDuration2>>
282 {
284 };
285}
duration
Definition duration.h:108
Definition time_point.h:45
ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
Returns a duration representing the amount of time between this and the clock's epoch.
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
static ETL_NODISCARD ETL_CONSTEXPR14 time_point min() ETL_NOEXCEPT
Returns a time_point with the smallest possible duration.
Definition time_point.h:129
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const time_point &other) const ETL_NOEXCEPT
Definition time_point.h:150
static ETL_NODISCARD ETL_CONSTEXPR14 time_point max() ETL_NOEXCEPT
Returns a time_point with the largest possible duration.
Definition time_point.h:138
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-=(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 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:339
bitset_ext
Definition absolute.h:39
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:195
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:170
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:182