Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The aim is to call

void
setExpirationTime(my_casted_double) {
  boost::asio::steady_timer m_timer;
  m_timer.expires_after(my_casted_double)
}
like image 869
bellotas Avatar asked Oct 18 '25 12:10

bellotas


1 Answers

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
like image 117
m88 Avatar answered Oct 21 '25 01:10

m88