C ++ chrono系统时间(以毫秒为单位),时间操作 [英] C++ chrono system time in milliseconds, time operations
问题描述
由于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.
我尝试了:
std::chrono::time_point<std::chrono::system_clock> now =
std::chrono::system_clock::now();
但是我不知道以这种方式获得的分辨率是time_point,我不知道知道如何将这段时间作为一个简单的无符号长久格式,而且我对如何将其转换为另一个分辨率没有任何想法。
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()
以获取表示自该纪元以来的时间的持续时间,并使用时钟的分辨率。要转换为毫秒,请使用 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 ++ chrono系统时间(以毫秒为单位),时间操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!