Namespaces
Variants

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

From cppreference.net
헤더 파일에 정의됨 <chrono>
template < class ToDuration, class Clock, class Duration >

constexpr std:: chrono :: time_point < Clock, ToDuration >

round ( const std:: chrono :: time_point < Clock, Duration > & tp ) ;
(C++17부터)

tp 에 가장 가까운 시간 포인트를 반환하며, ToDuration 에서 표현 가능한 값으로, 중간 값인 경우 짝수로 반올림합니다.

이 함수는 다음 조건을 만족하지 않는 한 오버로드 해결에 참여하지 않습니다: ToDuration std::chrono::duration 의 특수화이며, std:: chrono :: treat_as_floating_point_v < typename ToDuration :: rep > false 인 경우입니다.

목차

매개변수

tp - 가장 가까운 값으로 반올림할 시간 포인트

반환값

tp ToDuration 유형의 지속 시간을 사용하여 가장 가까운 시간 포인트로 반올림하며, 중간 값의 경우 짝수로 반올림합니다.

가능한 구현

namespace detail
{
    template<class> inline constexpr bool is_duration_v = false;
    template<class Rep, class Period> inline constexpr bool is_duration_v<
        std::chrono::duration<Rep, Period>> = true;
}
template<class To, class Clock, class FromDuration,
         class = std::enable_if_t<detail::is_duration_v<To> &&
                !std::chrono::treat_as_floating_point_v<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())};
}

예제

#include <chrono>
#include <iostream>
#include <string>
template<typename TimePoint>
std::string to_string(const TimePoint& time_point)
{
    return std::to_string(time_point.time_since_epoch().count());
}
int main()
{
    using namespace std::literals::chrono_literals;
    using Sec = std::chrono::seconds;
    std::cout << "Time point\t" "Cast\t" "Floor\t" "Round\t" "Ceil\n";
    std::cout << "(ms)\t\t"     "(s)\t"  "(s)\t"   "(s)\t"   "(s)\n";
    for (const auto value_ms : {5432ms, 5678ms})
    {
        std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>
            time_point_ms(value_ms);
        std::cout
            << to_string(time_point_ms) << "\t\t"
            << to_string(std::chrono::time_point_cast<Sec>(time_point_ms)) << '\t'
            << to_string(std::chrono::floor<Sec>(time_point_ms)) << '\t'
            << to_string(std::chrono::round<Sec>(time_point_ms)) << '\t'
            << to_string(std::chrono::ceil<Sec>(time_point_ms)) << '\n';
    }
}

출력:

Time point	Cast	Floor	Round	Ceil
(ms)		(s)	(s)	(s)	(s)
5432		5	5	5	6
5678		5	5	6	6

참고 항목

동일한 클록에서 다른 duration을 가진 시간 포인트로 변환
(함수 템플릿)
시간 포인트를 다른 시간 포인트로 변환하며, 올림 처리
(함수 템플릿)
시간 포인트를 다른 시간 포인트로 변환하며, 내림 처리
(함수 템플릿)
duration을 다른 duration으로 변환하며, 가장 가까운 값으로 반올림(동점 시 짝수로)
(함수 템플릿)
(C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11) (C++11)
가장 가까운 정수로 반올림(중간값일 경우 0에서 멀어지는 방향)
(함수)