Linux tm time_t timeval timespec以及与时间相关函数用法
生活随笔
收集整理的這篇文章主要介紹了
Linux tm time_t timeval timespec以及与时间相关函数用法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、時(shí)間類型
linux中編程通常需要用到時(shí)間變量,和相關(guān)的時(shí)間操作函數(shù)。常用的時(shí)間類型有:
time_t 、struct? timeval、struct ? timespec、struct ? tm。
在用到相關(guān)的類型和函數(shù)時(shí),需要加上頭文件:#include <time.h>
- time_t: 存儲(chǔ)從1970年到現(xiàn)在經(jīng)過了多少秒。格式為long int。UTC時(shí)間。
- struct? timeval:提供秒和微秒單位,最高精度是微秒。
- struct ? timespec:提供秒和納秒單位,最高精度是納秒
- struct ? tm:詳細(xì)時(shí)間的結(jié)構(gòu)體
二、時(shí)間函數(shù)
char *asctime(const struct tm* timeptr); //將結(jié)構(gòu)中的信息轉(zhuǎn)換為真實(shí)世界的UTC時(shí)間,以字符串的形式顯示char *ctime(const time_t *timep); //將timep轉(zhuǎn)換為真是世界的時(shí)間,以字符串顯示,它和asctime不同就在于傳入的參數(shù)形式不同double difftime(time_t time1, time_t time2); //返回兩個(gè)時(shí)間相差的秒數(shù)int gettimeofday(struct timeval *tv, struct timezone *tz); //返回當(dāng)前距離1970年的秒數(shù)和微秒數(shù),后面的tz是時(shí)區(qū),一般不用struct tm* gmtime(const time_t *timep); //將time_t表示的時(shí)間轉(zhuǎn)換為沒有經(jīng)過時(shí)區(qū)轉(zhuǎn)換的UTC時(shí)間,是一個(gè)struct tm結(jié)構(gòu)指針stuct tm* localtime(const time_t *timep); //和gmtime類似,但是它是經(jīng)過時(shí)區(qū)轉(zhuǎn)換的時(shí)間。time_t mktime(struct tm* timeptr); //將struct tm 結(jié)構(gòu)的時(shí)間轉(zhuǎn)換為從1970年至今的秒數(shù)time_t time(time_t *t); //取得從1970年1月1日至今的秒數(shù) //eg,totalsec = time(NULL) or time(&totalsec);int clock_gettime(clockid_t clk_id, struct timespec* tp); /*可以根據(jù)需要,獲取不同要求的精確時(shí)間 參數(shù) clk_id : 檢索和設(shè)置的clk_id指定的時(shí)鐘時(shí)間。 CLOCK_REALTIME:系統(tǒng)實(shí)時(shí)時(shí)間,隨系統(tǒng)實(shí)時(shí)時(shí)間改變而改變,即從UTC1970-1-1 0:0:0開始計(jì)時(shí), 中間時(shí)刻如果系統(tǒng)時(shí)間被用戶改成其他,則對(duì)應(yīng)的時(shí)間相應(yīng)改變 CLOCK_MONOTONIC:從系統(tǒng)啟動(dòng)這一刻起開始計(jì)時(shí),不受系統(tǒng)時(shí)間被用戶改變的影響 CLOCK_PROCESS_CPUTIME_ID:本進(jìn)程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間 CLOCK_THREAD_CPUTIME_ID:本線程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間*///eg. 獲得從開機(jī)到現(xiàn)在的時(shí)間:clock_gettime(CLOCK_MONOTONIC,&tspec);三、示例
1、設(shè)置系統(tǒng)時(shí)間:
struct timeval tv; tv.tv_sec = (time_t)gps_utctime_sec; tv.tv_usec = 0; if(settimeofday (&tv, NULL) < 0){ MSG("[ GPS ]:Set system datatime error,may need root permissions!\n"); } else{MSG("[ GPS ]:GPS valid,update system time!\n");2、讀取當(dāng)前時(shí)間tick
/*! get the current system time tick, second unit */ static uint32_t getCurrSysTick(void) {time_t timep;time (&timep);return timep;//seconds from 1970-1-1:0:0:0 }3、讀取當(dāng)前時(shí)間字符串:Fri Dec 20 14:44:18 2019
static char* getCurrSysTime(void) {time_t timep;time (&timep);return asctime(localtime(&timep)); }4、設(shè)置延時(shí)
static void delay_ms(unsigned int dms) {struct timespec sleeper, dummy ;sleeper.tv_sec = (time_t)(dms / 1000) ;sleeper.tv_nsec = (long)(dms % 1000) * 1000000 ;nanosleep (&sleeper, &dummy);//delay 1ms }5、獲取當(dāng)前時(shí)間tm
struct tm* t_tm; t_tm = localtime((time_t *)&meas_gps_utctime.tv_sec); /* test gps */ MSG("today is %4d-%02d-%02d %02d:%02d:%02d\n",t_tm->tm_year+1900,t_tm->tm_mon+1,t_tm->tm_mday,t_tm->tm_hour,t_tm->tm_min,t_tm->tm_sec);?
Linux時(shí)間轉(zhuǎn)換圖:
?
?
參考:
總結(jié)
以上是生活随笔為你收集整理的Linux tm time_t timeval timespec以及与时间相关函数用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php获取服务器名称,PHP 获取服务器
- 下一篇: linux 下建立多个tomcat