Search code examples
c++11doublec++-chrono

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?


Solution

  • 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