| (since C++11) (until C++14) | |||
| (since C++14) |
Converts a std::chrono::time_point
from one duration to another.
Parameters
t | - | time_point to convert from |
Return value
std::chrono::time_point<Clock, ToDuration>(std::chrono::duration_cast<ToDuration>(t.time_since_epoch()))
.
Notes
time_point_cast
will only participate in overload resolution if ToDuration
is an instantiation of duration
.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #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 } |
Output:
1 2 | 4000 ms 5000 ms |
Please login to continue.