std::chrono::time_point<Clock,Duration>:: time_point
From cppreference.net
<
cpp
|
chrono
|
time point
C++
Date and time library
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::chrono::time_point
| Member functions | ||||
|
time_point::time_point
|
||||
|
(C++20)
(C++20)
|
||||
| Non-member functions | ||||
|
(until C++20)
(C++20)
|
||||
|
(C++17)
|
||||
|
(C++17)
|
||||
|
(C++17)
|
||||
| Helper classes | ||||
|
(C++26)
|
| (1) | ||
|
time_point
(
)
;
|
(自 C++11 起)
(自 C++14 起为 constexpr) |
|
| (2) | ||
|
explicit
time_point
(
const
duration
&
d
)
;
|
(自 C++11 起)
(自 C++14 起为 constexpr) |
|
| (3) | ||
|
template
<
class
Duration2
>
time_point ( const time_point < Clock, Duration2 > & t ) ; |
(自 C++11 起)
(自 C++14 起为 constexpr) |
|
从若干可选数据源之一构造新的
time_point
。
2)
构造一个位于
Clock
纪元加上
d
的
time_point
。
3)
通过将
t
转换为
duration
来构造一个
time_point
。仅当
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秒 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 转换为具有不同时间间隔单位的类型
(函数模板) |