time_t nowtime; -- 聲明變量 nowtime(現(xiàn)在時(shí)間) 為 time_t 型

10年積累的網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有湘陰免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
struct tm *timeinfo; -- 聲明變量timeinfo(時(shí)間信息)為 tm 型 結(jié)構(gòu) 指針。
time_t , tm 都是 time.h 頭文件里定義 的 類型。
time( nowtime ); -- 調(diào)系統(tǒng)函數(shù) time(), 獲得 現(xiàn)在時(shí)間 (1970年起多少個(gè)“滴答”,世界標(biāo)準(zhǔn)時(shí)間)
timeinfo = localtime( nowtime ); -- 調(diào)系統(tǒng)函數(shù), 獲得 當(dāng)?shù)?現(xiàn)在時(shí)間 (例如 東8 區(qū),北京時(shí)間)。時(shí)間數(shù)據(jù)是 tm 型 結(jié)構(gòu)。
int hour; -- 聲明變量 hour (小時(shí)),整型。
hour = timeinfo-tm_hour+1 ; -- 結(jié)構(gòu) timeinfo的成員tm_hour 是時(shí)間值,+1 得 hour(小時(shí))。
tm_hour -- 數(shù)值范圍 0-23。
#include time.h
#include stdio.h
#include dos.h
int main(void)
{
time_t timer;
struct tm *tblock;
timer = time(NULL);
tblock = localtime(timer);
printf("Local time is: %s", asctime(tblock));
return 0;
}
tm結(jié)構(gòu)定義如下:
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;
};
頭文件time.h
@函數(shù)名稱: localtime
函數(shù)原型: struct tm *localtime(const time_t *timer)
函數(shù)功能: 返回一個(gè)以tm結(jié)構(gòu)表達(dá)的機(jī)器時(shí)間信息
函數(shù)返回: 以tm結(jié)構(gòu)表達(dá)的時(shí)間,結(jié)構(gòu)tm定義如下:
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ù)獲得的機(jī)器時(shí)間
#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ù)功能: 得到機(jī)器時(shí)間(日期時(shí)間轉(zhuǎn)換為ASCII碼)
函數(shù)返回: 返回的時(shí)間字符串格式為:星期,月,日,小時(shí):分:秒,年
參數(shù)說明: 結(jié)構(gòu)指針ptr應(yīng)通過函數(shù)localtime()和gmtime()得到
所屬文件: time.h
#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ù)返回: 返回字符串格式:星期,月,日,小時(shí):分:秒,年
參數(shù)說明: time-該參數(shù)應(yīng)由函數(shù)time獲得
所屬文件: time.h
#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ù)功能: 得到兩次機(jī)器時(shí)間差,單位為秒
函數(shù)返回: 時(shí)間差,單位為秒
參數(shù)說明: time1-機(jī)器時(shí)間一,time2-機(jī)器時(shí)間二.該參數(shù)應(yīng)使用time函數(shù)獲得
所屬文件: time.h
#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í)間信息
函數(shù)返回: 以結(jié)構(gòu)tm表示的時(shí)間信息指針
參數(shù)說明: time-用函數(shù)time()得到的時(shí)間信息
所屬文件: time.h
#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ù)功能: 得到機(jī)器的日歷時(shí)間或者設(shè)置日歷時(shí)間
函數(shù)返回: 機(jī)器日歷時(shí)間
參數(shù)說明: timer=NULL時(shí)得到機(jī)器日歷時(shí)間,timer=時(shí)間數(shù)值時(shí),用于設(shè)置日歷時(shí)間,time_t是一個(gè)long類型
所屬文件: time.h
#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ù),用于得到時(shí)區(qū),在DOS環(huán)境下無用途
函數(shù)返回:
參數(shù)說明:
所屬文件: time.h
#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;
}
#include?stdio.h
#include?time.h?
int?main()
{?
time_t?rawtime;?
struct?tm?*?timeinfo;?
time?(?rawtime?);?
timeinfo?=?localtime?(?rawtime?);?
printf?(?"The?current?date/time?is:?%s",?asctime?(timeinfo)?);?
return?0;
}
說明:
time_t // 時(shí)間類型(time.h 定義)?
struct tm { // 時(shí)間結(jié)構(gòu),time.h 定義如下:?
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;?
}?
time ( rawtime ); // 獲取時(shí)間,以秒計(jì),從1970年1月一日起算,存于rawtime?
localtime ( rawtime ); //轉(zhuǎn)為當(dāng)?shù)貢r(shí)間,tm 時(shí)間結(jié)構(gòu)?
asctime() // 轉(zhuǎn)為標(biāo)準(zhǔn)ASCII時(shí)間格式:?
//就是直接打印tm,tm_year 從1900年計(jì)算,所以要加1900,月tm_mon,從0計(jì)算,所以要加1
clock()是C/C++中的計(jì)時(shí)函數(shù),而與其相關(guān)的數(shù)據(jù)類型是clock_t。
它的具體功能是返回處理器調(diào)用某個(gè)進(jìn)程或函數(shù)所花費(fèi)的時(shí)間。函數(shù)返回從“開啟這個(gè)程序進(jìn)程”到“程序中調(diào)用clock()函數(shù)”時(shí)之間的CPU時(shí)鐘計(jì)時(shí)單元(clock tick)數(shù),其中clock_t是用來保存時(shí)間的數(shù)據(jù)類型。
在time.h文件中,我們可以找到對(duì)它的定義:
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
clock_t其實(shí)就是long,即長整形。該函數(shù)返回值是硬件滴答數(shù),要換算成秒或者毫秒,需要除以CLK_TCK或者 CLK_TCK CLOCKS_PER_SEC。比如,在VC++6.0下,這兩個(gè)量的值都是1000,這表示硬件滴答1000下是1秒,因此要計(jì)算一個(gè)進(jìn)程的時(shí)間,用clock()除以1000即可。
clock的返回值一直是0的原因:
1、編譯器優(yōu)化,for循環(huán)實(shí)際根本沒執(zhí)行,直接跳過去了,所以時(shí)間為0。
2、clock計(jì)算的是程序占用cpu的時(shí)間,如果你的程序執(zhí)行的動(dòng)作很少,那么clock算出的時(shí)間也很少。
3、建議使用time gettimeofday函數(shù)來計(jì)時(shí)。
擴(kuò)展資料:
C語言中clock()函數(shù)的程序例1:(TC下運(yùn)行通過)
#include?stdio.h
#include?time.h
int?main(void)
{
clock_t?start,?end;
start?=?clock();
delay(2000);
end?=?clock();
printf("The?time?was:?%f\n",?(double)(end?-?start)?/?CLK_TCK);
return?0;
}
說明:CLK_TCK?定義在TC中的time.h中:#define?CLK_TCK18.2。
在VC6.0中也有關(guān)于CLK_TCK的宏定義,不過其值不再是18.2,而是1000。
實(shí)際上在VC6.0中CLK_TCK已完全等同CLOCKS_PER_SEC。
參考資料來源:百度百科-clock()
CLOCK()函數(shù):
clock()是C/C++中的計(jì)時(shí)函數(shù),而與其相關(guān)的數(shù)據(jù)類型是clock_t。在MSDN中,查得對(duì)clock函數(shù)定義如下:
clock_t
clock(void)
;
這個(gè)函數(shù)返回從“開啟這個(gè)程序進(jìn)程”到“程序中調(diào)用clock()函數(shù)”時(shí)之間的CPU時(shí)鐘計(jì)時(shí)單元(clock
tick)數(shù),在MSDN中稱之為掛鐘時(shí)間(wal-clock);若掛鐘時(shí)間不可取,則返回-1。其中clock_t是用來保存時(shí)間的數(shù)據(jù)類型,在time.h文件中,我們可以找到對(duì)它的定義:
#ifndef
_CLOCK_T_DEFINED
typedef
long
clock_t;
#define
_CLOCK_T_DEFINED
#endif
很明顯,clock_t是一個(gè)長整形數(shù)。在time.h文件中,還定義了一個(gè)常量CLOCKS_PER_SEC,它用來表示一秒鐘會(huì)有多少個(gè)時(shí)鐘計(jì)時(shí)單元,其定義如下:
#define
CLOCKS_PER_SEC
((clock_t)1000)
可以看到每過千分之一秒(1毫秒),調(diào)用clock()函數(shù)返回的值就加1。下面舉個(gè)例子,你可以使用公式clock()/CLOCKS_PER_SEC來計(jì)算一個(gè)進(jìn)程自身的運(yùn)行時(shí)間:
void
elapsed_time()
{
printf("Elapsed
time:%u
secs.\n",clock()/CLOCKS_PER_SEC);
}
當(dāng)然,你也可以用clock函數(shù)來計(jì)算你的機(jī)器運(yùn)行一個(gè)循環(huán)或者處理其它事件到底花了多少時(shí)間:
#include
stdio.h
#include
stdlib.h
#include
time.h
int
main(void)
{
long
i
=
10000000L;
clock_t
start,
finish;
double
duration;
/*
測(cè)量一個(gè)事件持續(xù)的時(shí)間*/
printf(
"Time
to
do
%ld
empty
loops
is
",
i)
;
start
=
clock();
while(
i--
);
finish
=
clock();
duration
=
(double)(finish
-
start)
/
CLOCKS_PER_SEC;
printf(
"%f
seconds\n",
duration
);
system("pause");
}
在筆者的機(jī)器上,運(yùn)行結(jié)果如下:
Time
to
do
10000000
empty
loops
is
0.03000
seconds
上面我們看到時(shí)鐘計(jì)時(shí)單元的長度為1毫秒,那么計(jì)時(shí)的精度也為1毫秒,那么我們可不可以通過改變CLOCKS_PER_SEC的定義,通過把它定義的大一些,從而使計(jì)時(shí)精度更高呢?通過嘗試,你會(huì)發(fā)現(xiàn)這樣是不行的。在標(biāo)準(zhǔn)C/C++中,最小的計(jì)時(shí)單位是一毫秒。
time_t
time(
time_t
*timer
);
返回值是1970年到現(xiàn)在的秒數(shù)
用long型接就可以了
參數(shù)也是同樣意義
如
long
time_s
=
0;
time_s
=
time(
NULL
);
//
time_s就是1970年到現(xiàn)在的秒數(shù)
或者
long
*
time_s
=
NULL;
time(time_s);
//
*time_s就是1970年到現(xiàn)在的秒數(shù)
要計(jì)算前后一段時(shí)間的話之前取一次time,之后取一次相減就知道用了多少秒了
分享文章:c語言時(shí)間函數(shù)的用法,c語言中的時(shí)間函數(shù)
URL分享:http://chinadenli.net/article25/dsgiici.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站收錄、小程序開發(fā)、外貿(mào)建站、網(wǎng)站維護(hù)、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)