我使用这段代码在 c/c++ 中以微秒为单位获取时间戳。但它看起来不像微秒。我也不知道有没有办法格式化。
timeval curTime;
gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000;
unsigned long micro = curTime.tv_usec*(uint64_t)1000000+curTime.tv_usec;
char buffer [80];
//localtime is not thread safe
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&curTime.tv_sec));
char currentTime[84] = "";
char currentTime2[80] = "";
sprintf(currentTime, "%s:%3d", buffer, milli);
sprintf(currentTime2, "%s:%Lu", buffer, micro);
printf("time %s, hptime %s\n", currentTime, currentTime2);
什么是正确的输出格式?谢谢!