#include<sys/time.h>
struct timeval start1;
gettimeofday(&start1,0);
sprintf(cMsg,"%d:%d\n",start1.tv_sec,start1.tv_usec);
printf(cMsg);
————————————————————————————————————————————
#include <sys/time.h>
#include <unistd.h>
#include <iostream>
string TimeToString()
{
char charFormat[30];
struct timeval nowtimeval;
gettimeofday(&nowtimeval,0);
time_t now;
struct tm *timenow;
time(&now);
timenow = localtime(&now);
sprintf(charFormat,
"%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d%3.3d",
timenow->tm_year + 1900,
timenow->tm_mon + 1,
timenow->tm_mday,
timenow->tm_hour,
timenow->tm_min,
timenow->tm_sec,
nowtimeval.tv_usec/1000);
return charFormat;
}
由于获取微秒和和获取当前年月日分两次获取,所以会存在误差