How would I get a double value from boost::chrono::steady_clock::now()? I don't believe there is a .count() parameter for this.
Why do I need this? I have a method that cannot parse the boost return.
boost::chrono::steady_clock::now()
返回一个boost::chrono::time_point
。boost::chrono::time_point
有一个time_since_epoch
返回 a 的方法boost::chrono::duration
。boost::chrono::duration
有一种count
方法可以给你一个标量。当我们把它们放在一起时,它归结为:
auto now = boost::chrono::steady_clock::now().time_since_epoch();
auto timeSinceEpoch = boost::chrono::duration_cast<boost::chrono::milliseconds>(now).count();
当然,这还double
不够接近:您可以更改duration_cast
以获得所需的任何精度和/或根据自己的喜好划分,timeSinceEpoch
以double
满足您的要求。例如:
// Days since epoch, with millisecond (see duration_cast above) precision
double daysSinceEpoch = double(timeSinceEpoch) / (24. * 3600. * 1000.);
你可以double
在里面指定duration
:
auto some_time = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::duration<double>>(some_time).count();