tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
round.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_CHRONO_ROUND_HPP
4#define TETL_CHRONO_ROUND_HPP
5
9
10namespace etl::chrono {
11
13template <typename To, typename Rep, typename Period>
14 requires(detail::is_duration_v<To>)
15[[nodiscard]] constexpr auto round(duration<Rep, Period> const& dur)
17{
18 auto const low = floor<To>(dur);
19 auto const high = low + To{1};
20 auto const lowDiff = dur - low;
21 auto const highDiff = high - dur;
22 if (lowDiff < highDiff) {
23 return low;
24 }
25 if (lowDiff > highDiff) {
26 return high;
27 }
28 return low.count() & 1 ? high : low;
29}
30
32template <typename To, typename Clock, typename Duration>
33 requires(detail::is_duration_v<To>)
34[[nodiscard]] constexpr auto round(time_point<Clock, Duration> const& tp) -> time_point<Clock, To>
35{
37}
38
39} // namespace etl::chrono
40
41#endif // TETL_CHRONO_ROUND_HPP
constexpr auto round(duration< Rep, Period > const &dur) noexcept(is_arithmetic_v< Rep > and is_arithmetic_v< typename To::rep >) -> To
Definition round.hpp:15
constexpr auto floor(duration< Rep, Period > const &d) noexcept(is_arithmetic_v< Rep > and is_arithmetic_v< typename To::rep >) -> To
Returns the greatest duration t representable in ToDuration that is less or equal to d....
Definition floor.hpp:18
Definition abs.hpp:11
constexpr bool is_arithmetic_v
Definition is_arithmetic.hpp:21
Class template etl::chrono::duration represents a time interval.
Definition duration.hpp:31
constexpr auto count() const -> rep
Returns the number of ticks for this duration.
Definition duration.hpp:97
Class template time_point represents a point in time. It is implemented as if it stores a value of ty...
Definition time_point.hpp:21
constexpr auto time_since_epoch() const noexcept -> duration
Returns a duration representing the amount of time between *this and the clock's epoch.
Definition time_point.hpp:61