生活随笔
收集整理的這篇文章主要介紹了
c语言中time相关函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
工作中遇到的函數(shù):
int seed = time(NULL);
srand(seed);
signal(SIGINT, stop);
signal(SIGUSR1, sig_usr1); 搜time函數(shù)時,看到相關(guān)time ? 函數(shù)的文章,粘貼如下:
-------------------------
from:http://blog.csdn.net/wangluojisuan/article/details/7045592
c語言中time函數(shù)的用法
標簽:?語言ctimerstruct日歷null 2011-12-06 12:48?61912人閱讀? ?分類: C語言(3)?
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
頭文件time.h? @函數(shù)名稱: ? ? localtime? 函數(shù)原型: ? ? struct tm *localtime(const time_t *timer)? 函數(shù)功能: ? ? 返回一個以tm結(jié)構(gòu)表達的機器時間信息? 函數(shù)返回: ? ? 以tm結(jié)構(gòu)表達的時間,結(jié)構(gòu)tm定義如下:?
[cpp]?view plaincopy
struct??tm{?????????int?tm_sec;?????????int?tm_min;?????????int?tm_hour;?????????int?tm_mday;?????????int?tm_mon;?????????int?tm_year;?????????int?tm_wday;?????????int?tm_yday;?????????int?tm_isdst;???????};??? 參數(shù)說明: ? ? timer-使用time()函數(shù)獲得的機器時間?
[cpp]?view plaincopy
#include?<time.h>???#include?<stdio.h>???#include?<dos.h>???int?main()?{???????time_t?timer;???????struct?tm?*tblock;???????timer=time(NULL);???????tblock=localtime(&timer);???????printf("Local?time?is:?%s",asctime(tblock));???????return?0;???}??? @函數(shù)名稱: ? ? asctime? 函數(shù)原型: ? ? char* asctime(struct tm * ptr)? 函數(shù)功能: ? ? 得到機器時間(日期時間轉(zhuǎn)換為ASCII碼)? 函數(shù)返回: ? ? 返回的時間字符串格式為:星期,月,日,小時:分:秒,年? 參數(shù)說明: ? ? 結(jié)構(gòu)指針ptr應(yīng)通過函數(shù)localtime()和gmtime()得到? 所屬文件: ? ? <time.h>?
[cpp]?view plaincopy
#include?<stdio.h>???#include?<string.h>???#include?<time.h>???int?main()?{???????struct?tm?t;???????char?str[80];???????t.tm_sec=1;???????t.tm_min=3;???????t.tm_hour=7;???????t.tm_mday=22;???????t.tm_mon=11;???????t.tm_year=56;???????t.tm_wday=4;???????t.tm_yday=0;???????t.tm_isdst=0;???????strcpy(str,asctime(&t));???????printf("%s",str);???????return?0;???}??? @函數(shù)名稱: ? ? ctime? 函數(shù)原型: ? ? char *ctime(long time)? 函數(shù)功能: ? ? 得到日歷時間? 函數(shù)返回: ? ? 返回字符串格式:星期,月,日,小時:分:秒,年? 參數(shù)說明: ? ? time-該參數(shù)應(yīng)由函數(shù)time獲得? 所屬文件: ? ? <time.h>?
[cpp]?view plaincopy
#include?<stdio.h>???#include?<time.h>???int?main()?{???????time_t?t;???????time(&t);???????printf("Today's?date?and?time:?%s",ctime(&t));???????return?0;???}??? @函數(shù)名稱: ? ? difftime? 函數(shù)原型: ? ? double difftime(time_t time2, time_t time1)? 函數(shù)功能: ? ? 得到兩次機器時間差,單位為秒? 函數(shù)返回: ? ? 時間差,單位為秒? 參數(shù)說明: ? ? time1-機器時間一,time2-機器時間二.該參數(shù)應(yīng)使用time函數(shù)獲得? 所屬文件: ? ? <time.h>?
[cpp]?view plaincopy
#include?<time.h>???#include?<stdio.h>???#include?<dos.h>???#include?<conio.h>???int?main()?{???????time_t?first,?second;???????clrscr();???????first=time(NULL);???????delay(2000);???????second=time(NULL);???????printf("The?difference?is:?%f?seconds",difftime(second,first));???????getch();???????return?0;???}??? @函數(shù)名稱: ? ? gmtime? 函數(shù)原型: ? ? struct tm *gmtime(time_t ?*time)? 函數(shù)功能: ? ? 得到以結(jié)構(gòu)tm表示的時間信息? 函數(shù)返回: ? ? 以結(jié)構(gòu)tm表示的時間信息指針? 參數(shù)說明: ? ? time-用函數(shù)time()得到的時間信息? 所屬文件: ? ? <time.h>?
[cpp]?view plaincopy
#include?<stdio.h>???#include?<stdlib.h>???#include?<time.h>???#include?<dos.h>???char?*tzstr="TZ=PST8PDT";???int?main()?{???????time_t?t;???????struct?tm?*gmt,?*area;???????putenv(tzstr);???????tzset();???????t=time(NULL);???????area=localtime(&t);???????printf("Local?time?is:%s",?asctime(area));???????gmt=gmtime(&t);???????printf("GMT?is:%s",?asctime(gmt));???????return?0;???}??? @函數(shù)名稱: ? ? time? 函數(shù)原型: ? ? time_t time(time_t *timer)? 函數(shù)功能: ? ? 得到機器的日歷時間或者設(shè)置日歷時間? 函數(shù)返回: ? ? 機器日歷時間? 參數(shù)說明: ? ? timer=NULL時得到機器日歷時間,timer=時間數(shù)值時,用于設(shè)置日歷時間,time_t是一個long類型? 所屬文件: ? ? <time.h>?
[cpp]?view plaincopy
#include?<time.h>???#include?<stdio.h>???#include?<dos.h>???int?main()?{???????time_t?t;???????t=time();???????printf("The?number?of?seconds?since?January?1,1970?is?%ld",t);???????return?0;???}??? @函數(shù)名稱: ? ? tzset? 函數(shù)原型: ? ? void tzset(void)? 函數(shù)功能: ? ? UNIX兼容函數(shù),用于得到時區(qū),在DOS環(huán)境下無用途? 函數(shù)返回:? 參數(shù)說明:? 所屬文件: ? ? <time.h>?
[cpp]?view plaincopy
#include?<time.h>???#include?<stdlib.h>???#include?<stdio.h>???int?main()?{???????time_t?td;???????putenv("TZ=PST8PDT");???????tzset();???????time(&td);???????printf("Current?time=%s",asctime(localtime(&td)));???????return?0;???}??
轉(zhuǎn)載于:https://www.cnblogs.com/the-tops/p/5900163.html
總結(jié)
以上是生活随笔為你收集整理的c语言中time相关函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。