日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux时间函数札记

發(fā)布時(shí)間:2023/11/30 linux 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux时间函数札记 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(guān)于gmtimegmtime_rlocaltimelocaltime_r


測(cè)試環(huán)境:vmware?7?+?Redhat5.5,系統(tǒng)時(shí)間使用UTC,時(shí)區(qū)為上海。

?1、函數(shù)功能介紹

??????? 使用man?gmtimeman?localtime都可以的得到這幾個(gè)函數(shù)的介紹。原型如下:

??????? struct?tm?*gmtime(const?time_t?*timep);

??????? struct?tm?*gmtime_r(const?time_t?*timep,?struct?tm?*result);

??????? struct?tm?*localtime(const?time_t?*timep);

??????? struct?tm?*localtime_r(const?time_t?*timep,?struct?tm?*result);

man手冊(cè)中對(duì)它們的解釋如下:

??????? The?gmtime()?function?converts?the?calendar?time?timep?to?broken-down?time?representation,?expressed?in?Coordinated?Universal?Time?(UTC).?It?may?return?NULL?when?the?year?does?not?fit?into?an?integer.?The?return?value?points?to?a?statically?allocated?struct?which?might?be?overwritten??by?subsequent?calls?to?any?of?the?date?and?time?functions.?The?gmtime_r()?function?does?the?same,?but?stores?the?data?in?a?user-supplied?struct.

??????? The?localtime()?function?converts?the?calendar?time?timep?to?broken-time?representation,?expressed?relative?to?the?user's?specified?time?zone.?The?function?acts?as?if?it?called?tzset(3)?and?sets?the?external?variables?tzname?with?information?about?the?current?time?zone,?timezone?with??the?difference?between?Coordinated?Universal?Time?(UTC)?and?local?standard?time?in?seconds,?and?daylight?to?a?non-zero?value?if?daylight?savings?time?rules?apply?during?some?part?of?the?year.??The?return?value?points?to?a?statically?allocated?struct?which?might?be?overwritten?by?subsequent?calls?to?any?of?the?date?and?time?functions.?The?localtime_r()?function?does?the?same,?but?stores??the?data?in?a?user-supplied?struct.?It?need?not?set?tzname

翻譯如下:

??????? gmtime()?函數(shù)將日歷時(shí)間timep轉(zhuǎn)換為用UTC時(shí)間表示的時(shí)間。它可能返回NULL,比如年份不能放到一個(gè)整數(shù)中。返回值指向一個(gè)靜態(tài)分配的結(jié)構(gòu),該結(jié)構(gòu)可能會(huì)被接下來(lái)的任何日期和時(shí)間函數(shù)調(diào)用覆蓋。gmtime_r()函數(shù)功能與此相同,但是它可以將數(shù)據(jù)存儲(chǔ)到用戶(hù)提供的結(jié)構(gòu)體中。

??????? localtime()?函數(shù)將日歷時(shí)間timep轉(zhuǎn)換為用戶(hù)指定的時(shí)區(qū)的時(shí)間。這個(gè)函數(shù)的行為好像是它調(diào)用了tzset(3)?并且將外部變量tzname設(shè)置為當(dāng)前時(shí)區(qū)的信息,將timezone設(shè)為UTC和本地標(biāo)準(zhǔn)時(shí)間的差值,并且,如果在一年的部分時(shí)間使用日光節(jié)約規(guī)則時(shí)將daylight設(shè)置為非空值。返回值指向一個(gè)靜態(tài)分配的結(jié)構(gòu),該結(jié)構(gòu)可能會(huì)被接下來(lái)的任何日期和時(shí)間函數(shù)調(diào)用覆蓋。localtime_r()函數(shù)功能與此相同,但是它可以將數(shù)據(jù)存儲(chǔ)到用戶(hù)提供的結(jié)構(gòu)體中。它不需要設(shè)置tzname

?

2、功能測(cè)試

程序一:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?utc_tm;;

if(?NULL?==?gmtime_r(?&cur_time,?&utc_tm?)?)

{

perror("gmtime"?);

return?-1;

}

?

struct?tm?local_tm;

if(?NULL?==?localtime_r(?&cur_time,?&local_tm?)?)

{

perror("localtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(&utc_tm)?);

printf("LOC?=?%s",?asctime(&local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?09:16:10?2011

LOC?=?Thu?Oct?27?17:16:10?2011

LOC?=?Thu?Oct?27?17:16:10?2011

由于系統(tǒng)時(shí)間使用了UTC,可以看到本地時(shí)間= UTC時(shí)間?+?8”,輸出正確。

?

程序二:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?*utc_tm?=?gmtime(?&cur_time?);

if(?NULL?==?utc_tm?)

{

perror("gmtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(utc_tm)?);

?

struct?tm?*local_tm?=?localtime(?&cur_time?);

if(?NULL?==?local_tm?)

{

perror("localtime"?);

return?-1;

}

?

printf("LOC?=?%s",?asctime(local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?09:20:45?2011

LOC?=?Thu?Oct?27?17:20:45?2011

LOC?=?Thu?Oct?27?17:20:45?2011

同樣是正確的。

?

程序三:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?*utc_tm?=?gmtime(?&cur_time?);

if(?NULL?==?utc_tm?)

{

perror("gmtime"?);

return?-1;

}

?

struct?tm?*local_tm?=?localtime(?&cur_time?);

if(?NULL?==?local_tm?)

{

perror("localtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(utc_tm)?);

printf("LOC?=?%s",?asctime(local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?17:21:59?2011

LOC?=?Thu?Oct?27?17:21:59?2011

LOC?=?Thu?Oct?27?17:21:59?2011

這程序輸出有錯(cuò),UTC時(shí)間和本地時(shí)間相同了,這應(yīng)該就是由于man文檔中描述的可能會(huì)被接下來(lái)的任何日期和時(shí)間函數(shù)調(diào)用覆蓋造成的。為驗(yàn)證這個(gè)設(shè)想,使用程序四:

?

程序四:

#include?<stdio.h>

#include?<time.h>

int?main()

{

time_t?cur_time=time(NULL);

if(?cur_time?<?0?)

{

perror("time");

return?-1;

}

?

struct?tm?*local_tm?=?localtime(?&cur_time?);

if(?NULL?==?local_tm?)

{

perror("localtime"?);

return?-1;

}

?

struct?tm?*utc_tm?=?gmtime(?&cur_time?);

if(?NULL?==?utc_tm?)

{

perror("gmtime"?);

return?-1;

}

?

printf("UTC?=?%s",?asctime(utc_tm)?);

printf("LOC?=?%s",?asctime(local_tm)?);

printf("LOC?=?%s",?ctime(&cur_time)?);

return?0;

}

程序輸出:

UTC?=?Thu?Oct?27?09:24:23?2011

LOC?=?Thu?Oct?27?09:24:23?2011

LOC?=?Thu?Oct?27?17:24:23?2011

驗(yàn)證了該設(shè)想。

?

3、總結(jié)

??????? 使用gmtimelocaltime后要立即處理結(jié)果,否則返回的指針指向的內(nèi)容可能會(huì)被覆蓋,一個(gè)好的方法是使用gmtime_rlocaltime_r,由于使用了用戶(hù)分配的內(nèi)存,這兩個(gè)函數(shù)是不會(huì)出錯(cuò)的。





Linux時(shí)間函數(shù)

分類(lèi): Linux C編程2012-04-28 22:48 22366人閱讀 評(píng)論(6) 收藏 舉報(bào)

linuxstructnulltimezonetimer

系統(tǒng)環(huán)境:ubuntu10.04


簡(jiǎn)介

本文旨在為了解Linux各種時(shí)間類(lèi)型與時(shí)間函數(shù)提供技術(shù)文檔。


1Linux下常用時(shí)間類(lèi)型

Linux下常用時(shí)間類(lèi)型有四種:time_tstruct tmstruct timevalstruct timespec


1.1 time_t時(shí)間類(lèi)型

time_t類(lèi)型在time.h中定義:

  • #ifndef __TIME_T ?
  • #define __TIME_T ?
  • typedef? long? time_t; ?
  • #endif ?
  • 可見(jiàn),time_t實(shí)際是一個(gè)長(zhǎng)整型。其值表示為從UTC(coordinated universal time)時(shí)間19701100時(shí)0000(也稱(chēng)為Linux系統(tǒng)的Epoch時(shí)間)到當(dāng)前時(shí)刻的秒數(shù)。由于time_t類(lèi)型長(zhǎng)度的限制,它所表示的時(shí)間不能晚于203811903時(shí)1407(UTC)。為了能夠表示更久遠(yuǎn)的時(shí)間,可用64位或更長(zhǎng)的整形數(shù)來(lái)保存日歷時(shí)間,這里不作詳述。

    使用time()函數(shù)獲取當(dāng)前時(shí)間的time_t值,使用ctime()函數(shù)將time_t轉(zhuǎn)為當(dāng)?shù)貢r(shí)間字符串。

    備注UTC時(shí)間有時(shí)也稱(chēng)為GMT時(shí)間,其實(shí)UTCGMT兩者幾乎是同一概念。它們都是指格林尼治標(biāo)準(zhǔn)時(shí)間,只不過(guò)UTC的稱(chēng)呼更為正式一點(diǎn)。兩者區(qū)別在于前者是天文上的概念,而后者是基于一個(gè)原子鐘。


    1.2 struct tm時(shí)間類(lèi)型

    tm結(jié)構(gòu)在time.h中定義:

  • #ifndef _TM_DEFINED ?
  • struct tm{ ?
  • ? ? int tm_sec; /* - 取值區(qū)間為[0, 59]*/ ?
  • ? ? int tm_min; /* - 取值區(qū)間為[0, 59]*/ ?
  • ? ? int tm_hour; /*時(shí) - 取值區(qū)間為[0, 23]*/ ?
  • ? ? int tm_mday; /* - 取值區(qū)間為[1, 31]*/ ?
  • ? ? int tm_mon; /*月份 - 取值區(qū)間為[0, 11]*/ ?
  • ? ? int tm_year; /*年份 - 其值為1900年至今年數(shù)*/ ?
  • ? ? int tm_wday; /*星期 - 取值區(qū)間[0, 6]0代表星期天,1代表星期1,以此類(lèi)推*/ ?
  • ? ? int tm_yday; /*從每年的11日開(kāi)始的天數(shù)-取值區(qū)間為[0, 365]0代表11*/ ?
  • ? ? int tm_isdst; /*夏令時(shí)標(biāo)識(shí)符,使用夏令時(shí),tm_isdst為正,不使用夏令時(shí),tm_isdst0,不了解情況時(shí),tm_isdst為負(fù)*/ ?
  • }; ?
  • #define _TM_DEFINED ?
  • #endif ?
  • ANSI C標(biāo)準(zhǔn)稱(chēng)使用tm結(jié)構(gòu)的這種時(shí)間表示為分解時(shí)間(broken-down time)

    使用gmtime( )localtime( )可將time_t時(shí)間類(lèi)型轉(zhuǎn)換為tm結(jié)構(gòu)體;

    使用mktime( )tm結(jié)構(gòu)體轉(zhuǎn)換為time_t時(shí)間類(lèi)型;

    使用asctime( )struct tm轉(zhuǎn)換為字符串形式。

    ?

    1.3 struct timeval時(shí)間類(lèi)型

    timeval結(jié)構(gòu)體在time.h中定義:

  • Struct tmieval{ ?
  • ? ? time_t tv_sec; /*s*/ ?
  • ? ? suseconds_t tv_usec; /*微秒us*/ ?
  • }; ?
  • 設(shè)置時(shí)間函數(shù)settimeofday( )與獲取時(shí)間函數(shù)gettimeofday( )均使用該事件類(lèi)型作為傳參。

    ?

    1.4 struct timespec時(shí)間類(lèi)型

    timespec結(jié)構(gòu)體在time.h定義:


  • struct timespec{ ?
  • ? ? time_t tv_sec; /*s*/ ?
  • ? ? long tv_nsec; /*納秒ns*/ ?
  • }; ?
  • ?

    2Linux下常用時(shí)間函數(shù)

    Linux下常用時(shí)間函數(shù)有:time( )ctime( )gmtime( )localtime( )mktime( )asctime( )difftime( )gettimeofday( )settimeofday( )


    2.1 time( )函數(shù)

    頭文件:#include <time.h>

    函數(shù)定義:time_t time(time_t *timer)

    功能描述:該函數(shù)返回從19701100時(shí)0000秒至今所經(jīng)過(guò)的秒數(shù)。如果time_t *timer非空指針,函數(shù)也會(huì)將返回值存到timer指針指向的內(nèi)存。

    返回值:成功則返回秒數(shù),失敗則返回((time_t)-1)值,錯(cuò)誤原因存于errno中。

    例:


  • time_t seconds; ?
  • seconds = time((time_t *)NULL); ?

  • 2.2 ctime( )函數(shù)

    頭文件:#include <time.h>

    函數(shù)定義:char *ctime(const time_t *timep);

    功能描述:ctime( )將參數(shù)timep指向的time_t時(shí)間信息轉(zhuǎn)換成實(shí)際所使用的時(shí)間日期表示方法,并以字符串形式返回。字符串格式為:"Wed Jun 20 21:00:00 2012\n"

    例:

  • time_t timep; ?
  • tmep = time(NULL); ?
  • printf("%s\n", ctime(&timep)); ?

  • 2.3 gmtime( )函數(shù)

    頭文件:#include <time.h>

    函數(shù)定義:struct tm *gmtime(const time_t *timep)

    功能描述:gmtime( )將參數(shù)timep指向的time_t時(shí)間信息轉(zhuǎn)換成以tm結(jié)構(gòu)體表示的GMT時(shí)間信息,并以struct tm*指針?lè)祷亍?/p>

    GMTGMT是中央時(shí)區(qū),北京在東8區(qū),相差8個(gè)小時(shí),所以北京時(shí)間=GMT時(shí)間+8小時(shí)。

    例:

  • int main(void) ?
  • { ?
  • ? ? char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; ?
  • ? ? time_t timep; ?
  • ? ? struct tm *p_tm; ?
  • ? ? timep = time(NULL); ?
  • ? ? p_tm = gmtime(&timep); /*獲取GMT時(shí)間*/ ?
  • ? ? printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday); ?
  • ? ? printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec); ?
  • } ?

  • 2.4 localtime( )函數(shù)

    頭文件:#include <time.h>

    函數(shù)定義:struct tm *localtime(const time_t *timep);

    功能描述:localtime( )將參數(shù)timep指向的time_t時(shí)間信息轉(zhuǎn)換成以tm結(jié)構(gòu)體表示的本地時(shí)區(qū)時(shí)間(如北京時(shí)間= GMT+小時(shí))

    例:

  • int main(void) ?
  • { ?
  • ? ? char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; ?
  • ? ? time_t timep; ?
  • ? ? struct tm *p_tm; ?
  • ? ? timep = time(NULL); ?
  • ? ? p_tm = localtime(&timep); /*獲取本地時(shí)區(qū)時(shí)間*/ ?
  • ? ? printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday); ?
  • ? ? printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec); ?
  • ? ? return 0; ?
  • } ?

  • 2.5 mktime( )函數(shù)

    頭文件:#include <time.h>

    函數(shù)定義:time_t mktime(struct tm *p_tm);

    功能描述:mktime( )將參數(shù)p_tm指向的tm結(jié)構(gòu)體數(shù)據(jù)轉(zhuǎn)換成從19701100時(shí)0000秒至今的GMT時(shí)間經(jīng)過(guò)的秒數(shù)。

    例:

  • int main(void) ?
  • { ?
  • ? ? time_t timep: ?
  • ? ? struct tm *p_tm; ?
  • ? ? timep = time(NULL); ?
  • ? ? pintf("time( ):%d\n", timep); ?
  • ? ? p_tm = local(&timep); ?
  • ? ? timep = mktime(p_tm); ?
  • ? ? printf("time( )->localtime( )->mktime( ):%d\n", timep); ?
  • ? ? return 0; ?
  • } ?

  • 2.6 asctime( )函數(shù)

    頭文件:#include <time.h>

    函數(shù)定義:char *asctime(const struct tm *p_tm);

    功能描述:asctime( )將參數(shù)p_tm指向的tm結(jié)構(gòu)體數(shù)據(jù)轉(zhuǎn)換成實(shí)際使用的時(shí)間日期表示方法,并以字符串形式返回(ctime函數(shù)相同)。字符串格式為:"Wed Jun 20 21:00:00 2012\n"

    例:

  • int main(void) ?
  • { ?
  • ? ? time_t timep; ?
  • ? ? timep = time(NULL); ?
  • ? ? printf("%s\n", asctime(gmtime(&timep))); ?
  • ? ? return 0; ?
  • } ?

  • 2.7 difftime( )函數(shù)

    頭文件:#include <time.h>

    函數(shù)定義:double difftime(time_t timep1, time_t timep2);

    功能描述:difftime( )比較參數(shù)timep1timep2時(shí)間是否相同,并返回之間相差秒數(shù)。

    例:

  • int main(void) ?
  • { ?
  • ? ? time_t timep1, timep2; ?
  • ? ? timep1 = time(NULL); ?
  • ? ? sleep(2); ?
  • ? ? timep2 = time(NULL); ?
  • ? ? printf("the difference is %f seconds\n", difftime(timep1, timep2)); ?
  • ? ? return 0; ?
  • } ?

  • 2.8 gettimeofday( )函數(shù)

    頭文件:#include <sys/time.h>

    ? ? ? ? #include <unistd.h>

    函數(shù)定義:int gettimeofday(struct timeval *tv, struct timezone *tz);

    功能描述:gettimeofday( )把目前的時(shí)間信息存入tv指向的結(jié)構(gòu)體,當(dāng)?shù)貢r(shí)區(qū)信息則放到tz指向的結(jié)構(gòu)體。

    struct timezone原型:

  • struct timezone{ ?
  • ? ? int tz_minuteswest; /*miniutes west of Greenwich*/ ?
  • ? ? int tz_dsttime; /*type of DST correction*/ ?
  • }; ?
  • 例:

  • struct timeval tv; ?
  • struct timeval tz; ?
  • gettimeofday(&tv, &tz); ?

  • 附:

    使用time函數(shù)族獲取時(shí)間并輸出指定格式字符串例子(strftime( )函數(shù)):

  • int main(void) ?
  • { ?
  • ? ? char strtime[20] = {0}; ?
  • ? ? time_t timep; ?
  • ? ? struct tm *p_tm; ?
  • ? ? timep = time(NULL); ?
  • ? ? p_tm = localtime(&timep); ?
  • ? ? strftime(strtime, sizeof(strtime), "%Y-%m-%d %H:%M:%S", p_tm); ?
  • ? ? return 0; ?
  • } ?

  • 2.9 settimeofday( )函數(shù)

    頭文件:#include <sys/time.h>

    ? ? ? ? #include <unistd.h>

    函數(shù)定義:int settimeofday(const struct timeval *tv, const struct timezone *gz);

    功能描述:settimeofday( )把當(dāng)前時(shí)間設(shè)成由tv指向的結(jié)構(gòu)體數(shù)據(jù)。當(dāng)前地區(qū)信息則設(shè)成tz指向的結(jié)構(gòu)體數(shù)據(jù)。

    例:


  • int main(void) ?
  • { ?
  • ? ? char t_string[] = "2012-04-28 22:30:00"; ?
  • ? ? struct tm time_tm; ?
  • ? ? struct timeval time_tv; ?
  • ? ? time_t timep; ?
  • ? ? int ret = 0; ?
  • ?
  • ? ? sscanf(t_string, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec); ?
  • ? ? time_tm.tm_year -= 1900; ?
  • ? ? time_tm.tm_mon -= 1; ?
  • ? ? time_tm.tm_wday = 0; ?
  • ? ? time_tm.tm_yday = 0; ?
  • ? ? time_tm.tm_isdst = 0; ?
  • ?
  • ? ? timep = mktime(&time_tm); ?
  • ? ? time_tv.tv_sec = timep; ?
  • ? ? time_tv.tv_usec = 0; ?
  • ?
  • ? ? ret = settimeofday(&time_tv, NULL); ?
  • ? ? if(ret != 0) ?
  • ? ? { ?
  • ? ? ? ? fprintf(stderr, "settimeofday failed\n"); ?
  • ? ? ? ? return -1; ?
  • ? ? } ?
  • ? ? return 0; ?
  • } ?

  • 總結(jié)

    以上是生活随笔為你收集整理的Linux时间函数札记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。