将steady_clock :: time_point转换为time_t [英] Converting steady_clock::time_point to time_t

查看:1901
本文介绍了将steady_clock :: time_point转换为time_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 steady_clock 保存某些邮件的时间戳。

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

对于其他时钟,ther的静态函数 to_time_t ,但是在GCC(MinGW 4.8.0)上,此功能不存在。

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

现在我打印如下:

Timestamp: 26735259098242

所以我不能使用 system_clock 或其他。

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

编辑打印从 time_since_epoch()。count ()

推荐答案

假设您需要内部计算的稳定行为,而不是显示,可以使用这个函数转换为 time_t 来显示。 p>

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()))
}

如果你需要稳定的日志记录,在启动时获得(system_clock :: now(),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.

这篇关于将steady_clock :: time_point转换为time_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆