linux C 获取当前时间

经常会遇到打印时间的问题,记录一下,方便自己和他人查阅。

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.883