Linux C語言時間相關知識總結

2023-05-09 10:13:51 來源:Linux大陸

大家好,我是LinuxZn。

實際開發中,經常要獲取各種時間。下面匯總幾個常用的時間接口:


【資料圖】

1、clock_gettime

#include/***@brief根據系統時鐘的類型,獲取當前時間**Detailedfunctiondescription**@param[in]__clock_id:系統時鐘的類型。常用取值:-CLOCK_REALTIME:從1970年1月1日到目前的時間-CLOCK_MONOTONIC:系統啟動時間-CLOCK_PROCESS_CPUTIME_ID:本進程運行時間-CLOCK_THREAD_CPUTIME_ID:本線程運行的時間*@param[out]__tp:存放當前的時間。**@return成功則返回0,失敗則返回-1*/intclock_gettime(clockid_t__clock_id,structtimespec*__tp);

timespec結構體:

structtimespec{__time_ttv_sec;/*Seconds.秒*/__syscall_slong_ttv_nsec;/*Nanoseconds.納秒*/};

例子:

#include#include#includelonglongget_clock_sys_time_ns(void){structtimespectp;longlongtime_ns=0;clock_gettime(CLOCK_MONOTONIC,&tp);time_ns=(longlong)tp.tv_sec*1000000000+tp.tv_nsec;returntime_ns;}intmain(void){structtimespectp;///<獲取從1970年1月1日到目前的時間memset(&tp,0,sizeof(structtimespec));clock_gettime(CLOCK_REALTIME,&tp);printf("clock_id=CLOCK_REALTIME,sec=%ld,nsec=%ld",tp.tv_sec,tp.tv_nsec);///<獲取系統啟動時間memset(&tp,0,sizeof(structtimespec));clock_gettime(CLOCK_MONOTONIC,&tp);printf("clock_id=CLOCK_MONOTONIC,sec=%ld,nsec=%ld,sys_time=%lldns",tp.tv_sec,tp.tv_nsec,get_clock_sys_time_ns());///<獲取本進程運行時間memset(&tp,0,sizeof(structtimespec));clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp);printf("clock_id=CLOCK_PROCESS_CPUTIME_ID,sec=%ld,nsec=%ld",tp.tv_sec,tp.tv_nsec);///<獲取本線程運行時間memset(&tp,0,sizeof(structtimespec));clock_gettime(CLOCK_THREAD_CPUTIME_ID,&tp);printf("clock_id=CLOCK_THREAD_CPUTIME_ID,sec=%ld,nsec=%ld",tp.tv_sec,tp.tv_nsec);return0;}

編譯、運行:

2、gettimeofday

#include/***@brief獲取當前時間(從1970年1月1日到目前的時間)**Detailedfunctiondescription**@param[out]tv:當前UTC時間*@param[out]tz:當前時區信息**@return成功則返回0,失敗則返回-1*/intgettimeofday(structtimeval*tv,structtimezone*tz);

timeval結構體:

structtimeval{__time_ttv_sec;/*Seconds.秒*/__suseconds_ttv_usec;/*Microseconds.微秒*/};

timezone結構體:

structtimezone{inttz_minuteswest;/*MinuteswestofGMT.和Greenwich時間差了多少分鐘*/inttz_dsttime;/*NonzeroifDSTiseverineffect.日光節約時間的狀態*/};

例子:

#include#include#includelonglongget_sys_time_ms(void){longlongtime_ms=0;structtimevaltv;gettimeofday(&tv,NULL);time_ms=((longlong)tv.tv_sec*1000000+tv.tv_usec)/1000;returntime_ms;}intmain(void){///<獲取系統時間printf("sys_time=%lldms",get_sys_time_ms());return0;}

編譯、運行:

3、time

#include/***@brief獲取1970-01-010000+0000至今的秒數(UTC)**Detailedfunctiondescription**@param[out]tloc:返回的秒存儲指針**@return成功則返回秒數,失敗則返回-1,錯誤原因存在errno中。*/time_ttime(time_t*tloc);

time_t的類型:

typedeflongtime_t;

例子:

#include#includetime_tget_utc_time(void){returntime(NULL);}intmain(intargc,char**argv){time_tutc_time=get_utc_time();printf("utc_time=%lds",utc_time);return0;}

編譯、運行:

4、localtime

#include/***@brief將time_t類型的時間轉換為structtm類型的時間**Detailedfunctiondescription**@param[in]timep:當前UTC秒數**@return返回當地時間*/structtm*localtime(consttime_t*timep);

tm結構體:

structtm{inttm_sec;/*Seconds.[0-60](1leapsecond)*/inttm_min;/*Minutes.[0-59]*/inttm_hour;/*Hours.[0-23]*/inttm_mday;/*Day.[1-31]*/inttm_mon;/*Month.[0-11]注意:0代表1月,以此類推*/inttm_year;/*Year-1900.該值為實際年份減去1900*/inttm_wday;/*Dayofweek.[0-6]注意:0代表星期一,以此類推*/inttm_yday;/*Daysinyear.[0-365]從每年的1月1日開始的天數,其中0代表1月1日,以此類推*/inttm_isdst;/*DST.[-1/0/1]夏玲時標識符*/};

例子:

#include#includetime_tget_utc_time(void){returntime(NULL);}intmain(intargc,char**argv){time_tutc_time=get_utc_time();printf("utc_time=%lds",utc_time);structtm*local_tm=localtime(&utc_time);printf("localtime=%.4d-%.2d-%.2d%.2d:%.2d:%.2d",local_tm->tm_year+1900,local_tm->tm_mon+1,local_tm->tm_mday,local_tm->tm_hour,local_tm->tm_min,local_tm->tm_sec);return0;}

編譯、運行:

5、localtime_r

#include/***@brief將time_t類型的時間轉換為structtm類型的時間**Detailedfunctiondescription**@param[in]timep:當前UTC秒數*@param[out]timep:當地時間**@return返回當地時間*/structtm*localtime_r(consttime_t*timep,structtm*result);

localtime不是一個線程安全的函數,關于線程安全的知識點,看閱讀往期文章:如何理解線程安全?。

對于實時性要求較高的系統,多個線程同時調用localtime,可能會造成數據被覆蓋。我們項目中之前是用localtime來獲取系統時間、日期。并使用這個數據去做邏輯,數據異常導致了邏輯異常。

后面使用localtime_r來替代,問題解決。

例子:

#include#includetime_tget_utc_time(void){returntime(NULL);}intmain(intargc,char**argv){time_tutc_time=get_utc_time();printf("utc_time=%lds",utc_time);structtmresult;structtm*local_tm=localtime_r(&utc_time,&result);printf("localtime=%.4d-%.2d-%.2d%.2d:%.2d:%.2d",local_tm->tm_year+1900,local_tm->tm_mon+1,local_tm->tm_mday,local_tm->tm_hour,local_tm->tm_min,local_tm->tm_sec);printf("resulttime=%.4d-%.2d-%.2d%.2d:%.2d:%.2d",result.tm_year+1900,result.tm_mon+1,result.tm_mday,result.tm_hour,result.tm_min,result.tm_sec);return0;}

編譯、運行:

6、gmtime

#include/***@brief返回tm結構的GMT時間(UTC時間)**Detailedfunctiondescription**@param[in]timep:當前UTC秒數**@return返回當地時間*/structtm*gmtime(consttime_t*timep);

例子:

#include#includetime_tget_utc_time(void){returntime(NULL);}intmain(intargc,char**argv){time_tutc_time=get_utc_time();printf("utc_time=%lds",utc_time);structtm*gmt_tm=gmtime(&utc_time);printf("gmttime=%.4d-%.2d-%.2d%.2d:%.2d:%.2d",gmt_tm->tm_year+1900,gmt_tm->tm_mon+1,gmt_tm->tm_mday,gmt_tm->tm_hour,gmt_tm->tm_min,gmt_tm->tm_sec);return0;}

編譯、運行:

localtime和gmtime的區別?

localtime和gmtime都是C語言中的函數,用于將time_t類型的時間轉換為struct tm類型的時間。它們的區別在于,gmtime將time_t轉換為UTC時間,即世界標準時間,而localtime將time_t轉換為本地時間。

例子:使用gmtime與localtime接口返回的小時數來計算當地時區

#include#includetime_tget_utc_time(void){returntime(NULL);}intmain(intargc,char**argv){time_tutc_time=get_utc_time();printf("utc_time=%lds",utc_time);structtm*gmt_tm=gmtime(&utc_time);printf("gmttime=%.4d-%.2d-%.2d%.2d:%.2d:%.2d",gmt_tm->tm_year+1900,gmt_tm->tm_mon+1,gmt_tm->tm_mday,gmt_tm->tm_hour,gmt_tm->tm_min,gmt_tm->tm_sec);intgmt_hour=gmt_tm->tm_hour;structtm*local_tm=localtime(&utc_time);printf("localtime=%.4d-%.2d-%.2d%.2d:%.2d:%.2d",local_tm->tm_year+1900,local_tm->tm_mon+1,local_tm->tm_mday,local_tm->tm_hour,local_tm->tm_min,local_tm->tm_sec);intlocal_hour=local_tm->tm_hour;intlocal_time_zone=local_hour-gmt_hour;if(local_time_zone<-12){local_time_zone+=24;}elseif(local_time_zone>12){local_time_zone-=24;}else{}printf("local_time_zone=%d",local_time_zone);return0;}

編譯、運行:

以上就是本次的分享,如果文章有幫助,麻煩幫忙轉發,謝謝!

審核編輯:湯梓紅

標簽:

上一篇:板級埋人式封裝工藝流程與技術
下一篇:最后一頁