在linux中的時間有
time_t和struct tm
time_t的定義為type long time_t
time_t描述的時間是從2023年1月1日0時0分0秒到獲取時間的那一秒的秒數。
函式time(null)可以獲取當前的時間並以time_t的型別返回。
time_t 獲取到的時間不夠直觀,為了直觀地描述時間,採用了tm結構體的形式,tm結構體的內容為:
struct tm = ;
time_t t =
time
(null);
//獲取當前時間
printf
("time_t = %ld\n\n"
, t);
struct tm
*tt=
gmtime
(&t);
//轉換成格林尼治時間
printf
("gmtime:\n"
);
printf
("%d-%d-%d "
, (tt->tm_year+
1900
), (tt->tm_mon+
1), tt->tm_mday);
printf
("%s
%d:%d:%d\n\n"
, wday[tt->tm_wday], tt->tm_hour, tt->tm_min, tt->tm_sec);
tt =
localtime
(&t);
//轉換成當地時間
printf
("localtime:\n"
);
printf
("%d-%d-%d "
, (tt->tm_year+
1900
), (tt->tm_mon+
1), tt->tm_mday);
printf
("%s
%d:%d:%d\n\n"
, wday[tt->tm_wday], tt->tm_hour, tt->tm_min, tt->tm_sec);
time_t ttt = mktime(tt);
//將當地時間轉換回 time_t 型別
printf
("mktime:\n"
);
printf
("time_t = %ld\n"
, ttt);
return0;}
Linux網路程式設計中的時間函式
目錄gmtime localtime timegm mktime strftime struct tm 是等與當前時間無關的時間格式轉換函式。用於讓程式等待一段時間或安排計畫任務 對於多執行緒服務端程式設計 gettimeofday 2 原型 include int gettimeofday str...
linux 時間程式設計
時間獲取 include time t time time t tloc struct tm gmtime const time t timep 將日曆時間轉化為格林威治標準時間,儲存在tm結構中。struct tm localtime const time t timep 將日曆時間轉化為本地時間...
linux時間程式設計
關於linux下時間程式設計的問題 1.linux下與時間有關的結構體 struct timeval 其中tv sec是由凌晨開始算起的秒數,tv usec則是微秒 10e 6 second struct timezone tv minuteswest是格林威治時間往西方的時差,tv dsttime...