/////////////////////////////////////////////////////
//獲取系統當前時間,並轉換為當地時間顯示
/////////////////////////////////////////////////////
#include <stdio.h>
#include <time.h>
int main (void)
{
time_t now;
struct tm *ptm;
//time() returns the time as the number of seconds since the Epoch,
//1970-01-01 00:00:00 +0000 (UTC)
//這里只能得到當前距離某個時間的總秒數,需要進一步轉換
time (&now);
//獲取當地日期和時間
ptm = localtime (&now);
//將轉換后的時間以字符串形式顯示
printf ("now: %s", asctime(ptm));
return 0;
}
結果
linux@ubuntu:~/c_file$ ./get_time
now: Sun Dec 8 11:56:59 2019