time_t t; /*定義一個(gè)time_t型(在time.h中有typedef long time_t;語句,由此可知,time_t類型也就是long類型)的變量*/

創(chuàng)新互聯(lián)建站專注于淮安網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供淮安營(yíng)銷型網(wǎng)站建設(shè),淮安網(wǎng)站制作、淮安網(wǎng)頁設(shè)計(jì)、淮安網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造淮安網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供淮安網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
time(t); /*將當(dāng)前的日歷時(shí)間(即從1970-1-1到執(zhí)行此語句時(shí)所經(jīng)歷的秒數(shù))保存到t中*/
printf("%s/n", ctime(t)); /*ctime(t)將把t所指向的日歷時(shí)間轉(zhuǎn)換為系統(tǒng)所提供的一個(gè)字符串,這個(gè)函數(shù)將返回這個(gè)字符串的基址,然后由printf語句將這個(gè)字符串輸出,從而得到現(xiàn)在的時(shí)刻*/
來源
1、C語言中讀取系統(tǒng)時(shí)間的函數(shù)為time(),其函數(shù)原型為:\x0d\x0a#include \x0d\x0atime_t time( time_t * ) ;\x0d\x0atime_t就是long,函數(shù)返回從1970年1月1日(MFC是1899年12月31日)0時(shí)0分0秒,到現(xiàn)在的的秒數(shù)。\x0d\x0a2、C語言還提供了將秒數(shù)轉(zhuǎn)換成相應(yīng)的時(shí)間格式的函數(shù):\x0d\x0a char * ctime(const time_t *timer); //將日歷時(shí)間轉(zhuǎn)換成本地時(shí)間,返回轉(zhuǎn)換后的字符串指針 可定義字符串或是字符指針來接收返回值\x0d\x0a struct tm * gmtime(const time_t *timer); //將日歷時(shí)間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時(shí)間(即格林尼治時(shí)間),返回結(jié)構(gòu)體指針 可定義struct tm *變量來接收結(jié)果\x0d\x0a struct tm * localtime(const time_t * timer); //將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間,返回結(jié)構(gòu)體指針 可定義struct tm *變量來接收結(jié)果\x0d\x0a3、例程:\x0d\x0a#include \x0d\x0avoid main()\x0d\x0a{\x0d\x0a time_t t;\x0d\x0a struct tm *pt ;\x0d\x0a char *pc ;\x0d\x0a time(t);\x0d\x0a pc=ctime(t) ; printf("ctime:%s", pc );\x0d\x0a pt=localtime(t) ; printf("year=%d", pt-tm_year+1900 );\x0d\x0a}\x0d\x0a\x0d\x0a時(shí)間結(jié)構(gòu)體struct tm 說明:\x0d\x0a\x0d\x0astruct tm { \x0d\x0a int tm_sec; /* 秒 _ 取值區(qū)間為[0,59] */ \x0d\x0a int tm_min; /* 分 - 取值區(qū)間為[0,59] */ \x0d\x0a int tm_hour; /* 時(shí) - 取值區(qū)間為[0,23] */ \x0d\x0a int tm_mday; /* 一個(gè)月中的日期 - 取值區(qū)間為[1,31] */ \x0d\x0a int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */ \x0d\x0a int tm_year; /* 年份,其值等于實(shí)際年份減去1900 */ \x0d\x0a int tm_wday; /* 星期 _ 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類推 */ \x0d\x0a int tm_yday; /* 從每年的1月1日開始的天數(shù) _ 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */ \x0d\x0a int tm_isdst; /* 夏令時(shí)標(biāo)識(shí)符,實(shí)行夏令時(shí)的時(shí)候,tm_isdst為正。不實(shí)行夏令時(shí)的進(jìn)候,tm_isdst為0;不了解情況時(shí),tm_isdst()為負(fù)。*/ \x0d\x0a};
C語言的建時(shí)間函數(shù)是 mktime(),原型在 time.h 里
調(diào)用有點(diǎn)繁。
下面,用我的程序輸入 年月日時(shí)分秒,調(diào)用mktime(), 就得 C語言 可直接使用的 時(shí)間, 存放在 t 里。
例如 輸入年月日時(shí)分秒: 2008 8 16 9 55 25
time_t t; 里 就有了 各種時(shí)間信息,例如星期幾...
#include stdio.h
#include time.h
void main(){
struct tm *target_time;
time_t rawtime, t;
int year,month,mday,hh,mm,ss;
time ( rawtime );
target_time = localtime ( rawtime );
printf("Please enter year month day hour minute second\n");
printf("For example: \n");
printf("2008 8 16 9 55 25\n");
scanf("%d %d %d %d %d %d", year, month, mday, hh,mm,ss);
target_time-tm_year = year - 1900;
target_time-tm_mon= month - 1;
target_time-tm_mday = mday ;
target_time-tm_hour = hh ;
target_time-tm_min = mm ;
target_time-tm_sec = ss ;
//
t = mktime (target_time);
// t is ready to use
printf("%s ",ctime(t));
}
需要利用C語言的時(shí)間函數(shù)time和localtime,具體說明如下:
一、函數(shù)接口介紹:
1、time函數(shù)。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結(jié)構(gòu)體,一般為長(zhǎng)整型。
這個(gè)函數(shù)會(huì)獲取當(dāng)前時(shí)間,并返回。 如果參數(shù)__timer非空,會(huì)存儲(chǔ)相同值到__timer指向的內(nèi)存中。
time函數(shù)返回的為unix時(shí)間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經(jīng)過的秒數(shù),不考慮閏秒。
由于是秒作為單位的,所以這并不是習(xí)慣上的時(shí)間,要轉(zhuǎn)為習(xí)慣上的年月日時(shí)間形式就需要另外一個(gè)函數(shù)了。
2、localtime函數(shù)。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個(gè)結(jié)構(gòu)體,包含了年月日時(shí)分秒等信息。
這種結(jié)構(gòu)是適合用來輸出的。
二、參考代碼:
#include?stdio.h
#include?time.h
int?main?()
{
time_t?t;
struct?tm?*?lt;
time?(t);//獲取Unix時(shí)間戳。
lt?=?localtime?(t);//轉(zhuǎn)為時(shí)間結(jié)構(gòu)。
printf?(?"%d/%d/%d?%d:%d:%d\n",lt-tm_year+1900,?lt-tm_mon,?lt-tm_mday,?lt-tm_hour,?lt-tm_min,?lt-tm_sec);//輸出結(jié)果
return?0;
}
注意事項(xiàng):
struct tm中的tm_year 值為實(shí)際年減去1900, 所以輸出的時(shí)候要是lt-tm_year+1900。
網(wǎng)站標(biāo)題:c語言調(diào)用時(shí)間的函數(shù)是 c語言程序運(yùn)行時(shí)間函數(shù)
網(wǎng)站網(wǎng)址:http://www.yijiale78.com/article28/ddogjjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、電子商務(wù)、定制網(wǎng)站、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、建站公司
聲明:本網(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)