[英]How to get std::chrono::system_clock::time_point from std::chrono::system_clock::time_point.time_since_epoch().count()?
I have a variable stored as a long
a value from std::chrono::system_clock::time_point.time_since_epoch().count()
. 我有存储为可变
long
从一个值std::chrono::system_clock::time_point.time_since_epoch().count()
I would now like to restore that std::chrono::system_clock::time_point
from the long
variable. 我现在想从
long
变量中恢复该std::chrono::system_clock::time_point
。 So how can I convert a long
to a std::chrono::system_clock::time_point
? 那么如何将
long
转换为std::chrono::system_clock::time_point
?
You first need to convert the integral type to a chrono::duration
, and then convert the duration
to a system_clock::time_point
. 您首先需要将整数类型转换为
system_clock::time_point
chrono::duration
,然后将duration
转换为system_clock::time_point
。 But there's a catch: 但是有一个陷阱:
duration
is a template: duration
是一个模板:
template <class Rep, class Period> class duration;
If you convert the integral type to the wrong duration
, you'll get the wrong time_point
. 如果将整数类型转换为错误的
duration
,则会得到错误的time_point
。
Fortunately system_clock
itself tells you the correct duration
with its nested duration
type: system_clock::duration
. 幸运的是,
system_clock
本身会使用嵌套的duration
类型告诉您正确的duration
duration
: system_clock::duration
。 Additionally, each of these conversions is explicit
. 此外,这些转换都是
explicit
。
So, in summary: 因此,总而言之:
using namespace std::chrono;
system_clock::time_point tp{system_clock::duration{i}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.