Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert "std::chrono::system_clock::now()" to double

auto current_time = std::chrono::system_clock::now();

Using std::chrono in C++ I get current time like above.
How can I use it to get the number of seconds since the clock's epoch as a double?

like image 528
TheWaterProgrammer Avatar asked Oct 14 '25 14:10

TheWaterProgrammer


1 Answers

time_since_epoch can give you a duration since the clock's epoch (whatever the epoch may be):

auto current_time = std::chrono::system_clock::now();
auto duration_in_seconds = std::chrono::duration<double>(current_time.time_since_epoch());

double num_seconds = duration_in_seconds.count();

Demo

like image 110
AndyG Avatar answered Oct 18 '25 02:10

AndyG