在线文档教程
C++
应用 | Utilities

std::chrono::time_point_cast

STD::时间::时间[医]点[医]铸造

template time_point time_point_cast( const time_point &t(since C++11) (until C++14)
template <class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> time_point_cast( const time_point<Clock, Duration> &t(since C++14)

转换std::chrono::time_point从一段时间到另一段时间。

参数

t-time_point to convert from

返回值

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

注记

time_point_cast将只参与重载解决方案。ToDuration的实例化duration...

二次

#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

二次

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/crono/time[医]点/时间[医]点[医]铸造