简体   繁体   English

从boost :: chrono :: steady_clock :: now()获取双倍

[英]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. 我有一个方法无法解析提升返回。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM