Linux时间函数
系統環境:ubuntu10.04
簡介
本文旨在為了解Linux各種時間類型與時間函數提供技術文檔。
1、Linux下常用時間類型
Linux下常用時間類型有四種:time_t、struct?tm、struct?timeval、struct?timespec
1.1?time_t時間類型
time_t類型在time.h中定義:
[cpp]?view plaincopyprint? #ifndef?__TIME_T?? #define?__TIME_T?? typedef??long??time_t;?? #endif??
可見,time_t實際是一個長整型。其值表示為從UTC(coordinated?universal?time)時間1970年1月1日00時00分00秒(也稱為Linux系統的Epoch時間)到當前時刻的秒數。由于time_t類型長度的限制,它所表示的時間不能晚于2038年1月19日03時14分07秒(UTC)。為了能夠表示更久遠的時間,可用64位或更長的整形數來保存日歷時間,這里不作詳述。
使用time()函數獲取當前時間的time_t值,使用ctime()函數將time_t轉為當地時間字符串。
備注:UTC時間有時也稱為GMT時間,其實UTC和GMT兩者幾乎是同一概念。它們都是指格林尼治標準時間,只不過UTC的稱呼更為正式一點。兩者區別在于前者是天文上的概念,而后者是基于一個原子鐘。
1.2?struct?tm時間類型
tm結構在time.h中定義:
[cpp]?view plaincopyprint? #ifndef?_TM_DEFINED?? struct?tm{?? ????int?tm_sec;?/*秒?-?取值區間為[0,?59]*/?? ????int?tm_min;?/*分?-?取值區間為[0,?59]*/?? ????int?tm_hour;?/*時?-?取值區間為[0,?23]*/?? ????int?tm_mday;?/*日?-?取值區間為[1,?31]*/?? ????int?tm_mon;?/*月份?-?取值區間為[0,?11]*/?? ????int?tm_year;?/*年份?-?其值為1900年至今年數*/?? ????int?tm_wday;?/*星期?-?取值區間[0,?6],0代表星期天,1代表星期1,以此類推*/?? ????int?tm_yday;?/*從每年的1月1日開始的天數-取值區間為[0,?365],0代表1月1日*/?? ????int?tm_isdst;?/*夏令時標識符,使用夏令時,tm_isdst為正,不使用夏令時,tm_isdst為0,不了解情況時,tm_isdst為負*/?? };?? #define?_TM_DEFINED?? #endif??
ANSI?C標準稱使用tm結構的這種時間表示為分解時間(broken-down?time)。
使用gmtime(?)和localtime(?)可將time_t時間類型轉換為tm結構體;
使用mktime(?)將tm結構體轉換為time_t時間類型;
使用asctime(?)將struct?tm轉換為字符串形式。
?
1.3?struct?timeval時間類型
timeval結構體在time.h中定義:
[cpp]?view plaincopyprint? Struct?tmieval{?? ????time_t?tv_sec;?/*秒s*/?? ????suseconds_t?tv_usec;?/*微秒us*/?? };??
設置時間函數settimeofday(?)與獲取時間函數gettimeofday(?)均使用該事件類型作為傳參。
?
1.4?struct?timespec時間類型
timespec結構體在time.h定義:
[cpp]?view plaincopyprint? struct?timespec{?? ????time_t?tv_sec;?/*秒s*/?? ????long?tv_nsec;?/*納秒ns*/?? };??
?
2、Linux下常用時間函數
Linux下常用時間函數有:time(?)、ctime(?)、gmtime(?)、localtime(?)、mktime(?)、asctime(?)、difftime(?)、gettimeofday(?)、settimeofday(?)
2.1?time(?)函數
頭文件:#include?<time.h>
函數定義:time_t?time(time_t?*timer)
功能描述:該函數返回從1970年1月1日00時00分00秒至今所經過的秒數。如果time_t?*timer非空指針,函數也會將返回值存到timer指針指向的內存。
返回值:成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存于errno中。
例:
[cpp]?view plaincopyprint? time_t?seconds;?? seconds?=?time((time_t?*)NULL);??
2.2?ctime(?)函數
頭文件:#include?<time.h>
函數定義:char?*ctime(const?time_t?*timep);
功能描述:ctime(?)將參數timep指向的time_t時間信息轉換成實際所使用的時間日期表示方法,并以字符串形式返回。字符串格式為:"Wed?Jun?20?21:00:00?2012\n"。
例:
[cpp]?view plaincopyprint? time_t?timep;?? tmep?=?time(NULL);?? printf("%s\n",?ctime(&timep));??
2.3?gmtime(?)函數
頭文件:#include?<time.h>
函數定義:struct?tm?*gmtime(const?time_t?*timep)
功能描述:gmtime(?)將參數timep指向的time_t時間信息轉換成以tm結構體表示的GMT時間信息,并以struct?tm*指針返回。
GMT:GMT是中央時區, 北京 在東8區,相差8個小時,所以北京時間=GMT時間+8小時。
例:
[cpp]?view plaincopyprint? 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時間*/?? ????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(?)函數
頭文件:#include?<time.h>
函數定義:struct?tm?*localtime(const?time_t?*timep);
功能描述:localtime(?)將參數timep指向的time_t時間信息轉換成以tm結構體表示的本地時區時間(如北京時間=?GMT+小時)。
例:
[cpp]?view plaincopyprint? 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);?/*獲取本地時區時間*/?? ????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(?)函數
頭文件:#include?<time.h>
函數定義:time_t?mktime(struct?tm?*p_tm);
功能描述:mktime(?)將參數p_tm指向的tm結構體數據轉換成從1970年1月1日00時00分00秒至今的GMT時間經過的秒數。
例:
[cpp]?view plaincopyprint? 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(?)函數
頭文件:#include?<time.h>
函數定義:char?*asctime(const?struct?tm?*p_tm);
功能描述:asctime(?)將參數p_tm指向的tm結構體數據轉換成實際使用的時間日期表示方法,并以字符串形式返回(與ctime函數相同)。字符串格式為:"Wed?Jun?20?21:00:00?2012\n"。
例:
[cpp]?view plaincopyprint? int?main(void)?? {?? ????time_t?timep;?? ????timep?=?time(NULL);?? ????printf("%s\n",?asctime(gmtime(&timep)));?? ????return?0;?? }??
2.7?difftime(?)函數
頭文件:#include?<time.h>
函數定義:double?difftime(time_t?timep1,?time_t?timep2);
功能描述:difftime(?)比較參數timep1和timep2時間是否相同,并返回之間相差秒數。
例:
[cpp]?view plaincopyprint? 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(?)函數
頭文件:#include?<sys/time.h>
????????#include?<unistd.h>
函數定義:int?gettimeofday(struct?timeval?*tv,?struct?timezone?*tz);
功能描述:gettimeofday(?)把目前的時間信息存入tv指向的結構體,當地時區信息則放到tz指向的結構體。
struct?timezone原型:
[cpp]?view plaincopyprint? struct?timezone{?? ????int?tz_minuteswest;?/*miniutes?west?of?Greenwich*/?? ????int?tz_dsttime;?/*type?of?DST?correction*/?? };??
例:
[cpp]?view plaincopyprint? struct?timeval?tv;?? struct?timeval?tz;?? gettimeofday(&tv,?&tz);??
附:
使用time函數族獲取時間并輸出指定格式字符串例子(strftime(?)函數):
[cpp]?view plaincopyprint? 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(?)函數
頭文件:#include?<sys/time.h>
????????#include?<unistd.h>
函數定義:int?settimeofday(const?struct?timeval?*tv,?const?struct?timezone?*gz);
功能描述:settimeofday(?)把當前時間設成由tv指向的結構體數據。當前地區信息則設成tz指向的結構體數據。
例:
[cpp]?view plaincopyprint? 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;?? }??
參考資料:
http://blog.csdn.net/c395565746c/article/details/6621153
http://fanqiang.chinaunix.net/a4/b8/20010527/201001267.html
http://qgjie456.blog.163.com/blog/static/35451367200844102949365/
http://hi.baidu.com/wangzhongli/blog/item/e260b3a1f388278746106461.html
http://www.eefocus.com/xuefu2009/blog/10-03/187348_f456a.html
簡介
本文旨在為了解Linux各種時間類型與時間函數提供技術文檔。
1、Linux下常用時間類型
Linux下常用時間類型有四種:time_t、struct?tm、struct?timeval、struct?timespec
1.1?time_t時間類型
time_t類型在time.h中定義:
[cpp]?view plaincopyprint?
使用time()函數獲取當前時間的time_t值,使用ctime()函數將time_t轉為當地時間字符串。
備注:UTC時間有時也稱為GMT時間,其實UTC和GMT兩者幾乎是同一概念。它們都是指格林尼治標準時間,只不過UTC的稱呼更為正式一點。兩者區別在于前者是天文上的概念,而后者是基于一個原子鐘。
1.2?struct?tm時間類型
tm結構在time.h中定義:
[cpp]?view plaincopyprint?
使用gmtime(?)和localtime(?)可將time_t時間類型轉換為tm結構體;
使用mktime(?)將tm結構體轉換為time_t時間類型;
使用asctime(?)將struct?tm轉換為字符串形式。
?
1.3?struct?timeval時間類型
timeval結構體在time.h中定義:
[cpp]?view plaincopyprint?
?
1.4?struct?timespec時間類型
timespec結構體在time.h定義:
[cpp]?view plaincopyprint?
2、Linux下常用時間函數
Linux下常用時間函數有:time(?)、ctime(?)、gmtime(?)、localtime(?)、mktime(?)、asctime(?)、difftime(?)、gettimeofday(?)、settimeofday(?)
2.1?time(?)函數
頭文件:#include?<time.h>
函數定義:time_t?time(time_t?*timer)
功能描述:該函數返回從1970年1月1日00時00分00秒至今所經過的秒數。如果time_t?*timer非空指針,函數也會將返回值存到timer指針指向的內存。
返回值:成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存于errno中。
例:
[cpp]?view plaincopyprint?
2.2?ctime(?)函數
頭文件:#include?<time.h>
函數定義:char?*ctime(const?time_t?*timep);
功能描述:ctime(?)將參數timep指向的time_t時間信息轉換成實際所使用的時間日期表示方法,并以字符串形式返回。字符串格式為:"Wed?Jun?20?21:00:00?2012\n"。
例:
[cpp]?view plaincopyprint?
2.3?gmtime(?)函數
頭文件:#include?<time.h>
函數定義:struct?tm?*gmtime(const?time_t?*timep)
功能描述:gmtime(?)將參數timep指向的time_t時間信息轉換成以tm結構體表示的GMT時間信息,并以struct?tm*指針返回。
GMT:GMT是中央時區, 北京 在東8區,相差8個小時,所以北京時間=GMT時間+8小時。
例:
[cpp]?view plaincopyprint?
2.4?localtime(?)函數
頭文件:#include?<time.h>
函數定義:struct?tm?*localtime(const?time_t?*timep);
功能描述:localtime(?)將參數timep指向的time_t時間信息轉換成以tm結構體表示的本地時區時間(如北京時間=?GMT+小時)。
例:
[cpp]?view plaincopyprint?
2.5?mktime(?)函數
頭文件:#include?<time.h>
函數定義:time_t?mktime(struct?tm?*p_tm);
功能描述:mktime(?)將參數p_tm指向的tm結構體數據轉換成從1970年1月1日00時00分00秒至今的GMT時間經過的秒數。
例:
[cpp]?view plaincopyprint?
2.6?asctime(?)函數
頭文件:#include?<time.h>
函數定義:char?*asctime(const?struct?tm?*p_tm);
功能描述:asctime(?)將參數p_tm指向的tm結構體數據轉換成實際使用的時間日期表示方法,并以字符串形式返回(與ctime函數相同)。字符串格式為:"Wed?Jun?20?21:00:00?2012\n"。
例:
[cpp]?view plaincopyprint?
2.7?difftime(?)函數
頭文件:#include?<time.h>
函數定義:double?difftime(time_t?timep1,?time_t?timep2);
功能描述:difftime(?)比較參數timep1和timep2時間是否相同,并返回之間相差秒數。
例:
[cpp]?view plaincopyprint?
2.8?gettimeofday(?)函數
頭文件:#include?<sys/time.h>
????????#include?<unistd.h>
函數定義:int?gettimeofday(struct?timeval?*tv,?struct?timezone?*tz);
功能描述:gettimeofday(?)把目前的時間信息存入tv指向的結構體,當地時區信息則放到tz指向的結構體。
struct?timezone原型:
[cpp]?view plaincopyprint?
[cpp]?view plaincopyprint?
附:
使用time函數族獲取時間并輸出指定格式字符串例子(strftime(?)函數):
[cpp]?view plaincopyprint?
2.9?settimeofday(?)函數
頭文件:#include?<sys/time.h>
????????#include?<unistd.h>
函數定義:int?settimeofday(const?struct?timeval?*tv,?const?struct?timezone?*gz);
功能描述:settimeofday(?)把當前時間設成由tv指向的結構體數據。當前地區信息則設成tz指向的結構體數據。
例:
[cpp]?view plaincopyprint?
參考資料:
http://blog.csdn.net/c395565746c/article/details/6621153
http://fanqiang.chinaunix.net/a4/b8/20010527/201001267.html
http://qgjie456.blog.163.com/blog/static/35451367200844102949365/
http://hi.baidu.com/wangzhongli/blog/item/e260b3a1f388278746106461.html
http://www.eefocus.com/xuefu2009/blog/10-03/187348_f456a.html
總結
- 上一篇: 破解32位NT6内核系统(vista/w
- 下一篇: linux 其他常用命令