使用 C++ 获取当前时间(以毫秒为单位)和升压
在我的线程中(使用 boost::thread )我需要检索当前时间(以毫秒为单位或更少)并转换为毫秒:
实际上,阅读这里我发现了这个:
tick = boost::posix_time::second_clock::local_time();
now = boost::posix_time::second_clock::local_time();
并且似乎可以工作,但是在我需要很长时间之后现在的毫秒值...
我该怎么做?
In my thread (using boost::thread) I need 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 it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
boost::posix_time::time_duration
来获取时间范围。例如这样,为了获得更高分辨率,您可以更改您正在使用的时钟。例如
boost::posix_time::microsec_clock
,尽管这可能取决于操作系统。例如,在 Windows 上,boost::posix_time::microsecond_clock
具有毫秒分辨率,而不是微秒。一个稍微依赖于硬件的示例。
在我的win7机器上。第一个输出是 0 或 1000。第二个分辨率。
由于时钟的分辨率更高,第二个几乎总是 500。我希望能有所帮助。
You can use
boost::posix_time::time_duration
to get the time range. E.g like thisAnd 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.
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.
如果你的意思是自纪元以来的毫秒数,你可以这样做
但是,目前还不清楚你想要什么。
查看 DateTime 文档中的示例 增强日期时间
If you mean milliseconds since epoch you could do
However, it's not particularly clear what you're after.
Have a look at the example in the documentation for DateTime at Boost Date Time
试试这个:如上所述导入标头..仅给出秒和毫秒。如果您需要解释代码,请阅读 此链接。
Try this: import headers as mentioned.. gives seconds and milliseconds only. If you need to explain the code read this link.