Converting steady_clock::time_point to time_t

Assuming you need the steady behavior for internal calculations, and not for display, here’s a function you can use to convert to time_t for display.

using std::chrono::steady_clock;
using std::chrono::system_clock;

time_t steady_clock_to_time_t( steady_clock::time_point t )
{
    return system_clock::to_time_t(system_clock::now()
                 + duration_cast<system_clock::duration>(t - steady_clock::now()));
}

If you need steady behavior for logging, you’d want to get one ( system_clock::now(), steady_clock::now() ) pair at startup and use that forever after.

Leave a Comment