http://www.open-open.com/lib/view/open1405411154399.html
2014
在嵌入式編程中中,經(jīng)常需要輸出系統(tǒng)的當(dāng)前時(shí)間、計(jì)算程序的執(zhí)行時(shí)間、使用計(jì)時(shí)器等。最近也做了不少關(guān)于時(shí)間的操作,今天就認(rèn)真總結(jié)一下,部分內(nèi)容是在網(wǎng)上看到的。自己經(jīng)過驗(yàn)證總結(jié)出來。
?。╰ime_t在time.h中定義:typedef long int time_t)time_t記錄自1970年1月1日凌晨以來的秒數(shù),在Linux/Unix上定義為long int類型,在32位系統(tǒng)上,time_t最多只能記錄2,147,483,647秒,也就是說到了2038年將會(huì)產(chǎn)生溢出(這就是為什么很多嵌入式設(shè)備日期只能夠設(shè)置到2038年的原因),但在64位系統(tǒng)上不會(huì)出現(xiàn)此問題。函數(shù)double difftime(time_t time1, time_t time0);用于比較兩個(gè)time_t類型的值。
#include <time.h>
void main(){ long lSeconds = 0;
lSeconds = time(NULL);
printf("seconds = %ld\n", lSeconds);
}
----------------------------------------------------------------- tm_sec | 秒,范圍是0~59。 tm_min | 分,范圍是0~59。 tm_hour | 時(shí),范圍是0~23。 tm_mday | 日,范圍是1~31。 tm_mon | 月,范圍是0~11,注意是0到11。 tm_year | 年,自1900以來的年數(shù)。 tm_wday | 星期幾,從星期天開始計(jì)算,范圍是0~6。 tm_yday | 一年中的哪一天,0~365。
tm_isdst | 夏令時(shí)間(DST)的一個(gè)標(biāo)志。 -----------------------------------------------------------------
函數(shù)介紹:
struct tm *gmtime(const time_t *timep)
函數(shù)功能 : 將日歷時(shí)間轉(zhuǎn)化為格林威治標(biāo)準(zhǔn)時(shí)間,并保存在tm結(jié)構(gòu)
參數(shù):日歷時(shí)間的返回值
struct tm* localtime(const time_t *timep)
函數(shù)功能:將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間,并保存至tm結(jié)構(gòu)
參數(shù):日歷時(shí)間的返回值。
例:
#include <stdio.h>#include <time.h>
int main(void){ struct tm *local;
time_t t;
t = time(null); //獲取日歷時(shí)間
local = localtime(&t);//將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間,并保存在struct tm結(jié)構(gòu)中
printf("local hour is :%d\n",local->tm_hour);
local = gmtime(&t); //將日歷時(shí)間轉(zhuǎn)化為格林威治時(shí)間,并保存在struct tm結(jié)構(gòu)中
printf("utc hour is :%d\n",local->tm_hour);
return 0;
}
利用函數(shù)gmtime()、localtime()可以將日歷時(shí)間轉(zhuǎn)化為格林威治時(shí)間和本地時(shí)間,雖然用戶可通過結(jié)構(gòu)體tm來獲取這些時(shí)間值,但看起來還不方便,最好是將所有的信息,如年、月、日、星期、時(shí)、分、秒以字符串的形式顯示出來。
char *asctime(const struct tm *tm)
函數(shù)功能:將tm格式的時(shí)間轉(zhuǎn)化為字符串
參數(shù):日歷時(shí)間的返回值
例如: SAT Jul 30 08:43:03 2005
該函數(shù)必須按照下面3個(gè)步驟來進(jìn)行.
<1>使用函數(shù)time()來獲取日歷時(shí)間
<2>使用函數(shù)gmtime()將日歷時(shí)間轉(zhuǎn)化為格林威治標(biāo)準(zhǔn)時(shí)間
<3>使用函數(shù)asctime()將tm格式的時(shí)間轉(zhuǎn)化為字符串
例:
#include <time.h>#include <stdio.h>
int main(void)
{
struct tm *ptr;
time_t lt;
lt = time(null); //獲取日歷時(shí)間
ptr = gmtime(<); //轉(zhuǎn)化為格林威治時(shí)間*/
printf(asctime(ptr)); //以格林威治時(shí)間的字符串方式打印
printf(ctime(<)); //以本地時(shí)間的字符串方式打印*/
return 0;
}
char *ctime(const time_t *timep)
函數(shù)功能:將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間的字符串形式
參數(shù):日歷時(shí)間的返回值。
該函數(shù).必須按照下面2個(gè)步驟來進(jìn)行.
<1>使用函數(shù)time()來獲取日歷時(shí)間
<2>使用函數(shù)ctime()將日歷時(shí)間直接轉(zhuǎn)化為字符串
PS:有time函數(shù)將time_t值轉(zhuǎn)換為struct tm類型值,函數(shù)mktime 可以將struct tm類型值轉(zhuǎn)換為time_t類型的值,其原型為:
time_t mktime(struct tm *tm);
函數(shù)用法:可以在做某件事情之前調(diào)用gettimeofday(),在做完該件事情之后調(diào)用gettimeofday(),兩個(gè)函數(shù)的參數(shù)1的差就是做該事情所消耗的時(shí)間。也經(jīng)常在程序中作為定時(shí)器處理,比如每隔多長時(shí)間時(shí)間執(zhí)行一次什么事件。
struct timeval
{
int tv_sec; //秒數(shù)
int tv_usec; //微秒數(shù)
}
// 計(jì)算時(shí)間差,單位毫秒int elapsed_time(struct timeval *ct, struct timeval *lt)
{
int elapsed = (ct->tv_sec - lt->tv_sec) * 1000 + (ct->tv_usec - lt->tv_usec) / 1000;
return elapsed;
}
這里要介紹兩個(gè)函數(shù),同time和gettimeofday對應(yīng),stime和settimeofday,原型如下:
int stime(time_t *t);int settimeofday(const struct timeval *tv, const struct timezone *tz);
只是settimeofday設(shè)置的時(shí)間比stime更精確罷了。(1)unsigned int sleep(unsigned int seconds)
函數(shù)功能 : 使程序睡眠seconds秒
參數(shù) : 需要休眠的秒數(shù)
(2)void usleep(unsigned long usec)
函數(shù)功能 : 使程序睡眠usec微秒
參數(shù) : 需要休眠的秒數(shù)
#include <stdio.h>#include <unistd.h>
#include <signal.h>
void alarm_handler(int signum){
rintf("Five seconds passed!\n");
}
void func(void){
signal(SIGALRM, alarm_handler);
alarm(5);
pause();
}
int main(void){
func();
return 0;
}
程序?qū)⒃?秒之后執(zhí)行alarm_handler函數(shù),這里還使用了pause函數(shù),用于掛起進(jìn)程直到捕捉到一個(gè)信號(hào)時(shí)才退出。注意alarm一次只能發(fā)送發(fā)送一個(gè)信號(hào),如果要再次發(fā)送信號(hào),需要重新調(diào)用alarm函數(shù)。
除了alarm外,還可以使用setitimer來設(shè)置定時(shí)器,使用getitimer來獲取定時(shí)器的狀態(tài),原型如下:
int setitimer(int which, const struct itimerval *restrict value, struct itimerval *restrict ovalue);
int getitimer(int which, struct itimerval *value);
說明:
需要包含頭文件sys/time.h
which參數(shù)有三種取值:
ITIMER_REAL 按實(shí)際時(shí)間記時(shí),時(shí)間到了之后發(fā)送SIGALRM信號(hào),相當(dāng)于alarm。
ITIMER_VIRTUAL 僅當(dāng)進(jìn)程執(zhí)行時(shí)才進(jìn)行記時(shí),發(fā)送SIGVTALRM信號(hào)。
ITIMER_PROF 當(dāng)進(jìn)程執(zhí)行時(shí)和系統(tǒng)執(zhí)行該進(jìn)程時(shí)都記時(shí),發(fā)送的是SIGPROF信號(hào)。
struct itimerval用來指定定時(shí)時(shí)間,定義如下:
struct itimerval
{
struct timerval it_interval; /* next value */
struct timerval it_value; /* current value */
};
在setitimer函數(shù)中,ovalue如果不為空,則保留上次調(diào)用設(shè)置的值。
gmtime() 函數(shù)將日歷時(shí)間timep轉(zhuǎn)換為用UTC時(shí)間表示的時(shí)間。它可能返回NULL,比如年份不能放到一個(gè)整數(shù)中。返回值指向一個(gè)靜態(tài)分配的結(jié)構(gòu),該結(jié)構(gòu)可能會(huì)被接下來的任何日期和時(shí)間函數(shù)調(diào)用覆蓋。gmtime_r()函數(shù)功能與此相同,但是它可以將數(shù)據(jù)存儲(chǔ)到用戶提供的結(jié)構(gòu)體中。
localtime() 函數(shù)將日歷時(shí)間timep轉(zhuǎn)換為用戶指定的時(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ì)被接下來的任何日期和時(shí)間函數(shù)調(diào)用覆蓋。localtime_r()函數(shù)功能與此相同,但是它可以將數(shù)據(jù)存儲(chǔ)到用戶提供的結(jié)構(gòu)體中。它不需要設(shè)置tzname。
例1:
#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;
}
輸出結(jié)果:
xzg@byxc-PDSML:~/test$ gcc -o time time.c
xzg@byxc-PDSML:~/test$ ./time
UTC = Tue Jul 15 01:41:08 2014
LOC = Tue Jul 15 09:41:08 2014
LOC = Tue Jul 15 09:41:08 2014
系統(tǒng)時(shí)間使用了UTC,可以看到“本地時(shí)間= UTC時(shí)間 + 8”,輸出正確。
例2:
#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;
} <strong>printf("UTC = %s", asctime(utc_tm) );</strong> 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;
}
輸出結(jié)果:
xzg@byxc-PDSML:~/test$ gcc -o time1 time1.c
xzg@byxc-PDSML:~/test$ ./time1
UTC = Tue Jul 15 02:00:38 2014
LOC = Tue Jul 15 10:00:38 2014
LOC = Tue Jul 15 10:00:38 2014
xzg@byxc-PDSML:~/test$
例3:
#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;
}
<strong>printf("UTC = %s", asctime(utc_tm) );</strong>
printf("LOC = %s", asctime(local_tm) ); printf("LOC = %s", ctime(&cur_time) );
return 0;}輸出結(jié)果:
xzg@byxc-PDSML:~/test$ gcc -o time1 time1.c
xzg@byxc-PDSML:~/test$ ./time1
UTC = Tue Jul 15 10:03:26 2014
LOC = Tue Jul 15 10:03:26 2014
LOC = Tue Jul 15 10:03:26 2014
這程序輸出有錯(cuò),UTC時(shí)間和本地時(shí)間相同了“可能會(huì)被接下來的任何日期和時(shí)間函數(shù)調(diào)用覆蓋”造成的。
總結(jié):
使用gmtime和localtime后要立即處理結(jié)果,否則返回的指針指向的內(nèi)容可能會(huì)被覆蓋,一個(gè)好的方法是使用gmtime_r和localtime_r,由于使用了用戶分配的內(nèi)存,這兩個(gè)函數(shù)是不會(huì)出錯(cuò)的。
來自:http://blog.csdn.net/sin0803/article/details/37767865聯(lián)系客服