代碼如下:詳細見注釋
#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; }
?