C ++计时系统时间(以毫秒为单位),时间操作 [英] C++ chrono system time in miliseconds, time operations

查看:1019
本文介绍了C ++计时系统时间(以毫秒为单位),时间操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,由C ++ 11的文档不足造成。

I've got a small problem caused by insufficient documentation of C++11.

我想获得一个自纪元以来的时间,以毫秒或纳秒或秒,然后我将不得不转这个值到另一个分辨率。
我可以使用gettimeofday(),但它会很容易,所以我试图使用std :: chrono实现它。

I'd like to obtain a time since epoch in milliseconds, or nanoseconds or seconds and then I will have to "cast" this value to another resolution. I can do it using gettimeofday() but it will be to easy, so I tried to achieve it using std::chrono.

我试过: / p>

I tried:

std::chrono::time_point<std::chrono::system_clock> now = 
    std::chrono::system_clock::now();

但我不知道是什么是以这种方式获得的分辨率time_point,知道如何获得这个时间作为一个简单的unsigned long long,我没有任何概念如何把它转换为另一个解决方案。

But I have no idea what is a resolution of obtained in this way time_point, and I don't know how to get this time as a simple unsigned long long, and I haven't any conception how to cast it to another resolution.

推荐答案

您可以 now.time_since_epoch()获取代表自纪元以来的时间的持续时间,其中<​​a href =http://stackoverflow.com/q / 8386128>时钟的分辨率。要转换为毫秒,请使用 duration_cast

You can do now.time_since_epoch() to get a duration representing the time since the epoch, with the clock's resolution. To convert to milliseconds use duration_cast:

auto duration = now.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();

这篇关于C ++计时系统时间(以毫秒为单位),时间操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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