linux時間函式

2021-09-06 04:32:29 字數 1246 閱讀 6419

1、時間型別。linux下常用的時間型別:time_t,struct timeval,struct tm

(1)time_t是乙個長整型,一般用來表示用2023年以來的秒數。

(2)struct timeval有兩個成員,乙個是秒,乙個是微妙。

struct timeval

{long tv_sec;       

long tv_usec;  

(3)struct tm是直觀意義上的時間表示方法:

struct tm {

int     tm_sec;         

int     tm_min;       

int     tm_hour;        

int     tm_mday;      

int     tm_mon;        

int     tm_year;        

int     tm_wday;        

int     tm_yday;       

int     tm_isdst;      

2、 時間操作

(1) 時間格式間的轉換函式

主要是 time_t、struct tm、時間的字串格式之間的轉換。看下面的函式引數型別以及返回值型別:

char *asctime(const struct tm *tm);

char *ctime(const time_t *timep);

struct tm *gmtime(const time_t *timep);  //返回的是格林威治時間

struct tm *localtime(const time_t *timep);  //返回的是本地時間

time_t mktime(struct tm *tm);

(2) 獲取時間函式

兩個函式,獲取的時間型別看原型就知道了:

time_t time(time_t *t);

int gettimeofday(struct timeval *tv, struct timezone *tz);

前者獲取time_t型別,後者獲取struct timeval型別,因為型別的緣故,前者只能精確到秒,後者可以精確到微秒。相對於gettimeofday,還有settimeofday用來設定時間

linux時間函式

1 時間型別。linux下常用的時間型別 time t,struct timeval,struct tm 1 time t是乙個長整型,一般用來表示用1970年以來的秒數。2 struct timeval有兩個成員,乙個是秒,乙個是微妙。struct timeval long tv sec long...

Linux時間函式

一。時間程式設計 1.核心理論 1 時間型別 2.函式學習 1 獲取日曆時間 函式名 time 函式原型 time t time time t t 函式功能 獲取當前日曆時間 所屬標頭檔案 返回值 成功時 返回日曆時間 失敗時 返回 1 引數說明 t 不為空的儲存返回值 2 獲取格林威治時間 函式名...

linux時間函式gettimeofday解析

我們在程式中會頻繁地取當前時間,例如處理乙個http請求時,兩次呼叫gettimeofday取差值計算出處理該請求消耗了多少秒。這樣的呼叫無處不在,所以我們有必要詳細了解下,gettimeofday這個函式做了些什麼?核心1ms一次的時鐘中斷處理真的可以支援tv usec欄位達到微秒精度嗎?它的呼叫...