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_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 ETL_CONSTEXPR14 int compare(const time_point& other) const ETL_NOEXCEPT
150 {
151 if (dur < other.dur)
152 return -1;
153 if (dur > other.dur)
154 return 1;
155
156 return 0;
157 }
158
159 private:
160
161 duration dur;
162 };
163
164 //***********************************************************************
166 //***********************************************************************
167 template <typename TToDuration, typename TClock, typename TDuration>
169 ETL_NOEXCEPT
170 {
172 }
173
174 //***********************************************************************
176 //***********************************************************************
177 template <typename TToDuration, typename TClock, typename TDuration>
179 {
181 }
182
183 //***********************************************************************
186 //***********************************************************************
187 template <typename TToDuration, typename TClock, typename TDuration>
189 ETL_NOEXCEPT
190 {
192 }
193
194 template <typename TToDuration, typename TClock, typename TDuration>
195 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point<TClock, TToDuration> time_point_cast(const etl::chrono::time_point<TClock, TDuration>& tp)
196 ETL_NOEXCEPT
197 {
198 TToDuration dur = etl::chrono::duration_cast<TToDuration>(tp.time_since_epoch());
199
201 }
202
203 //***************************************************************************
205 //***************************************************************************
206 template <typename TClock, typename TDuration1, typename TDuration2>
207 ETL_CONSTEXPR14 bool operator==(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
208 {
209 return lhs.time_since_epoch() == rhs.time_since_epoch();
210 }
211
212 //***************************************************************************
214 //***************************************************************************
215 template <typename TClock, typename TDuration1, typename TDuration2>
216 ETL_CONSTEXPR14 bool operator!=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
217 {
218 return !(lhs == rhs);
219 }
220
221 //***************************************************************************
223 //***************************************************************************
224 template <typename TClock, typename TDuration1, typename TDuration2>
225 ETL_CONSTEXPR14 bool operator<(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
226 {
227 return lhs.time_since_epoch() < rhs.time_since_epoch();
228 }
229
230 //***************************************************************************
232 //***************************************************************************
233 template <typename TClock, typename TDuration1, typename TDuration2>
234 ETL_CONSTEXPR14 bool operator<=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
235 {
236 return !(rhs < lhs);
237 }
238
239 //***************************************************************************
241 //***************************************************************************
242 template <typename TClock, typename TDuration1, typename TDuration2>
243 ETL_CONSTEXPR14 bool operator>(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
244 {
245 return rhs < lhs;
246 }
247
248 //***************************************************************************
250 //***************************************************************************
251 template <typename TClock, typename TDuration1, typename TDuration2>
252 ETL_CONSTEXPR14 bool operator>=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
253 {
254 return !(lhs < rhs);
255 }
256 } // namespace chrono
257
258 //***********************************************************************
260 //***********************************************************************
261#if ETL_USING_CPP20
262 template <typename TClock, typename TDuration1, typename TDuration2>
263 [[nodiscard]]
265 ETL_NOEXCEPT
266 {
267 return (lhs.time_since_epoch() <=> rhs.time_since_epoch());
268 }
269#endif
270
271 //***************************************************************************
273 //***************************************************************************
274 template <typename TClock, typename TDuration1, typename TDuration2>
275 struct common_type<etl::chrono::time_point<TClock, TDuration1>, etl::chrono::time_point<TClock, TDuration2>>
276 {
278 };
279} // 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
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:149
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 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
bitset_ext
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:188
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:168
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:178