std::chrono::round(std::chrono::time_point)

From cppreference.com
< cpplrm; | chronolrm; | time point
Date and time utilities
(C++11)
(C++11)
Clocks
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time zones
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
C-style date and time
Defined in header <chrono>
template <class ToDuration, class Clock, class Duration>
constexpr time_point<Clock, ToDuration> round(const time_point<Clock, Duration>& tp);
(since C++17)

Returns the nearest time point to tp representable in ToDuration, rounding to even in halfway cases.

The function does not participate in the overload resolution unless ToDuration is an specialization of std::chrono::duration and std::chrono::treat_as_floating_point<typename ToDuration::rep>::value is false.

Parameters

tp - time point to round to nearest

Return value

d rounded to nearest time point using duration of type ToDuration, rounding to even in halfway cases.

Possible implementation

template <class T> struct is_duration : std::false_type {};
template <class Rep, class Period> struct is_duration<
    std::chrono::duration<Rep, Period>> : std::true_type {};

template <class To, class Clock, class FromDuration,
          class = std::enable_if_t<is_duration<To>{}
             && !std::chrono::treat_as_floating_point<typename To::rep>{}>>
constexpr std::chrono::time_point<Clock, To> round(
    const std::chrono::time_point<Clock, FromDuration>& tp)
{
    return std::chrono::time_point<Clock, To>{
        std::chrono::round<To>(tp.time_since_epoch())};
}

Example

See also

converts a time point to another time point on the same clock, with a different duration
(function template)
converts a time_point to another, rounding up
(function template)
converts a time_point to another, rounding down
(function template)
converts a duration to another, rounding to nearest, ties to even
(function template)
(C++11)(C++11)(C++11)
nearest integer, rounding away from zero in halfway cases
(function)