linux有計算時間的函數,例如:可獲取秒級時間差的函數time()、可獲取微秒級時間差的函數gettimeofday()和settimeofday()、可獲取納秒級時間差的函數clock_gettime()等等。
本教程操作環境:linux7.3系統、Dell G3電腦。
下面講解的是linux中 時間相關的函數和將時間轉換相關函數
1.獲取時間相關函數
1.1 獲取秒級時間差函數
#include?<time.h> time_t?time(time_t?*timer);//通過函數返回值或者timer?變量均可以獲取到當前時間</time.h>
time_t實際上是一個長整型,表示UTC時間(1970年1月1日0時0分0秒,Linux系統的Epoch時間)到當前系統時間的秒數級時間差
1.2 獲取微秒級時間差函數
#include?<sys> #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);</unistd.h></sys>
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);</time.h>
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.h>
將time_t轉換成struct tm結構體常用的函數如下:
#include?<time.h>? struct?tm*?gmtime(const?time_t*?timep); struct?tm*localtime(const?time_t*?timep);</time.h>
gmtime() 和localtime() 函數可以將time_t 數據格式轉化成tm格式的類型。區別是gmtime()轉換的結果是GMT(中央時區)對應的信息,而localtime() 函數轉換的結果是當前所在時區的信息。
2.2將time_t轉換成我們習慣性使用的時間和日期字符串
對應轉換函數如下:
#include?<time.h> char*?ctime(time_t*?timep);</time.h>
2.3 將struct tm 轉換成 time_t
對應函數如下:
#include?<time.h> time_t?mktime(struct?tm?*p_tm);</time.h>
2.4將struct tm轉換成我們習慣性使用的時間和日期字符串
對應函數如下:
#include?<time.h> char?*asctime(const?struct?tm?*p_tm);?//習慣性字符串?Thu?Dec??9?07:13:35?2021</time.h>
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> 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:%ldnn",?times); printf("**************?使用ctime轉換time_t成我們習慣性使用的時間和日期字符串?**************n"); pstr?=?ctime(&times); printf("ctime:%sn",?pstr); printf("**************?使用gmtime轉換time_t成struct?tm?時間和日期**************n"); ptm?=?gmtime(&times); printf("time?:?%d:%d:%dn",?ptm->tm_hour,??ptm->tm_min,??ptm->tm_sec); printf("date:?%d:%d:%dn",?ptm->tm_year+1900,?ptm->tm_mon+1,?ptm->tm_mday); printf("year:?wday:%d?yday:%d?isdst:%dnn",?ptm->tm_wday,?ptm->tm_yday,?ptm->tm_isdst); printf("**************?使用asctime轉換struct?tm成我們習慣性使用的時間和日期字符串**************n"); pstr?=?asctime(ptm); printf("asctime:%snn",?pstr); printf("**************?使用localtime轉換time_t成struct?tm?時間和日期**************n"); ptm?=?localtime(&times); printf("time?:?%d:%d:%dn",?ptm->tm_hour,??ptm->tm_min,??ptm->tm_sec); printf("date:?%d:%d:%dn",?ptm->tm_year+1900,?ptm->tm_mon+1,?ptm->tm_mday); printf("year:?wday:%d?yday:%d?isdst:%dn",?ptm->tm_wday,?ptm->tm_yday,?ptm->tm_isdst); pstr?=?asctime(ptm); printf("asctime:%snn",?pstr); printf("**************?使用gettimeofday獲取微秒級的時間**************n"); struct?timeval?tv; struct?timezone?tz; gettimeofday(&tv,?&tz); ptm?=?localtime(&tv.tv_sec); printf("time?:?%d:%d:%dn",?ptm->tm_hour,??ptm->tm_min,??ptm->tm_sec); printf("date:?%d:%d:%dn",?ptm->tm_year+1900,?ptm->tm_mon+1,?ptm->tm_mday); printf("year:?wday:%d?yday:%d?isdst:%dn",?ptm->tm_wday,?ptm->tm_yday,?ptm->tm_isdst); printf("tv_usec:%ldnn",?tv.tv_usec); printf("**************?使用clock_gettime獲取納秒級的時間**************n"); struct?timespec?tp; clock_gettime(CLOCK_REALTIME,?&tp); ptm?=?localtime(&tp.tv_sec); printf("time?:?%d:%d:%dn",?ptm->tm_hour,??ptm->tm_min,??ptm->tm_sec); printf("date:?%d:%d:%dn",?ptm->tm_year+1900,?ptm->tm_mon+1,?ptm->tm_mday); printf("year:?wday:%d?yday:%d?isdst:%dn",?ptm->tm_wday,?ptm->tm_yday,?ptm->tm_isdst); printf("tp.tv_nsec:%ldnn",?tp.tv_nsec); return?0; }</sys></time.h></unistd.h></string.h></stdio.h>
特定的時間字符相互轉換
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",?&t); ????printf("****?tm_isdst:?%d,?tm_yday:%d,?tm_wday%d,n?%d-%d-%d?n?%d:%d:%dn",? ????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:%ldn",?mktime(&t)); printf("asctime:%sn",?asctime(&t)); strftime(pstr,?sizeof(pstr),?"%Y-%m-%d?%H:%M:%S",?&t); ????printf("pstr:%sn",?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",?&t); ????printf("pstr:%sn",?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?<p>相關推薦:《<a href="http://www.php.cn/course/list/33.html" target="_blank">Linux視頻教程</a>》<br></p>