名前空間
変種

std::chrono::time_point_cast

提供: cppreference.com
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
日付と時間のユーティリティ
(C++11)
(C++11)
時刻
(C++20)



(C++20)(C++20)(C++20)(C++20)
時計
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
カレンダー
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
タイムゾーン
(C++20)
(C++20)
(C++20)
(C++20)
C スタイルの日付と時間
 
 
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>
template <class ToDuration, class Clock, class Duration> time_point<Clock, ToDuration> time_point_cast( const time_point<Clock, Duration> &t);
(C++11以上)
(C++14未満)
template <class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> time_point_cast( const time_point<Clock, Duration> &t);
(C++14以上)

std::chrono::time_point の時間単位を別の時間単位に変換します。

引数

t - 変換する time_point

戻り値

std::chrono::time_point<Clock, ToDuration>(std::chrono::duration_cast<ToDuration>(t.time_since_epoch()))

ノート

time_point_cast は、 ToDurationduration のインスタンスである場合にのみ、オーバーロード解決に参加します。

#include <iostream>
#include <chrono>

using Clock = std::chrono::high_resolution_clock;
using Ms = std::chrono::milliseconds;
using Sec = std::chrono::seconds;

template<class Duration>
using TimePoint = std::chrono::time_point<Clock, Duration>;

inline void print_ms(const TimePoint<Ms>& time_point) 
{
    std::cout << time_point.time_since_epoch().count() << " ms\n";
}

int main() 
{
    TimePoint<Sec> time_point_sec(Sec(4));

    // implicit cast, no precision loss
    TimePoint<Ms> time_point_ms(time_point_sec);
    print_ms(time_point_ms); // 4000 ms

    time_point_ms = TimePoint<Ms>(Ms(5756));

    // explicit cast, need when precision loss may happens
    // 5756 truncated to 5000
    time_point_sec = std::chrono::time_point_cast<Sec>(time_point_ms);
    print_ms(time_point_sec); // 5000 ms
}

出力:

4000 ms
5000 ms
Morty Proxy This is a proxified and sanitized view of the page, visit original site.