tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
duration.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_CHRONO_DURATION_HPP
4#define TETL_CHRONO_DURATION_HPP
5
10#include <etl/_numeric/lcm.hpp>
11#include <etl/_ratio/ratio.hpp>
15
16namespace etl::chrono {
17
20
30template <typename Rep, typename Period = etl::ratio<1>>
31struct duration {
33 using rep = Rep;
34
37 using period = typename Period::type;
38
41 constexpr duration() noexcept = default;
42
46 constexpr duration(duration const&) noexcept = default;
47
59 template <typename Rep2>
61 constexpr explicit duration(Rep2 const& r) noexcept
62 : _rep(r)
63 {
64 }
65
83 template <typename Rep2, typename Period2>
84 requires(
87 )
88 constexpr duration(duration<Rep2, Period2> const& other) noexcept
89 : _rep(static_cast<Rep>(other.count() * ratio_divide<Period2, period>::num))
90 {
91 }
92
94 auto operator=(duration const& other) -> duration& = default;
95
97 [[nodiscard]] constexpr auto count() const -> rep { return _rep; }
98
100 [[nodiscard]] static constexpr auto zero() noexcept -> duration
101 {
103 }
104
106 [[nodiscard]] static constexpr auto min() noexcept -> duration
107 {
109 }
110
112 [[nodiscard]] static constexpr auto max() noexcept -> duration
113 {
115 }
116
118 [[nodiscard]] constexpr auto operator+() const -> etl::common_type_t<duration>
119 {
120 return etl::common_type_t<duration>(*this);
121 }
122
124 [[nodiscard]] constexpr auto operator-() const -> etl::common_type_t<duration>
125 {
126 return etl::common_type_t<duration>(-_rep);
127 }
128
131 constexpr auto operator++() -> duration&
132 {
133 ++_rep;
134 return *this;
135 }
136
139 constexpr auto operator++(int) -> duration { return duration(_rep++); }
140
143 constexpr auto operator--() -> duration&
144 {
145 --_rep;
146 return *this;
147 }
148
151 constexpr auto operator--(int) -> duration { return duration(_rep--); }
152
155 constexpr auto operator+=(duration const& d) noexcept -> duration&
156 {
157 _rep += d.count();
158 return *this;
159 }
160
163 constexpr auto operator-=(duration const& d) noexcept -> duration&
164 {
165 _rep -= d.count();
166 return *this;
167 }
168
171 constexpr auto operator*=(rep const& rhs) noexcept -> duration&
172 {
173 _rep *= rhs;
174 return *this;
175 }
176
179 constexpr auto operator/=(rep const& rhs) noexcept -> duration&
180 {
181 _rep /= rhs;
182 return *this;
183 }
184
187 constexpr auto operator%=(rep const& rhs) noexcept -> duration&
188 {
189 _rep %= rhs;
190 return *this;
191 }
192
195 constexpr auto operator%=(duration const& rhs) noexcept -> duration&
196 {
197 _rep %= rhs.count();
198 return *this;
199 }
200
201private:
202 rep _rep{};
203};
204
205} // namespace etl::chrono
206
207namespace etl {
208
216template <typename Rep1, typename Period1, typename Rep2, typename Period2>
217struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>> {
218private:
219 static constexpr auto num = gcd(Period1::num, Period2::num);
220 static constexpr auto den = lcm(Period1::den, Period2::den);
221
222public:
224};
225
226} // namespace etl
227
228namespace etl::chrono {
229
237template <typename Rep1, typename Period1, typename Rep2, typename Period2>
238[[nodiscard]] constexpr auto operator+(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs)
240{
242 using CR = typename CD::rep;
243 return CD(static_cast<CR>(CD(lhs).count() + CD(rhs).count()));
244}
245
254template <typename Rep1, typename Period1, typename Rep2, typename Period2>
255[[nodiscard]] constexpr auto operator-(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs)
257{
259 using CR = typename CD::rep;
260 return CD(static_cast<CR>(CD(lhs).count() - CD(rhs).count()));
261}
262
271template <typename Rep1, typename Period1, typename Rep2, typename Period2>
272[[nodiscard]] constexpr auto operator/(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs)
274{
276 return CD(lhs).count() / CD(rhs).count();
277}
278
287template <typename Rep1, typename Period1, typename Rep2, typename Period2>
288[[nodiscard]] constexpr auto operator%(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs)
290{
292 using CR = typename CD::rep;
293 return CD(static_cast<CR>(CD(lhs).count() % CD(rhs).count()));
294}
295
298template <typename Rep1, typename Period1, typename Rep2, typename Period2>
299[[nodiscard]] constexpr auto operator==(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs) -> bool
300{
302 return common_t(lhs).count() == common_t(rhs).count();
303}
304
307template <typename Rep1, typename Period1, typename Rep2, typename Period2>
308[[nodiscard]] constexpr auto operator!=(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs) -> bool
309{
310 return !(lhs == rhs);
311}
312
315template <typename Rep1, typename Period1, typename Rep2, typename Period2>
316[[nodiscard]] constexpr auto operator<(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs) -> bool
317{
319 return common_t(lhs).count() < common_t(rhs).count();
320}
321
324template <typename Rep1, typename Period1, typename Rep2, typename Period2>
325[[nodiscard]] constexpr auto operator<=(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs) -> bool
326{
327 return !(rhs < lhs);
328}
329
332template <typename Rep1, typename Period1, typename Rep2, typename Period2>
333[[nodiscard]] constexpr auto operator>(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs) -> bool
334{
335 return rhs < lhs;
336}
337
340template <typename Rep1, typename Period1, typename Rep2, typename Period2>
341[[nodiscard]] constexpr auto operator>=(duration<Rep1, Period1> const& lhs, duration<Rep2, Period2> const& rhs) -> bool
342{
343 return !(lhs < rhs);
344}
345
348
351
354
357
360
363
366
369
372
375
377
378} // namespace etl::chrono
379
380// NOLINTNEXTLINE(modernize-concat-nested-namespaces)
381namespace etl {
382
383inline namespace literals {
384inline namespace chrono_literals {
385
388constexpr auto operator""_h(unsigned long long h) -> etl::chrono::hours
389{
390 return etl::chrono::hours(static_cast<etl::chrono::hours::rep>(h));
391}
392
396constexpr auto operator""_h(long double h) -> etl::chrono::duration<long double, ratio<3600, 1>>
397{
399}
400
403constexpr auto operator""_min(unsigned long long m) -> etl::chrono::minutes
404{
405 return etl::chrono::minutes(static_cast<etl::chrono::minutes::rep>(m));
406}
407
411constexpr auto operator""_min(long double m) -> etl::chrono::duration<long double, etl::ratio<60, 1>>
412{
414}
415
418constexpr auto operator""_s(unsigned long long m) -> etl::chrono::seconds
419{
420 return etl::chrono::seconds(static_cast<etl::chrono::seconds::rep>(m));
421}
422
426constexpr auto operator""_s(long double m) -> etl::chrono::duration<long double>
427{
429}
430
434constexpr auto operator""_ms(unsigned long long m) -> etl::chrono::milliseconds
435{
437}
438
442constexpr auto operator""_ms(long double m) -> etl::chrono::duration<long double, etl::milli>
443{
445}
446
450constexpr auto operator""_us(unsigned long long m) -> etl::chrono::microseconds
451{
453}
454
458constexpr auto operator""_us(long double m) -> etl::chrono::duration<long double, etl::micro>
459{
461}
462
466constexpr auto operator""_ns(unsigned long long m) -> etl::chrono::nanoseconds
467{
469}
470
474constexpr auto operator""_ns(long double m) -> etl::chrono::duration<long double, etl::nano>
475{
477}
478
479} // namespace chrono_literals
480} // namespace literals
481} // namespace etl
482
483namespace etl::chrono {
484using namespace etl::literals::chrono_literals;
485} // namespace etl::chrono
486
487#endif // TETL_CHRONO_DURATION_HPP
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:21
constexpr bool treat_as_floating_point_v
Definition treat_as_floating_point.hpp:24
constexpr auto gcd(M m, N n) noexcept -> etl::common_type_t< M, N >
Computes the greatest common divisor of the integers m and n.
Definition gcd.hpp:16
constexpr auto lcm(M m, N n) -> common_type_t< M, N >
Computes the least common multiple of the integers m and n.
Definition lcm.hpp:20
Definition abs.hpp:11
duration< int_least64_t, micro > microseconds
Signed integer type of at least 55 bits.
Definition duration.hpp:350
constexpr auto operator%(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> common_type_t< duration< Rep1, Period1 >, duration< Rep2, Period2 > >
Performs basic arithmetic operations between two durations or between a duration and a tick count.
Definition duration.hpp:288
constexpr auto operator<(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:316
constexpr auto operator+(day const &d, days const &ds) noexcept -> day
Definition day.hpp:92
constexpr auto operator>(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:333
duration< int_least32_t, ratio< 31556952 > > months
Signed integer type of at least 20 bits.
Definition duration.hpp:371
duration< int_least64_t, milli > milliseconds
Signed integer type of at least 45 bits.
Definition duration.hpp:353
constexpr auto operator==(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Checks if lhs and rhs are equal, i.e. the number of ticks for the type common...
Definition duration.hpp:299
duration< int_least32_t, ratio< 86400 > > days
Signed integer type of at least 25 bits.
Definition duration.hpp:365
duration< int_least32_t, ratio< 604800 > > weeks
Signed integer type of at least 22 bits.
Definition duration.hpp:368
constexpr auto operator!=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Checks if lhs and rhs are equal, i.e. the number of ticks for the type common...
Definition duration.hpp:308
duration< int_least32_t, ratio< 60 > > minutes
Signed integer type of at least 29 bits.
Definition duration.hpp:359
constexpr auto operator/(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> common_type_t< Rep1, Rep2 >
Performs basic arithmetic operations between two durations or between a duration and a tick count.
Definition duration.hpp:272
duration< int_least32_t, ratio< 2629746 > > years
Signed integer type of at least 17 bits.
Definition duration.hpp:374
constexpr auto operator<=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:325
duration< int_least64_t, nano > nanoseconds
Signed integer type of at least 64 bits.
Definition duration.hpp:347
constexpr auto operator-(day const &d, days const &ds) noexcept -> day
Definition day.hpp:102
duration< int_least64_t > seconds
Signed integer type of at least 35 bits.
Definition duration.hpp:356
constexpr auto operator>=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:341
duration< int_least32_t, ratio< 3600 > > hours
Signed integer type of at least 23 bits.
Definition duration.hpp:362
Definition day.hpp:117
Definition day.hpp:116
Definition adjacent_find.hpp:8
typename common_type< T... >::type common_type_t
Definition common_type.hpp:50
constexpr bool is_convertible_v
Definition is_convertible.hpp:46
static constexpr auto max() -> Rep
Returns the special duration value max.
Definition duration_values.hpp:28
static constexpr auto min() -> Rep
Returns the smallest possible representation.
Definition duration_values.hpp:25
static constexpr auto zero() -> Rep
Returns a zero-length representation.
Definition duration_values.hpp:22
Class template etl::chrono::duration represents a time interval.
Definition duration.hpp:31
static constexpr auto min() noexcept -> duration
Returns a duration with the lowest possible value.
Definition duration.hpp:106
constexpr auto count() const -> rep
Returns the number of ticks for this duration.
Definition duration.hpp:97
typename Period::type period
A etl::ratio representing the tick period (i.e. the number of seconds per tick).
Definition duration.hpp:37
constexpr auto operator--() -> duration &
Increments or decrements the number of ticks for this duration. Equivalent to –_rep; return *this;.
Definition duration.hpp:143
constexpr auto operator/=(rep const &rhs) noexcept -> duration &
Performs compound assignments between two durations with the same period or between a duration and a ...
Definition duration.hpp:179
constexpr auto operator-=(duration const &d) noexcept -> duration &
Performs compound assignments between two durations with the same period or between a duration and a ...
Definition duration.hpp:163
constexpr auto operator+=(duration const &d) noexcept -> duration &
Performs compound assignments between two durations with the same period or between a duration and a ...
Definition duration.hpp:155
constexpr auto operator+() const -> etl::common_type_t< duration >
Implements unary plus and unary minus for the durations.
Definition duration.hpp:118
constexpr auto operator++(int) -> duration
Increments or decrements the number of ticks for this duration. Equivalent to return duration(_rep++)
Definition duration.hpp:139
Rep rep
Rep, an arithmetic type representing the number of ticks.
Definition duration.hpp:33
constexpr auto operator%=(rep const &rhs) noexcept -> duration &
Performs compound assignments between two durations with the same period or between a duration and a ...
Definition duration.hpp:187
auto operator=(duration const &other) -> duration &=default
Assigns the contents of one duration to another.
static constexpr auto zero() noexcept -> duration
Returns a zero-length duration.
Definition duration.hpp:100
constexpr auto operator-() const -> etl::common_type_t< duration >
Implements unary plus and unary minus for the durations.
Definition duration.hpp:124
constexpr auto operator*=(rep const &rhs) noexcept -> duration &
Performs compound assignments between two durations with the same period or between a duration and a ...
Definition duration.hpp:171
constexpr duration(duration< Rep2, Period2 > const &other) noexcept
Constructs a duration by converting d to an appropriate period and tick count, as if by duration_cast...
Definition duration.hpp:88
static constexpr auto max() noexcept -> duration
Returns a duration with the largest possible value.
Definition duration.hpp:112
constexpr auto operator--(int) -> duration
Increments or decrements the number of ticks for this duration. Equivalent to return duration(_rep–);...
Definition duration.hpp:151
constexpr auto operator%=(duration const &rhs) noexcept -> duration &
Performs compound assignments between two durations with the same period or between a duration and a ...
Definition duration.hpp:195
constexpr auto operator++() -> duration &
Increments or decrements the number of ticks for this duration. Equivalent to ++_rep; return *this;.
Definition duration.hpp:131
constexpr duration() noexcept=default
Constructs a new duration from one of several optional data sources. The default constructor is defau...
chrono::duration< common_type_t< Rep1, Rep2 >, ratio< num, den > > type
Definition duration.hpp:223
Determines the common type among all types T..., that is the type all T... can be implicitly converte...
Definition common_type.hpp:18
The typename template provides compile-time rational arithmetic support. Each instantiation of this t...
Definition ratio.hpp:21
static constexpr intmax_t den
Definition ratio.hpp:23
static constexpr intmax_t num
Definition ratio.hpp:22