[英]Get a double from boost::chrono::steady_clock::now()
How would I get a double value from boost::chrono::steady_clock::now()? 我如何从boost :: chrono :: steady_clock :: now()获得double值? I don't believe there is a .count() parameter for this.
我不相信这有一个.count()参数。
Why do I need this? 我为什么需要这个? I have a method that cannot parse the boost return.
我有一个方法无法解析提升返回。
boost::chrono::steady_clock::now()
returns a boost::chrono::time_point
. boost::chrono::steady_clock::now()
返回一个boost::chrono::time_point
。 boost::chrono::time_point
has a time_since_epoch
method which returns a boost::chrono::duration
. boost::chrono::time_point
有一个time_since_epoch
方法,它返回一个boost::chrono::duration
time_since_epoch
boost::chrono::duration
。 boost::chrono::duration
has a count
method that gives you a scalar. boost::chrono::duration
有一个count
方法,可以给你一个标量。 When we put it all together, it boils down to: 当我们把它们放在一起时,归结为:
auto now = boost::chrono::steady_clock::now().time_since_epoch();
auto timeSinceEpoch = boost::chrono::duration_cast<boost::chrono::milliseconds>(now).count();
Granted that's not a double
but close enough: you can change the duration_cast
to get whatever precision you need and/or divide the timeSinceEpoch
to your liking so that your double
fits your requirements. 虽然这不是一个
double
但足够接近:你可以改变duration_cast
来获得你需要的任何精度和/或根据你的喜好划分timeSinceEpoch
,这样你的double
符合你的要求。 For example: 例如:
// Days since epoch, with millisecond (see duration_cast above) precision
double daysSinceEpoch = double(timeSinceEpoch) / (24. * 3600. * 1000.);
you can specify double
inside duration
: 你可以指定
double
duration
:
auto some_time = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::duration<double>>(some_time).count();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.