使用 C++ 和 Boost 以毫秒为单位获取当前时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6734375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current time in milliseconds using C++ and Boost
提问by ghiboz
In my thread (using boost::thread) I need to retrieve the current time in ms or less and to convert into ms:
在我的线程中(使用 boost::thread),我需要以毫秒或更短的时间检索当前时间并转换为毫秒:
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?
我该怎么做?
回答by mkaes
You can use boost::posix_time::time_durationto get the time range. E.g like this
您可以使用boost::posix_time::time_duration来获取时间范围。比如像这样
boost::posix_time::time_duration diff = tick - now;
diff.total_milliseconds();
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_clockhas milisecond resolution, not microsecond.
为了获得更高的分辨率,您可以更改正在使用的时钟。例如对于boost::posix_time::microsec_clock,尽管这可能取决于操作系统。例如,在 Windows 上,boost::posix_time::microsecond_clock分辨率为毫秒,而不是微秒。
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;
}
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.
在我的 win7 机器上。第一个输出是 0 或 1000。第二个分辨率。第二个几乎总是 500,因为时钟的分辨率更高。我希望这有点帮助。
回答by Brian O'Kennedy
If you mean milliseconds since epochyou could do
如果你的意思是自纪元以来的毫秒数,你可以做
ptime time_t_epoch(date(1970,1,1));
ptime now = microsec_clock::local_time();
time_duration diff = now - time_t_epoch;
x = diff.total_milliseconds();
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
在Boost Date Time 的DateTime 文档中查看示例
回答by Macbeth's Enigma
// Get current date/time in milliseconds.
#include "boost/date_time/posix_time/posix_time.hpp"
namespace pt = boost::posix_time;
int main()
{
pt::ptime current_date_microseconds = pt::microsec_clock::local_time();
long milliseconds = current_date_microseconds.time_of_day().total_milliseconds();
pt::time_duration current_time_milliseconds = pt::milliseconds(milliseconds);
pt::ptime current_date_milliseconds(current_date_microseconds.date(),
current_time_milliseconds);
std::cout << "Microseconds: " << current_date_microseconds
<< " Milliseconds: " << current_date_milliseconds << std::endl;
// Microseconds: 2013-Jul-12 13:37:51.699548 Milliseconds: 2013-Jul-12 13:37:51.699000
}
回答by user1476945
Try this: import headers as mentioned.. gives seconds and milliseconds only. If you need to explain the code read this link.
试试这个: 如上所述的导入标头.. 只给出秒和毫秒。如果您需要解释代码,请阅读此链接。
#include <windows.h>
#include <stdio.h>
void main()
{
SYSTEMTIME st;
SYSTEMTIME lt;
GetSystemTime(&st);
// GetLocalTime(<);
printf("The system time is: %02d:%03d\n", st.wSecond, st.wMilliseconds);
// printf("The local time is: %02d:%03d\n", lt.wSecond, lt.wMilliseconds);
}

