linux C 獲取當前時間

linux C 獲取當前時間

常常會遇到打印時間的問題,記錄一下,方便本身和他人查閱。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

相關文章
相關標籤/搜索