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// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_CHRONO_ROUND_HPP
5#define TETL_CHRONO_ROUND_HPP
6
7#include <etl/_chrono/duration_cast.hpp>
8#include <etl/_chrono/time_point_cast.hpp>
9#include <etl/_type_traits/is_arithmetic.hpp>
10
11namespace etl::chrono {
12
13/// \ingroup chrono
14template <typename To, typename Rep, typename Period>
15 requires(detail::is_duration_v<To>)
16[[nodiscard]] constexpr auto
17round(duration<Rep, Period> const& dur) noexcept(is_arithmetic_v<Rep> and is_arithmetic_v<typename To::rep>) -> To
18{
19 auto const low = floor<To>(dur);
20 auto const high = low + To{1};
21 auto const lowDiff = dur - low;
22 auto const highDiff = high - dur;
23 if (lowDiff < highDiff) {
24 return low;
25 }
26 if (lowDiff > highDiff) {
27 return high;
28 }
29 return low.count() & 1 ? high : low;
30}
31
32/// \ingroup chrono
33template <typename To, typename Clock, typename Duration>
34 requires(detail::is_duration_v<To>)
35[[nodiscard]] constexpr auto round(time_point<Clock, Duration> const& tp) -> time_point<Clock, To>
36{
37 return time_point<Clock, To>{round<To>(tp.time_since_epoch())};
38}
39
40} // namespace etl::chrono
41
42#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:17
constexpr auto round(time_point< Clock, Duration > const &tp) -> time_point< Clock, To >
Definition round.hpp:35
Definition abs.hpp:12
Definition adjacent_find.hpp:9
Class template etl::chrono::duration represents a time interval.
Definition duration.hpp:32
Class template time_point represents a point in time. It is implemented as if it stores a value of ty...
Definition time_point.hpp:22