共計 6032 個字符,預計需要花費 16 分鐘才能閱讀完成。
這篇“linux 是否有計算時間的函數”文章的知識點大部分人都不太理解,所以丸趣 TV 小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“linux 是否有計算時間的函數”文章吧。
linux 有計算時間的函數,例如:可獲取秒級時間差的函數 time()、可獲取微秒級時間差的函數 gettimeofday() 和 settimeofday()、可獲取納秒級時間差的函數 clock_gettime() 等等。
1. 獲取時間相關函數
1.1 獲取秒級時間差函數
#include time.h
time_t time(time_t *timer);// 通過函數返回值或者 timer 變量均可以獲取到當前時間
time_t 實際上是一個長整型,表示 UTC 時間(1970 年 1 月 1 日 0 時 0 分 0 秒,Linux 系統的 Epoch 時間)到當前系統時間的秒數級時間差
1.2 獲取微秒級時間差函數
#include sys/time.h
#include unistd.h
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
struct timezone{
int tz_minuteswest; /*miniutes west of Greenwich*/
int tz_dsttime; /*type of DST correction*/
// 函數執行成功返回 0,失敗返回 -1. 其中 timezone 是時區相關的結構體
int gettimeofday(struct timeval *tv, struct timezone *tz);
// 用來設置制定的時間和時區信息
int settimeofday(const struct timeval *tv, const struct timezone *gz);
1.3 獲取納秒級時間差函數
#include time.h
其中 clk_id 用來制定對應的時鐘類型,不同的類型可以用來獲取不同的時間值,具體有四種:CLOCK_REALTIME: 系統實時時間,從 UTC 開始計時,若時間被用戶更改計數時間相應改變;CLOCK_MONOTONIC: 從系統啟動開始計時,即使用戶更改時間也沒有影響;CLOCK_PROCESS_CPUTIME_ID: 本進程開始到執行到當前程序系統 CPU 花費的時間;CLOCK_THREAD_CPUTIME_ID: 本線程開始到執行到當前程序系統 CPU 花費的時間
struct timespec{
time_t tv_sec; //s
long tv_nsec; //ns
int clock_gettime(clockid_t clk_id, struct timespec* tp);
2. 轉換時間相關函數
2.1 將上述獲取時間函數獲取到的時間參數 time_t 轉換為結構體
struct tm,該結構體包含年月日等非常詳細的域。下如所示:
#include time.h
struct tm{
int tm_sec; // 秒
int tm_min; // 分
int tm_hour; // 時;取值區間為 [0, 23]
int tm_mday; // 日;取值區間為 [1, 31]
int tm_mon; // 月份;取值區間為 [0, 11]; 0 表示 1 月份依次遞增到 12 月份
int tm_year; // 年份;其值為 1900 年至今年數
int tm_wday; // 星期;0 代表星期天,1 代表星期 1,以此類推
int tm_yday; // 日期;0 代表 1 月 1 日
int tm_isdst; // 夏令時標識符;使用夏令時為正,不使用 t 為 0,不確定時為負 */
};
將 time_t 轉換成 struct tm 結構體常用的函數如下:
#include time.h
struct tm* gmtime(const time_t* timep);
struct tm*localtime(const time_t* timep);
gmtime() 和 localtime() 函數可以將 time_t 數據格式轉化成 tm 格式的類型。區別是 gmtime() 轉換的結果是 GMT(中央時區)對應的信息,而 localtime() 函數轉換的結果是當前所在時區的信息。
2.2 將 time_t 轉換成我們習慣性使用的時間和日期字符串
對應轉換函數如下:
#include time.h
char* ctime(time_t* timep);
2.3 將 struct tm 轉換成 time_t
對應函數如下:
#include time.h
time_t mktime(struct tm *p_tm);
2.4 將 struct tm 轉換成我們習慣性使用的時間和日期字符串
對應函數如下:
#include time.h
char *asctime(const struct tm *p_tm); // 習慣性字符串 Thu Dec 9 07:13:35 2021
2.5 將時間字符串轉換成 struct tm 格式
/**************************************
** description: 將 struct tm 按照指定的 format 格式轉化成字符串
** parameter:** *s : 需要被轉換的時間字符串
** *format:時間字符串的格式
** *tm:轉換后的 tm 時間
**************************************/
char *strptime(const char *s, const char *format, struct tm *tm);
2.6 將 struct tm 按照指定的 format 格式轉化成字符串
/**************************************
** description: 將 struct tm 按照指定的 format 格式轉化成字符串
** parameter:** *s : 生成的時間字符串
** max: 字符串最大字符數(即最大可生成的字符數量)** *format:生成的字符串格式
** *tm:需要被轉換的 tm 時間
**************************************/
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
3. 舉例
#include stdio.h
#include string.h
#include unistd.h
#include time.h
#include sys/time.h
int main(int argc, char *argv[])
char *pstr = NULL;
struct tm *ptm = NULL;
printf( ************** 使用 ctime 獲取時間 time_t **************\n
time_t times = 0;
time(times);
printf(time_t:%ld\n\n , times);
printf( ************** 使用 ctime 轉換 time_t 成我們習慣性使用的時間和日期字符串 **************\n
pstr = ctime(times);
printf(ctime:%s\n , pstr);
printf( ************** 使用 gmtime 轉換 time_t 成 struct tm 時間和日期 **************\n
ptm = gmtime(times);
printf(time : %d:%d:%d\n , ptm- tm_hour, ptm- tm_min, ptm- tm_sec);
printf(date: %d:%d:%d\n , ptm- tm_year+1900, ptm- tm_mon+1, ptm- tm_mday);
printf(year: wday:%d yday:%d isdst:%d\n\n , ptm- tm_wday, ptm- tm_yday, ptm- tm_isdst);
printf( ************** 使用 asctime 轉換 struct tm 成我們習慣性使用的時間和日期字符串 **************\n
pstr = asctime(ptm);
printf(asctime:%s\n\n , pstr);
printf( ************** 使用 localtime 轉換 time_t 成 struct tm 時間和日期 **************\n
ptm = localtime(times);
printf(time : %d:%d:%d\n , ptm- tm_hour, ptm- tm_min, ptm- tm_sec);
printf(date: %d:%d:%d\n , ptm- tm_year+1900, ptm- tm_mon+1, ptm- tm_mday);
printf(year: wday:%d yday:%d isdst:%d\n , ptm- tm_wday, ptm- tm_yday, ptm- tm_isdst);
pstr = asctime(ptm);
printf(asctime:%s\n\n , pstr);
printf( ************** 使用 gettimeofday 獲取微秒級的時間 **************\n
struct timeval tv;
struct timezone tz;
gettimeofday(tv, tz);
ptm = localtime(tv.tv_sec);
printf(time : %d:%d:%d\n , ptm- tm_hour, ptm- tm_min, ptm- tm_sec);
printf(date: %d:%d:%d\n , ptm- tm_year+1900, ptm- tm_mon+1, ptm- tm_mday);
printf(year: wday:%d yday:%d isdst:%d\n , ptm- tm_wday, ptm- tm_yday, ptm- tm_isdst);
printf(tv_usec:%ld\n\n , tv.tv_usec);
printf( ************** 使用 clock_gettime 獲取納秒級的時間 **************\n
struct timespec tp;
clock_gettime(CLOCK_REALTIME, tp);
ptm = localtime(tp.tv_sec);
printf(time : %d:%d:%d\n , ptm- tm_hour, ptm- tm_min, ptm- tm_sec);
printf(date: %d:%d:%d\n , ptm- tm_year+1900, ptm- tm_mon+1, ptm- tm_mday);
printf(year: wday:%d yday:%d isdst:%d\n , ptm- tm_wday, ptm- tm_yday, ptm- tm_isdst);
printf(tp.tv_nsec:%ld\n\n , tp.tv_nsec);
return 0;
}
特定的時間字符相互轉換
int str_to_time(void)
char pstr[128] = {0};
struct tm t;
strptime( 2021-04-23 12:34:56 , %Y-%m-%d %H:%M:%S ,
printf( **** tm_isdst: %d, tm_yday:%d, tm_wday%d,\n %d-%d-%d \n %d:%d:%d\n ,
t.tm_isdst, t.tm_yday, t.tm_wday, t.tm_year+1900, t.tm_mon+1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec);
printf(mktime ts:%ld\n , mktime( t));
printf(asctime:%s\n , asctime( t));
strftime(pstr, sizeof(pstr), %Y-%m-%d %H:%M:%S ,
printf(pstr:%s\n , pstr);
int time_to_str(void)
char pstr[128] = {0};
struct tm t = { .tm_sec = 56, /* Seconds (0-60) */
.tm_min = 34, /* Minutes (0-59) */
.tm_hour = 12, /* Hours (0-23) */
.tm_mday = 23, /* Day of the month (1-31) */
.tm_mon = 4-1, /* Month (0-11) */
.tm_year = 2021-1900, /* Year - 1900 */
.tm_wday = 5, /* Day of the week (0-6, Sunday = 0) */
.tm_yday = 113, /* Day in the year (0-365, 1 Jan = 0) */
.tm_isdst = 0, /* Daylight saving time */
};
strftime(pstr, sizeof(pstr), %Y-%m-%d %H:%M:%S ,
printf(pstr:%s\n , pstr);
}
若想獲取從系統啟動開始計時,即使用戶更改時間也沒有影響的時間,單位毫秒,舉例如下:
unsigned long long clock_systick_get(void)
int ret = -1;
unsigned long long time;
int cnt = 0;
struct timespec now = {0, 0};
while (ret 0 cnt 3)
{ ret = clock_gettime(CLOCK_MONOTONIC, now); // 獲取失敗重試,最大執行 3 次
cnt++;
}
time = now.tv_sec * 1000 + now.tv_nsec / (1000000);
return time;
}
以上就是關于“linux 是否有計算時間的函數”這篇文章的內容,相信大家都有了一定的了解,希望丸趣 TV 小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注丸趣 TV 行業資訊頻道。