简体   繁体   中英

Get a double from boost::chrono::steady_clock::now()

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.

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. 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 :

auto some_time = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration_cast<std::chrono::duration<double>>(some_time).count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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