名前空間
変種
操作

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

From cppreference.com
< cpp‎ | chrono‎ | time point
 
 
 
 
ヘッダー <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 で返し、中間値は偶数丸めを行います。

この関数は、ToDurationstd::chrono::duration の特殊化であり、かつ std::chrono::treat_as_floating_point_v<typename ToDuration::rep>false でない限り、オーバーロード解決に参加しません。

目次

[編集] パラメータ

tp - 丸め対象の時間点

[編集] 戻り値

ToDuration 型の期間を使用して、指定された時間点 tp を最も近い時間点に丸めた結果。中間値は偶数丸めを行います。

[編集] 実装例

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

[編集] 関連項目

時間点を、同じクロック上の異なる期間を持つ別の時間点に変換する
(関数テンプレート) [edit]
time_point を別の time_point に変換し、切り上げる
(関数テンプレート) [edit]
time_point を別の time_point に変換し、切り捨てる
(関数テンプレート) [edit]
期間を別の期間に変換し、最も近い値に丸め、等距離の場合は偶数に丸める
(関数テンプレート) [edit]
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
最近接整数、中間値はゼロから遠い方に丸める
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)