国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Linux 下的時(shí)間編程總結(jié)

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é)出來。

1、時(shí)間的類型

1.格林威治標(biāo)準(zhǔn)時(shí)間
   coordinated universal time(UTC)是世界標(biāo)準(zhǔn)時(shí)間,即常說的格林威治標(biāo)準(zhǔn)時(shí)間(greenwich mean time,GMT)。
2.日歷時(shí)間
   日歷時(shí)間(calendar time)是用"一個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)(如1970年1月1日0點(diǎn))到此時(shí)經(jīng)過的秒數(shù)"來表示的時(shí)間。

2、常用時(shí)間函數(shù)的API

1、獲取日歷時(shí)間

   #include <time.h>
   time_t time(time_t *tloc)
   函數(shù)功能 : 獲取日歷時(shí)間,即從1970年1月1日0點(diǎn)到現(xiàn)在所經(jīng)歷的秒數(shù).
   參數(shù) : 通常設(shè)置為NULL

?。╰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);
}

2、將日歷時(shí)間轉(zhuǎn)換為格林威治標(biāo)準(zhǔn)時(shí)間和本地時(shí)間

      通常用戶得到日歷時(shí)間的秒數(shù)后可以將這些秒數(shù)轉(zhuǎn)化為更容易接受的時(shí)間表示方式,這些表示時(shí)間的方式有格林威治時(shí)間、本地時(shí)間等首先介紹一個(gè)表示時(shí)間常用的數(shù)據(jù)結(jié)構(gòu)。

 struct tm
 {
      int tm_sec;   //秒值

      int tm_min;   //分鐘值

      int tm_hour;  //小時(shí)值

      int tm_mday;  //本月第幾日

      int tm_mon;   //本年第幾月

      int tm_year;  //tm_year+1900=哪一年

      int tm_wday;  //本周第幾日

      int tm_yday;  //本年第幾日

      int tm_isdst; //日光節(jié)約時(shí)間

 }
----------------------------------------------------------------- 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;
}

3、時(shí)間顯示

      利用函數(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);

4、計(jì)算事件耗時(shí)

 int gettimeofday(struct timeval *tv, struct timezone *tz)

函數(shù)功能 : 獲取從今日凌晨(0:0:0)到現(xiàn)在的時(shí)間差,常用于計(jì)算事件耗時(shí)
參數(shù)1 : 存放從今日凌晨(0:0:0)到現(xiàn)在的時(shí)間差,時(shí)間差以秒或微秒為單位,以結(jié)構(gòu)體形式存放

參數(shù)2 : 常設(shè)置為null

函數(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;
}

5、設(shè)置系統(tǒng)時(shí)鐘

這里要介紹兩個(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更精確罷了。

6、延時(shí)函數(shù)

(1)unsigned int sleep(unsigned int seconds)
函數(shù)功能 : 使程序睡眠seconds秒
參數(shù) : 需要休眠的秒數(shù)
(2)void usleep(unsigned long usec)
函數(shù)功能 : 使程序睡眠usec微秒
參數(shù) : 需要休眠的秒數(shù)

7、定時(shí)器

unsigned alarm(unsigned seconds);

#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è)置的值。

3、時(shí)間函數(shù)的安全用法

      在寫代碼的時(shí)候,經(jīng)常會(huì)用到讀取系統(tǒng)時(shí)間的函數(shù)。localtime函數(shù)不是線程安全的。如果在多線程里調(diào)用localtime函數(shù),很可能會(huì)出現(xiàn)問題。
多線程應(yīng)用里面,應(yīng)該用localtime_r函數(shù)替代localtime函數(shù),因?yàn)閘ocaltime_r是線程安全的。同樣gmtime函數(shù)和gmtime_r函數(shù)

char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

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);

time_t mktime(struct tm *tm);

         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。


4、時(shí)間函數(shù)功能測試

例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é):

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

來自:http://blog.csdn.net/sin0803/article/details/37767865
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
linux時(shí)間函數(shù)
time()函數(shù)
c語言中時(shí)間的獲取
C、C++時(shí)間與日期函數(shù)
Linux C函數(shù)之時(shí)間函數(shù)
c++ 時(shí)間類型詳解 time
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服