HI如何打印时间在宏观第二也与使用下面的C++函数的,我使用,如何使用ctime()函数在微秒内打印时间?
time_t rawtime;
time(&rawtime);
std::cout<<"Cuuent TIme is :: "<< ctime(&rawtime)<< std::endl;
上面的代码给我唯一的小时,分钟和秒。我也需要微秒。 任何建议如何获得微秒的时间呢?
HI如何打印时间在宏观第二也与使用下面的C++函数的,我使用,如何使用ctime()函数在微秒内打印时间?
time_t rawtime;
time(&rawtime);
std::cout<<"Cuuent TIme is :: "<< ctime(&rawtime)<< std::endl;
上面的代码给我唯一的小时,分钟和秒。我也需要微秒。 任何建议如何获得微秒的时间呢?
我用下面的代码在H,M,S,微软获得时间:
char timeBuf [256];
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
sprintf (timeBuf, "%02d:%02d:%02d:%03ld",tm->tm_hour, tm->tm_min, tm->tm_sec, (tv.tv_usec/1000));
谢谢@CrazyC。 – BSalunke
BSalunke,看这一段代码。包括您使用的uSeconds和time_t结构。
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
struct timeval timeValue;
long uSeconds;
if (gettimeofday(&timeValue, NULL) == 0)
{
// This is what you have:
time_t time = timeValue.tv_sec;
// This is what you are looking for:
printf("Current milliseconds: %ld\n", timeValue.tv_usec);
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
的[?获取关于C中微秒时间戳(可能重复http://stackoverflow.com/questions/5833094/get-a-timestamp-in-c-in-microseconds) – Nim
有这么多的重复项 - 上面的是一个例子.. – Nim