std::chrono::time_point_cast
From cppreference.com
< cpp | chrono | time point
| ヘッダー <chrono> で定義 |
||
template< class ToDuration, class Clock, class Duration > std::chrono::time_point<Clock, ToDuration> |
(C++11以降) (C++14まで) |
|
| template< class ToDuration, class Clock, class Duration > constexpr std::chrono::time_point<Clock, ToDuration> |
(C++14以降) | |
ある期間から別の期間へ std::chrono::time_point を変換します。
time_point_cast は、ToDuration が std::chrono::duration の特殊化である場合にのみ、オーバーロード解決に参加します。
目次 |
[編集] パラメータ
| t | - | 変換元の `time_point` |
[編集] 戻り値
std::chrono::time_point<Clock, ToDuration>(
std::chrono::duration_cast<ToDuration>(t.time_since_epoch())).
[編集] 例
このコードを実行
#include <chrono> #include <iostream> using namespace std::chrono_literals; 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{4s}; // implicit conversion, no precision loss TimePoint<Ms> time_point_ms = time_point_sec; print_ms(time_point_ms); // 4000 ms time_point_ms = TimePoint<Ms>{5756ms}; print_ms(time_point_ms); // 5756 ms // explicit cast, need when precision loss may happen // 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 5756 ms 5000 ms
[編集] 関連項目
| time_point を別の time_point に変換し、切り捨てる (関数テンプレート) | |
| time_point を別の time_point に変換し、切り上げる (関数テンプレート) | |
| time_point を別の time_point に変換し、最も近い値に丸め、等距離の場合は偶数に丸める (関数テンプレート) | |
| (C++11) |
期間を異なる刻み間隔を持つ別の期間に変換する (関数テンプレート) |