以下代码用于打印日志中的时间:
#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/