Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to cast a double into std::chrono::milliseconds

I am using boost::asio::steady_timer m_timer and if I am not mistaken, in order to call m_timer.expires_after(expiration_time_ms);, expiration_time_ms should be a std::chrono::milleseconds variable.

Nevertheless, in my case, I have the expiration time as a double. I would like to know if it is possible to cast a double into std::chrono::milliseconds

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One nice trick is to multiply your values with chrono literals:

using namespace std::chrono_literals;

double time = 82.0;
auto t_82ms = time * 1ms;
std::this_thread::sleep_for(t_82ms);

It also works the other way around:

double time = t_82ms / 1s; // time == 0.082
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading