std::chrono:: time_point_cast
|
定义于头文件
<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}; // 隐式转换,无精度损失 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 // 显式转换,在可能发生精度损失时需要 // 5756 被截断为 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
参见
|
(C++17)
|
将 time_point 转换为另一种精度,向下取整
(函数模板) |
|
(C++17)
|
将 time_point 转换为另一种精度,向上取整
(函数模板) |
|
(C++17)
|
将 time_point 转换为另一种精度,四舍五入到最近值,中间值向偶数取整
(函数模板) |
|
(C++11)
|
将 duration 转换为具有不同时间间隔精度的类型
(函数模板) |