Linux获取当前年月日后缀精确到微秒,例如2017-05-06T23:57:07.713171
代码如下:详细见注释
#include <stdio.h> #include <string.h> #include <time.h> #include <sys/time.h> int main() { struct timeval start; struct tm *local_time = NULL; static char str_time[100]; char ms[16]; gettimeofday( &start, NULL );//获取当前时间,该结构体返回Unix时间戳秒数与微秒数 local_time = localtime(&start.tv_sec);//将秒转换标准时间 strftime(str_time, sizeof(str_time), "%Y-%m-%dT%H:%M:%S", local_time); sprintf(ms,".%ld",start.tv_usec); strcat(str_time,ms);//将毫秒附着在时间后 printf("time: %s \n", str_time); return 0; }
Go as far as your heart will take you.