1、時間型別。linux下常用的時間型別有4個:time_t,struct timeb, struct timeval,struct timespec,clock_t, struct tm.
(1) time_t是乙個長整型,一般用來表示用2023年以來的秒數.
該型別定義在中.
一般通過 time_t time = time(null); 獲取.
(2) struct timeb結構: 主要有兩個成員, 乙個是秒, 另乙個是毫秒, 精確度為毫秒.
struct timeb;
由函式int ftime(struct timeb *tp); 來獲取timeb.
成功返回0, 失敗返回-1.
(3) struct timeval有兩個成員,乙個是秒,乙個是微妙.
struct timeval ;
由int gettimeofday(struct timeval *tv, struct timezone *tz);獲取.
struct timezone結構的定義為:
struct timezone;
(4) struct timespec有兩個成員,乙個是秒,乙個是納秒, 所以最高精確度是納秒.
struct timespec;
一般由函式long clock_gettime (clockid_t which_clock, struct timespec *tp); 獲取.
獲取特定時鐘的時間,時間通過tp結構傳回,目前定義了6種時鐘,分別是
clock_realtime 統當前時間,從2023年1.1日算起
clock_monotonic 系統的啟動時間,不能被設定
clock_process_cputime_id 程序執行時間
clock_thread_cputime_id 執行緒執行時間
clock_realtime_hr clock_realtime的高精度版本
clock_monotonic_hr clock_monotonic的高精度版本
獲取特定時鐘的時間精度:
long clock_getres(clockid_t );
long clock_settime(clockid_t ,struct timespec*);
long clock_nanosleep(clockid_t ,int flag,timespec* time,timespec* left_time);
(5) clock_t型別, 由clock_t clock(); 返回獲取.
表示程序占用的cpu時間. 精確到微秒.
(6)struct tm是直觀意義上的時間表示方法:
struct tm ;
文章**
linux下的時間函式及
1.可以用 localtime 函式分別獲取年月日時分秒的數值。在所有的unix下,都有個time 的函式 格式 time t time time t t 這個函式會傳回格林尼治時間,如果t是non null,它將會把時間值填入t中。include include int main 說明 time ...
linux下時間有關的函式和結構體
1 時間型別。linux下常用的時間型別有4個 time t,struct timeb,struct timeval,struct timespec,clock t,struct tm.1 time t是乙個長整型,一般用來表示用1970年以來的秒數.該型別定義在中.一般通過 time t time...
linux下獲取時間函式
1 gettimeofday 獲取的時間為從1970 1 1 00 00 00到目前為止的時間。當使用該函式獲取ms級別時間時,需注意溢位問題。1 首先來看一下函式原型 int gettimeofday struct timeval tv,struct timezone tz 其中結構體struct...