經常會遇到打印時間的問題,記錄一下,方便自己和他人查閱。
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。