Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

c++ - Converting steady_clock::time_point to time_t

I'm using the steady_clock for saving the time stamp of some messages. For debug purpose is usefull to have the calendar (or something similar).

For other clocks ther's the static function to_time_t, but on GCC (MinGW 4.8.0) this function is not present.

Now i print something like:

Timestamp: 26735259098242

For timestamp i need a steady_clock so I cannot use system_clock or others.

Edit The previous print is given from the time_since_epoch().count()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()
                                          + (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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...