常常会遇到打印时间的问题,记录一下,方便本身和他人查阅。linux
struct tm nowtime; struct timeval tv; unsigned char time_now[128]; gettimeofday(&tv, NULL); localtime_r(&tv.tv_sec,&nowtime); sprintf(time_now,"%d-%d-%d %d:%d:%d.%03d ", nowtime.tm_year+1900, nowtime.tm_mon+1, nowtime.tm_mday, nowtime.tm_hour, nowtime.tm_min, nowtime.tm_sec, (int)(tv.tv_usec/1000) ); printf("current time is %s\n",time_now);
哈哈,获取时间就是这么简单。
打印结果以下:
current time is 2018-7-28 15:10:47.883ide