C ++得到提振以毫秒为单位的当前时间 [英] c++ boost get current time in milliseconds

查看:136
本文介绍了C ++得到提振以毫秒为单位的当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的螺纹(使用boost ::线程)来检索在毫秒或更少的当前时间,并转换成毫秒

I need in my thread (using boost::thread) to retrieve the current time in ms or less and to convert into ms:

其实,看完这里我发现这一点:

actually, reading here I've found this:

tick = boost::posix_time::second_clock::local_time();
now  = boost::posix_time::second_clock::local_time();

和似乎工作,但经过我需要的毫秒长值现在...
我能怎么做?
在此先感谢

and seems to work, but after I need to have a long value of the milliseconds of the now... how can I do? thanks in advance

推荐答案

您可以使用的boost ::了posix_time :: TIME_DURATION 来获取时间范围。例如像这样

You can use boost::posix_time::time_duration to get the time range. E.g like this

boost::posix_time::time_duration diff = tick - now;
diff.total_milliseconds();

和获得更高的分辨率,您可以更改您正在使用的时钟。例如在的boost ::了posix_time :: microsec_clock ,虽然这可能是与操作系统有关。在Windows上,例如,的boost ::了posix_time :: microsecond_clock 具有毫秒的分辨率,而不是微秒。

And to get a higher resolution you can change the clock you are using. For example to the boost::posix_time::microsec_clock, though this can be OS dependent. On Windows, for example, boost::posix_time::microsecond_clock has milisecond resolution, not microsecond.

这是实施例这是一个小依赖于硬件

An example which is a little dependent on the hardware.

int main(int argc, char* argv[])
{
    boost::posix_time::ptime t1 = boost::posix_time::second_clock::local_time();
    boost::this_thread::sleep(boost::posix_time::millisec(500));
    boost::posix_time::ptime t2 = boost::posix_time::second_clock::local_time();
    boost::posix_time::time_duration diff = t2 - t1;
    std::cout << diff.total_milliseconds() << std::endl;

    boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
    boost::this_thread::sleep(boost::posix_time::millisec(500));
    boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();
    boost::posix_time::time_duration msdiff = mst2 - mst1;
    std::cout << msdiff.total_milliseconds() << std::endl;
    return 0;
}

在我的win7的机器。第一个出来的是0或1000的第二项决议案。
第二个是几乎总是500,因为该较高分辨率的时钟的。我希望有点帮助。

On my win7 machine. The first out is either 0 or 1000. Second resolution. The second one is nearly always 500, because of the higher resolution of the clock. I hope that help a little.

这篇关于C ++得到提振以毫秒为单位的当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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