std::chrono::time_point<Clock,Duration>::time_point
来自 zh.cppreference.net
time_point();
|
(1) | 自C++11起 自C++14起为constexpr |
explicit time_point( const duration& d );
|
(2) | 自C++11起 自C++14起为constexpr |
template< class Duration2 >
time_point( const time_point<Clock, Duration2>& t );
|
(3) | 自C++11起 自C++14起为constexpr |
从多个可选数据源之一构造一个新的 time_point。
1)默认构造函数,创建一个
time_point 表示 Clock 的纪元(即 time_since_epoch() 为零)。2)构造一个
time_point,位于 Clock 的纪元加上 d。3)通过将
time_point 转换为 t 来构造一个 duration。只有当 Duration2 可以隐式转换为 duration 时,此构造函数才参与重载决议。参数
| d | - |
要复制的
duration
对象
|
| t | - |
要转换的
time_point
对象
|
示例
运行此代码
#include <chrono>
#include <iostream>
using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;
void print_ms(const TimePoint& point)
{
using Ms = std::chrono::milliseconds;
const Clock::duration since_epoch = point.time_since_epoch();
std::cout << std::chrono::duration_cast<Ms>(since_epoch) << '\n';
}
int main()
{
const TimePoint default_value = TimePoint(); // (1)
print_ms(default_value); // 0ms
const Clock::duration duration_4_seconds = std::chrono::seconds(4);
const TimePoint time_point_4_seconds(duration_4_seconds); // (2)
// 4 seconds from start of epoch
print_ms(time_point_4_seconds); // 4000ms
const TimePoint time_point_now = Clock::now(); // (3)
print_ms(time_point_now); // 212178842ms
}
可能的输出:
0ms
4000ms
212178842ms
参见
|
构造新的 duration 对象
(
std::chrono::duration<Rep,Period>
的公开成员函数)
|
|
|
(C++11)
|
将 duration 转换为具有不同时间间隔单位的类型
(函数模板) |