Linux获取系统时间,精确到毫秒
程序员文章站
2024-01-24 09:12:40
...
string GetDateTime()
{
char strTime[30];
struct timeval tv;
struct timezone tz;
struct tm *t;
gettimeofday(&tv, &tz);
t = localtime(&tv.tv_sec);
snprintf(strTime, 30, "%04d%02d%02d_%02d%02d%02d.%03d",
1900+t->tm_year, 1+t->tm_mon, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
return string(strTime);
}