c++ - 以毫秒为单位捕获时间

标签 c++ stl timer resolution

以下代码用于打印日志中的时间:

#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<<">>";

有没有办法为此添加毫秒?

最佳答案

要获得毫秒精度,您必须使用特定于您的操作系统的系统调用。

在 Linux 中你可以使用

#include <sys/time.h>

timeval tv;
gettimeofday(&tv, 0);
// then convert struct tv to your needed ms precision

timeval 具有微秒精度。

在 Windows 中,您可以使用:

#include <Windows.h>

SYSTEMTIME st;
GetSystemTime(&st);
// then convert st to your precision needs

当然你可以使用Boost为你做到这一点:)

关于c++ - 以毫秒为单位捕获时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1120478/

相关文章:

c++ - 清除标准容器而不是构造和破坏它是否有意义?

c++ - for_each 中使用的函数的默认值

c++ - 按降序对 vector 进行排序

android - 如何在 Activity 中显示计时器?

java - Timer + gregorianCalendar.getTime() 未正确刷新日期

c++ - 预期表达式错误,发现 'WIN32'

c++ - 如何使用 C++ 以跨平台方式检查操作系统版本?

c++ - 如果在编译时两个维度都未知,如何传递二维数组

debugging - 如何使用 cygwin 工具链在 NetBeans 8.1 调试器中观看 C++ STL 集合?

c - 用一个物理定时器模拟多个虚拟定时器