捕获以毫秒为单位的时间
下面的代码用于打印日志中的时间:
#define PRINTTIME() struct tm * tmptime;
time_t tmpGetTime;
time(&tmpGetTime);
tmptime = localtime(&tmpGetTime);
cout << tmptime->tm_mday << "/" <<tmptime->tm_mon+1 << "/" << 1900+tmptime->tm_year << " " << tmptime->tm_hour << ":" << tmptime->tm_min << ":" << tmptime->tm_sec<<">>";
有什么方法可以添加毫秒吗?
The following piece of code is used to print the time in the logs:
#define PRINTTIME() struct tm * tmptime;
time_t tmpGetTime;
time(&tmpGetTime);
tmptime = localtime(&tmpGetTime);
cout << tmptime->tm_mday << "/" <<tmptime->tm_mon+1 << "/" << 1900+tmptime->tm_year << " " << tmptime->tm_hour << ":" << tmptime->tm_min << ":" << tmptime->tm_sec<<">>";
Is there any way to add milliseconds to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要获得毫秒精度,您必须使用特定于您的操作系统的系统调用。
在 Linux 中,您可以使用
具有微秒精度的
timeval
。在 Windows 中,您可以使用:
当然您可以使用 Boost 来为您做到这一点:)
To have millisecond precision you have to use system calls specific to your OS.
In Linux you can use
timeval
has microsecond precision.In Windows you can use:
Of course you can use Boost to do that for you :)
//C++11 风格:
//C++11 Style:
您需要一个更高分辨率的计时器才能捕获毫秒。 试试这个:
这当然取决于您的操作系统。
You need a timer with a higher resolution in order to capture milliseconds. Try this:
This is of course dependent on your OS.
高分辨率计时器在 Linux 风格平台上通常是 gettimeofday,在 Windows 平台上通常是 QueryPerformanceCounter。
您应该意识到,对单个操作的持续时间进行计时(即使使用高分辨率计时器)也不会产生准确的结果。 有太多的随机因素在起作用。 为了获得可靠的计时信息,您应该循环运行要计时的任务并计算平均任务时间。 对于这种类型的计时,clock() 函数应该足够了。
The high resolution timers are usually gettimeofday on Linux style platforms and QueryPerformanceCounter on Windows.
You should be aware that timing the duration of a single operation (even with a high resolution timer) will not yield accurate results. There are too many random factors at play. To get reliable timing information, you should run the task to be timed in a loop and compute the average task time. For this type of timing, the clock() function should be sufficient.
如果您不想使用任何特定于操作系统的代码,可以使用 ACE 包,它为大多数标准操作系统提供
ACE_OS::gettimeofday
函数。例如:
无论您的操作系统如何,此代码都可以工作(只要 ACE 支持此操作系统)。
If you don't want to use any OS-specific code, you can use the ACE package which supplies the
ACE_OS::gettimeofday
function for most standard operating systems.For example:
This code will work regardless of your OS (as long as ACE supports this OS).
在 Ubuntu 16.04 中,这对我有用...
然后,...
我得到...
In Ubuntu 16.04 this worked for me...
Then, with...
I get...
使用 C++11 或 C++14 和这个免费开源库:
这只是为我输出:
这是我当前的本地日期和时间,精确到毫秒。 通过消除
floor()
,您将自动获得system_clock
的精度。如果您想要结果为 UTC 时间戳而不是本地时间戳,那就更简单了:
如果您想要 UTC 时间戳并且对精度或格式不挑剔,您可以:
这只是为我输出:
New answer for old question using C++11 or C++14 and this free, open-source library:
This just output for me:
which is my current local date and time to millisecond precision. By eliminating the
floor<milliseconds>()
you will automatically get whatever precision yoursystem_clock
has.If you wanted the result as a UTC timestamp instead of a local timestamp, it is even easier:
And if you want a UTC timestamp and you aren't picky about the precision or the format, you can just:
which just output for me: