c語(yǔ)言取時(shí)間函數(shù) c語(yǔ)言獲取當(dāng)前時(shí)間的函數(shù)

c語(yǔ)言 時(shí)間函數(shù)

c語(yǔ)言時(shí)間函數(shù):

為陜西等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及陜西網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站制作、陜西網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

1、獲得日歷時(shí)間函數(shù):

可以通過(guò)time()函數(shù)來(lái)獲得日歷時(shí)間(Calendar Time),其原型為:time_t time(time_t * timer);

如果已經(jīng)聲明了參數(shù)timer,可以從參數(shù)timer返回現(xiàn)在的日歷時(shí)間,同時(shí)也可以通過(guò)返回值返回現(xiàn)在的日歷時(shí)間,即從一個(gè)時(shí)間點(diǎn)(例如:1970年1月1日0時(shí)0分0秒)到現(xiàn)在此時(shí)的秒數(shù)。如果參數(shù)為空(NUL),函數(shù)將只通過(guò)返回值返回現(xiàn)在的日歷時(shí)間,比如下面這個(gè)例子用來(lái)顯示當(dāng)前的日歷時(shí)間:

2、獲得日期和時(shí)間函數(shù):

這里說(shuō)的日期和時(shí)間就是平時(shí)所說(shuō)的年、月、日、時(shí)、分、秒等信息。從第2節(jié)我們已經(jīng)知道這些信息都保存在一個(gè)名為tm的結(jié)構(gòu)體中,那么如何將一個(gè)日歷時(shí)間保存為一個(gè)tm結(jié)構(gòu)的對(duì)象呢?

其中可以使用的函數(shù)是gmtime()和localtime(),這兩個(gè)函數(shù)的原型為:

struct tm * gmtime(const time_t *timer);

struct tm * localtime(const time_t * timer);

其中g(shù)mtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時(shí)間(即格林尼治時(shí)間),并返回一個(gè)tm結(jié)構(gòu)體來(lái)保存這個(gè)時(shí)間,而localtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標(biāo)準(zhǔn)時(shí)間是2005年7月30日7點(diǎn)18分20秒,那么用localtime()函數(shù)在中國(guó)地區(qū)獲得的本地時(shí)間會(huì)比世界標(biāo)準(zhǔn)時(shí)間晚8個(gè)小時(shí),即2005年7月30日15點(diǎn)18分20秒。

c語(yǔ)言時(shí)間處理函數(shù)

C語(yǔ)言的標(biāo)準(zhǔn)庫(kù)函數(shù)包括一系列日期和時(shí)間處理函數(shù),它們都在頭文件中說(shuō)明。

在頭文件中定義了三種類(lèi)型:time_t,struct tm和clock_t。

下面列出了這些函數(shù)。

time_t time(time_t *timer);

double difftime(time_t time1,time_t time2);

struct tm *gmtime(const time_t *timer);

struct tm *localtime(const time_t *timer);

char *asctime(const struct tm *timeptr);

char *ctime(const time_t *timer);

size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr);

time_t mktime(struct tm *timeptr);

clock_t clock(void);

【具體應(yīng)用舉例】

asctime(將時(shí)間和日期以字符串格式表示)

相關(guān)函數(shù)

time,ctime,gmtime,localtime

表頭文件

#i nclude

定義函數(shù)

char * asctime(const struct tm * timeptr);

函數(shù)說(shuō)明

asctime()將參數(shù)timeptr所指的tm結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時(shí)間日期表示方法,然后將結(jié)果以字符串形態(tài)返回。

此函數(shù)已經(jīng)由時(shí)區(qū)轉(zhuǎn)換成當(dāng)?shù)貢r(shí)間,字符串格式為:"Wed Jun 30 21:49:08 1993\n"

返回值

若再調(diào)用相關(guān)的時(shí)間日期函數(shù),此字符串可能會(huì)被破壞。此函數(shù)與ctime不同處在于傳入的參數(shù)是不同的結(jié)構(gòu)。

附加說(shuō)明

返回一字符串表示目前當(dāng)?shù)氐臅r(shí)間日期。

范例

#i nclude

main()

{

time_t timep;

time (timep);

printf("%s",asctime(gmtime(timep)));

}

執(zhí)行

Sat Oct 28 02:10:06 2000

 

ctime(將時(shí)間和日期以字符串格式表示)

相關(guān)函數(shù)

time,asctime,gmtime,localtime

表頭文件

#i nclude

定義函數(shù)

char *ctime(const time_t *timep);

函數(shù)說(shuō)明

ctime ()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時(shí)間日期表示方法,然后將結(jié)果以字符串形態(tài)返回。

此函數(shù)已經(jīng)由時(shí)區(qū)轉(zhuǎn)換成當(dāng)?shù)貢r(shí)間,字符串格式為"Wed Jun 30 21 :49 :08 1993\n"。若再調(diào)用相關(guān)的時(shí)間日期函數(shù),此字符串可能會(huì)被破壞。

返回值

返回一字符串表示目前當(dāng)?shù)氐臅r(shí)間日期。

范例

#i nclude

main()

{

time_t timep;

time (timep);

printf("%s",ctime(timep));

}

執(zhí)行

Sat Oct 28 10 : 12 : 05 2000

 

gettimeofday(取得目前的時(shí)間)

相關(guān)函數(shù)

time,ctime,ftime,settimeofday

表頭文件

#i nclude

#i nclude

定義函數(shù)

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

函數(shù)說(shuō)明

gettimeofday()會(huì)把目前的時(shí)間有tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r(shí)區(qū)的信息則放到tz所指的結(jié)構(gòu)中。

timeval結(jié)構(gòu)定義為:

struct timeval{

long tv_sec; /*秒*/

long tv_usec; /*微秒*/

};

timezone 結(jié)構(gòu)定義為:

struct timezone{

int tz_minuteswest; /*和Greenwich 時(shí)間差了多少分鐘*/

int tz_dsttime; /*日光節(jié)約時(shí)間的狀態(tài)*/

};

上述兩個(gè)結(jié)構(gòu)都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態(tài)如下

DST_NONE /*不使用*/

DST_USA /*美國(guó)*/

DST_AUST /*澳洲*/

DST_WET /*西歐*/

DST_MET /*中歐*/

DST_EET /*東歐*/

DST_CAN /*加拿大*/

DST_GB /*大不列顛*/

DST_RUM /*羅馬尼亞*/

DST_TUR /*土耳其*/

DST_AUSTALT /*澳洲(1986年以后)*/

返回值

成功則返回0,失敗返回-1,錯(cuò)誤代碼存于errno。附加說(shuō)明EFAULT指針tv和tz所指的內(nèi)存空間超出存取權(quán)限。

范例

#i nclude

#i nclude

main(){

struct timeval tv;

struct timezone tz;

gettimeofday (tv , tz);

printf("tv_sec; %d\n", tv,.tv_sec) ;

printf("tv_usec; %d\n",tv.tv_usec);

printf("tz_minuteswest; %d\n", tz.tz_minuteswest);

printf("tz_dsttime, %d\n",tz.tz_dsttime);

}

執(zhí)行

tv_sec: 974857339

tv_usec:136996

tz_minuteswest:-540

tz_dsttime:0

 

gmtime(取得目前時(shí)間和日期)

相關(guān)函數(shù)

time,asctime,ctime,localtime

表頭文件

#i nclude

定義函數(shù)

struct tm*gmtime(const time_t*timep);

函數(shù)說(shuō)明

gmtime()將參數(shù)timep 所指的time_t 結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時(shí)間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回。

結(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;

};

int tm_sec 代表目前秒數(shù),正常范圍為0-59,但允許至61秒

int tm_min 代表目前分?jǐn)?shù),范圍0-59

int tm_hour 從午夜算起的時(shí)數(shù),范圍為0-23

int tm_mday 目前月份的日數(shù),范圍01-31

int tm_mon 代表目前月份,從一月算起,范圍從0-11

int tm_year 從1900 年算起至今的年數(shù)

int tm_wday 一星期的日數(shù),從星期一算起,范圍為0-6

int tm_yday 從今年1月1日算起至今的天數(shù),范圍為0-365

int tm_isdst 日光節(jié)約時(shí)間的旗標(biāo)

此函數(shù)返回的時(shí)間日期未經(jīng)時(shí)區(qū)轉(zhuǎn)換,而是UTC時(shí)間。

返回值

返回結(jié)構(gòu)tm代表目前UTC 時(shí)間

范例

#i nclude

main(){

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

time_t timep;

struct tm *p;

time(timep);

p=gmtime(timep);

printf("%d%d%d",(1900+p-tm_year), (1+p-tm_mon),p-tm_mday);

printf("%s%d;%d;%d\n", wday[p-tm_wday], p-tm_hour, p-tm_min, p-tm_sec);

}

執(zhí)行

2000/10/28 Sat 8:15:38

 

localtime(取得當(dāng)?shù)啬壳皶r(shí)間和日期)

相關(guān)函數(shù)

time, asctime, ctime, gmtime

表頭文件

#i nclude

定義函數(shù)

struct tm *localtime(const time_t * timep);

函數(shù)說(shuō)明

localtime()將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時(shí)間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回。

結(jié)構(gòu)tm的定義請(qǐng)參考gmtime()。此函

數(shù)返回的時(shí)間日期已經(jīng)轉(zhuǎn)換成當(dāng)?shù)貢r(shí)區(qū)。

返回值

返回結(jié)構(gòu)tm代表目前的當(dāng)?shù)貢r(shí)間。

范例

#i nclude

main(){

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

time_t timep;

struct tm *p;

time(timep);

p=localtime(timep); /*取得當(dāng)?shù)貢r(shí)間*/

printf ("%d%d%d ", (1900+p-tm_year),( l+p-tm_mon), p-tm_mday);

printf("%s%d:%d:%d\n", wday[p-tm_wday],p-tm_hour, p-tm_min, p-tm_sec);

}

執(zhí)行

2000/10/28 Sat 11:12:22

 

mktime(將時(shí)間結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成經(jīng)過(guò)的秒數(shù))

相關(guān)函數(shù)

time,asctime,gmtime,localtime

表頭文件

#i nclude

定義函數(shù)

time_t mktime(strcut tm * timeptr);

函數(shù)說(shuō)明

mktime()用來(lái)將參數(shù)timeptr所指的tm結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成從公元1970年1月1日0時(shí)0分0 秒算起至今的UTC時(shí)間所經(jīng)過(guò)的秒數(shù)。

返回值

返回經(jīng)過(guò)的秒數(shù)。

范例

/* 用time()取得時(shí)間(秒數(shù)),利用localtime()

轉(zhuǎn)換成struct tm 再利用mktine()將struct tm轉(zhuǎn)換成原來(lái)的秒數(shù)*/

#i nclude

main()

{

time_t timep;

strcut tm *p;

time(timep);

printf("time() : %d \n",timep);

p=localtime(timep);

timep = mktime(p);

printf("time()-localtime()-mktime():%d\n",timep);

}

執(zhí)行

time():974943297

time()-localtime()-mktime():974943297

 

settimeofday(設(shè)置目前時(shí)間)

相關(guān)函數(shù)

time,ctime,ftime,gettimeofday

表頭文件

#i nclude

#i nclude

定義函數(shù)

int settimeofday ( const struct timeval *tv,const struct timezone *tz);

函數(shù)說(shuō)明

settimeofday()會(huì)把目前時(shí)間設(shè)成由tv所指的結(jié)構(gòu)信息,當(dāng)?shù)貢r(shí)區(qū)信息則設(shè)成tz所指的結(jié)構(gòu)。詳細(xì)的說(shuō)明請(qǐng)參考gettimeofday()。

注意,只有root權(quán)限才能使用此函數(shù)修改時(shí)間。

返回值

成功則返回0,失敗返回-1,錯(cuò)誤代碼存于errno。

錯(cuò)誤代碼

EPERM 并非由root權(quán)限調(diào)用settimeofday(),權(quán)限不夠。

EINVAL 時(shí)區(qū)或某個(gè)數(shù)據(jù)是不正確的,無(wú)法正確設(shè)置時(shí)間。

 

time(取得目前的時(shí)間)

相關(guān)函數(shù)

ctime,ftime,gettimeofday

表頭文件

#i nclude

定義函數(shù)

time_t time(time_t *t);

函數(shù)說(shuō)明

此函數(shù)會(huì)返回從公元1970年1月1日的UTC時(shí)間從0時(shí)0分0秒算起到現(xiàn)在所經(jīng)過(guò)的秒數(shù)。如果t 并非空指針的話,

此函數(shù)也會(huì)將返回值存到t指針?biāo)傅膬?nèi)存。

返回值

成功則返回秒數(shù),失敗則返回((time_t)-1)值,錯(cuò)誤原因存于errno中。

范例

#i nclude

mian()

{

int seconds= time((time_t*)NULL);

printf("%d\n",seconds);

}

Date and Time Functions: time.h

The header time.h declares types and functions for manipulating date and time. Some functions process local time,

which may differ from calendar time, for example because of time zone. clock_t and time_t are arithmetic types

representing times, and struct tm holds the components

of a calendar time:

int tm_sec; seconds after the minute (0,61)

int tm_min; minutes after the hour (0,59)

int tm_hour; hours since midnight (0,23)

int tm_mday; day of the month (1,31)

int tm_mon; months since January (0,11)

int tm_year; years since 1900

int tm_wday; days since Sunday (0,6)

int tm_yday; days since January 1 (0,365)

int tm_isdst; Daylight Saving Time flag

tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available.

clock_t clock(void)

clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable.

clock()/CLK_PER_SEC is a time in

seconds.

time_t time(time_t *tp)

time returns the current calendar time or -1 if the time is not available. If tp is not NULL,

the return value is also assigned to *tp.

double difftime(time_t time2, time_t time1)

difftime returns time2-time1 expressed in seconds.

time_t mktime(struct tm *tp)

mktime converts the local time in the structure *tp into calendar time in the same representation used by time.

The components will have values

in the ranges shown. mktime returns the calendar time or -1 if it cannot be represented.

The next four functions return pointers to static objects that may be overwritten by other calls.

char *asctime(const struct tm *tp)

asctime*tp into a string of the form

Sun Jan 3 15:14:13 1988\n\0

char *ctime(const time_t *tp)

ctime converts the calendar time *tp to local time; it is equivalent to

asctime(localtime(tp))

struct tm *gmtime(const time_t *tp)

gmtime converts the calendar time *tp into Coordinated Universal Time (UTC). It returns NULL if UTC is not available.

The name gmtime has historical significance.

struct tm *localtime(const time_t *tp)

localtime converts the calendar time *tp into local time.

size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)

strftime formats date and time information from *tp into s according to fmt, which is analogous to a printf format.

Ordinary characters (including the terminating '\0') are copied into s. Each %c is replaced as described below,

using values appropriate for the local environment.

No more than smax characters are placed into s. strftime returns the number of characters, excluding the '\0',

or zero if more than smax characters were produced.

%a abbreviated weekday name.

%A full weekday name.

%b abbreviated month name.

%B full month name.

%c local date and time representation.

%d day of the month (01-31).

%H hour (24-hour clock) (00-23).

%I hour (12-hour clock) (01-12).

%j day of the year (001-366).

%m month (01-12).

%M minute (00-59).

%p local equivalent of AM or PM.

%S second (00-61).

%U week number of the year (Sunday as 1st day of week) (00-53).

%w weekday (0-6, Sunday is 0).

%W week number of the year (Monday as 1st day of week) (00-53).

%x local date representation.

%X local time representation.

%y year without century (00-99).

%Y year with century.

%Z time zone name, if any.

%% %

C語(yǔ)言獲取系統(tǒng)時(shí)間

需要利用C語(yǔ)言的時(shí)間函數(shù)time和localtime,具體說(shuō)明如下:

一、函數(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的午夜)開(kāi)始所經(jīng)過(guò)的秒數(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)是適合用來(lái)輸出的。

C語(yǔ)言中時(shí)間的函數(shù)

一.概念

在C/C++中,通過(guò)學(xué)習(xí)許多C/C++庫(kù),你可以有很多操作、使用時(shí)間的方法。但在這之前你需要了解一些“時(shí)間”和“日期”的概念,主要有以下幾個(gè):

1. 協(xié)調(diào)世界時(shí),又稱(chēng)為世界標(biāo)準(zhǔn)時(shí)間,也就是大家所熟知的格林威治標(biāo)準(zhǔn)時(shí)間(Greenwich Mean Time,GMT)。比如,中國(guó)內(nèi)地的時(shí)間與UTC的時(shí)差為+8,也就是UTC+8。美國(guó)是UTC-5。

2. 日歷時(shí)間,是用“從一個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)到此時(shí)的時(shí)間經(jīng)過(guò)的秒數(shù)”來(lái)表示的時(shí)間。這個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)對(duì)不同的編譯器來(lái)說(shuō)會(huì)有所不同,但對(duì)一個(gè)編譯系統(tǒng)來(lái)說(shuō),這個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)是不變的,該編譯系統(tǒng)中的時(shí)間對(duì)應(yīng)的日歷時(shí)間都通過(guò)該標(biāo)準(zhǔn)時(shí)間點(diǎn)來(lái)衡量,所以可以說(shuō)日歷時(shí)間是“相對(duì)時(shí)間”,但是無(wú)論你在哪一個(gè)時(shí)區(qū),在同一時(shí)刻對(duì)同一個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)來(lái)說(shuō),日歷時(shí)間都是一樣的。

3. 時(shí)間點(diǎn)。時(shí)間點(diǎn)在標(biāo)準(zhǔn)C/C++中是一個(gè)整數(shù),它用此時(shí)的時(shí)間和標(biāo)準(zhǔn)時(shí)間點(diǎn)相差的秒數(shù)(即日歷時(shí)間)來(lái)表示。

4. 時(shí)鐘計(jì)時(shí)單元(而不把它叫做時(shí)鐘滴答次數(shù)),一個(gè)時(shí)鐘計(jì)時(shí)單元的時(shí)間長(zhǎng)短是由CPU控制的。一個(gè)clock tick不是CPU的一個(gè)時(shí)鐘周期,而是C/C++的一個(gè)基本計(jì)時(shí)單位。

我們可以使用ANSI標(biāo)準(zhǔn)庫(kù)中的time.h頭文件。這個(gè)頭文件中定義的時(shí)間和日期所使用的方法,無(wú)論是在結(jié)構(gòu)定義,還是命名,都具有明顯的C語(yǔ)言風(fēng)格。下面,我將說(shuō)明在C/C++中怎樣使用日期的時(shí)間功能。

二. 介紹

1. 計(jì)時(shí)

C/C++中的計(jì)時(shí)函數(shù)是clock(),而與其相關(guān)的數(shù)據(jù)類(lèi)型是clock_t。在MSDN中,查得對(duì)clock函數(shù)定義如下:

clock_t clock( void );

這個(gè)函數(shù)返回從“開(kāi)啟這個(gè)程序進(jìn)程”到“程序中調(diào)用clock()函數(shù)”時(shí)之間的CPU時(shí)鐘計(jì)時(shí)單元(clock tick)數(shù),在MSDN中稱(chēng)之為掛鐘時(shí)間(wal-clock)。其中clock_t是用來(lái)保存時(shí)間的數(shù)據(jù)類(lèi)型,在time.h文件中,我們可以找到對(duì)它的定義:

#ifndef _CLOCK_T_DEFINED

typedef long clock_t;

#define _CLOCK_T_DEFINED

#endif

很明顯,clock_t是一個(gè)長(zhǎng)整形數(shù)。在time.h文件中,還定義了一個(gè)常量CLOCKS_PER_SEC,它用來(lái)表示一秒鐘會(huì)有多少個(gè)時(shí)鐘計(jì)時(shí)單元,其定義如下:

#define CLOCKS_PER_SEC ((clock_t)1000)

可以看到每過(guò)千分之一秒(1毫秒),調(diào)用clock()函數(shù)返回的值就加1。下面舉個(gè)例子,你可以使用公式clock()/CLOCKS_PER_SEC來(lái)計(jì)算一個(gè)進(jìn)程自身的運(yùn)行時(shí)間:

void elapsed_time()

{

printf("Elapsed time:%u secs. ",clock()/CLOCKS_PER_SEC);

}

當(dāng)然,你也可以用clock函數(shù)來(lái)計(jì)算你的機(jī)器運(yùn)行一個(gè)循環(huán)或者處理其它事件到底花了多少時(shí)間:

/* 測(cè)量一個(gè)事件持續(xù)的時(shí)間*/

/* Date : 10/24/2007 */

#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 ", duration );

system("pause");

}

在筆者的機(jī)器上,運(yùn)行結(jié)果如下:

Time to do 10000000 empty loops is 0.03000 seconds

上面我們看到時(shí)鐘計(jì)時(shí)單元的長(zhǎng)度為1毫秒,那么計(jì)時(shí)的精度也為1毫秒,那么我們可不可以通過(guò)改變CLOCKS_PER_SEC的定義,通過(guò)把它定義的大一些,從而使計(jì)時(shí)精度更高呢?通過(guò)嘗試,你會(huì)發(fā)現(xiàn)這樣是不行的。在標(biāo)準(zhǔn)C/C++中,最小的計(jì)時(shí)單位是一毫秒。

2.與日期和時(shí)間相關(guān)的數(shù)據(jù)結(jié)構(gòu)

在標(biāo)準(zhǔn)C/C++中,我們可通過(guò)tm結(jié)構(gòu)來(lái)獲得日期和時(shí)間,tm結(jié)構(gòu)在time.h中的定義如下:

#ifndef _TM_DEFINED

struct tm {

int tm_sec; /* 秒 – 取值區(qū)間為[0,59] */

int tm_min; /* 分 - 取值區(qū)間為[0,59] */

int tm_hour; /* 時(shí) - 取值區(qū)間為[0,23] */

int tm_mday; /* 一個(gè)月中的日期 - 取值區(qū)間為[1,31] */

int tm_mon; /* 月份(從一月開(kāi)始,0代表一月) - 取值區(qū)間為[0,11] */

int tm_year; /* 年份,其值等于實(shí)際年份減去1900 */

int tm_wday; /* 星期 – 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一,以此類(lèi)推 */

int tm_yday; /* 從每年的1月1日開(kāi)始的天數(shù) – 取值區(qū)間為[0,365],其中0代表1月1日,1代表1月2日,以此類(lèi)推 */

int tm_isdst; /* 夏令時(shí)標(biāo)識(shí)符,實(shí)行夏令時(shí)的時(shí)候,tm_isdst為正。不實(shí)行夏令時(shí)的進(jìn)候,tm_isdst為0;不了解情況時(shí),tm_isdst()為負(fù)。*/

};

#define _TM_DEFINED

#endif

ANSI C標(biāo)準(zhǔn)稱(chēng)使用tm結(jié)構(gòu)的這種時(shí)間表示為分解時(shí)間(broken-down time)。

而日歷時(shí)間(Calendar Time)是通過(guò)time_t數(shù)據(jù)類(lèi)型來(lái)表示的,用time_t表示的時(shí)間(日歷時(shí)間)是從一個(gè)時(shí)間點(diǎn)(例如:1970年1月1日0時(shí)0分0秒)到此時(shí)的秒數(shù)。在time.h中,我們也可以看到time_t是一個(gè)長(zhǎng)整型數(shù):

#ifndef _TIME_T_DEFINED

typedef long time_t; /* 時(shí)間值 */

#define _TIME_T_DEFINED /* 避免重復(fù)定義 time_t */

#endif

大家可能會(huì)產(chǎn)生疑問(wèn):既然time_t實(shí)際上是長(zhǎng)整型,到未來(lái)的某一天,從一個(gè)時(shí)間點(diǎn)(一般是1970年1月1日0時(shí)0分0秒)到那時(shí)的秒數(shù)(即日歷時(shí)間)超出了長(zhǎng)整形所能表示的數(shù)的范圍怎么辦?對(duì)time_t數(shù)據(jù)類(lèi)型的值來(lái)說(shuō),它所表示的時(shí)間不能晚于2038年1月18日19時(shí)14分07秒。為了能夠表示更久遠(yuǎn)的時(shí)間,一些編譯器廠商引入了64位甚至更長(zhǎng)的整形數(shù)來(lái)保存日歷時(shí)間。比如微軟在Visual C++中采用了__time64_t數(shù)據(jù)類(lèi)型來(lái)保存日歷時(shí)間,并通過(guò)_time64()函數(shù)來(lái)獲得日歷時(shí)間(而不是通過(guò)使用32位字的time()函數(shù)),這樣就可以通過(guò)該數(shù)據(jù)類(lèi)型保存3001年1月1日0時(shí)0分0秒(不包括該時(shí)間點(diǎn))之前的時(shí)間。

在time.h頭文件中,我們還可以看到一些函數(shù),它們都是以time_t為參數(shù)類(lèi)型或返回值類(lèi)型的函數(shù):

double difftime(time_t time1, time_t time0);

time_t mktime(struct tm * timeptr);

time_t time(time_t * timer);

char * asctime(const struct tm * timeptr);

char * ctime(const time_t *timer);

此外,time.h還提供了兩種不同的函數(shù)將日歷時(shí)間(一個(gè)用time_t表示的整數(shù))轉(zhuǎn)換為我們平時(shí)看到的把年月日時(shí)分秒分開(kāi)顯示的時(shí)間格式tm:

struct tm * gmtime(const time_t *timer);

struct tm * localtime(const time_t * timer);

通過(guò)查閱MSDN,我們可以知道Microsoft C/C++ 7.0中時(shí)間點(diǎn)的值(time_t對(duì)象的值)是從1899年12月31日0時(shí)0分0秒到該時(shí)間點(diǎn)所經(jīng)過(guò)的秒數(shù),而其它各種版本的Microsoft C/C++和所有不同版本的Visual C++都是計(jì)算的從1970年1月1日0時(shí)0分0秒到該時(shí)間點(diǎn)所經(jīng)過(guò)的秒數(shù)。

3.與日期和時(shí)間相關(guān)的函數(shù)及應(yīng)用

在本節(jié),我將向大家展示怎樣利用time.h中聲明的函數(shù)對(duì)時(shí)間進(jìn)行操作。這些操作包括取當(dāng)前時(shí)間、計(jì)算時(shí)間間隔、以不同的形式顯示時(shí)間等內(nèi)容。

4. 獲得日歷時(shí)間

我們可以通過(guò)time()函數(shù)來(lái)獲得日歷時(shí)間(Calendar Time),其原型為:

time_t time(time_t * timer);

如果你已經(jīng)聲明了參數(shù)timer,你可以從參數(shù)timer返回現(xiàn)在的日歷時(shí)間,同時(shí)也可以通過(guò)返回值返回現(xiàn)在的日歷時(shí)間,即從一個(gè)時(shí)間點(diǎn)(例如:1970年1月1日0時(shí)0分0秒)到現(xiàn)在此時(shí)的秒數(shù)。如果參數(shù)為空(NUL),函數(shù)將只通過(guò)返回值返回現(xiàn)在的日歷時(shí)間,比如下面這個(gè)例子用來(lái)顯示當(dāng)前的日歷時(shí)間:

運(yùn)行的結(jié)果與當(dāng)時(shí)的時(shí)間有關(guān),我當(dāng)時(shí)運(yùn)行的'結(jié)果是:

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

time_t lt;

lt =time(NULL);

printf("The Calendar Time now is %d ",lt);

return 0;

}

The Calendar Time now is 1122707619

其中1122707619就是我運(yùn)行程序時(shí)的日歷時(shí)間。即從1970-01-01 08:00:00到此時(shí)的秒數(shù)。

5. 獲得日期和時(shí)間

這里說(shuō)的日期和時(shí)間就是我們平時(shí)所說(shuō)的年、月、日、時(shí)、分、秒等信息。從第2節(jié)我們已經(jīng)知道這些信息都保存在一個(gè)名為tm的結(jié)構(gòu)體中,那么如何將一個(gè)日歷時(shí)間保存為一個(gè)tm結(jié)構(gòu)的對(duì)象呢?

其中可以使用的函數(shù)是gmtime()和localtime(),這兩個(gè)函數(shù)的原型為:

struct tm * gmtime(const time_t *timer);

struct tm * localtime(const time_t * timer);

其中g(shù)mtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時(shí)間(即格林尼治時(shí)間),并返回一個(gè)tm結(jié)構(gòu)體來(lái)保存這個(gè)時(shí)間,而localtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標(biāo)準(zhǔn)時(shí)間是2005年7月30日7點(diǎn)18分20秒,那么我用localtime()函數(shù)在中國(guó)地區(qū)獲得的本地時(shí)間會(huì)比世界標(biāo)準(zhǔn)時(shí)間晚8個(gè)小時(shí),即2005年7月30日15點(diǎn)18分20秒。下面是個(gè)例子:

//本地時(shí)間,世界標(biāo)準(zhǔn)時(shí)間

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm *local;

time_t t;

t=time(NULL);

local=localtime(t);

printf("Local hour is: %d:%d:%d ",local-tm_hour,local-tm_min,local-tm_sec);

local=gmtime(t);

printf("UTC hour is: %d:%d:%d ",local-tm_hour,local-tm_min,local-tm_sec);

return 0;

}

運(yùn)行結(jié)果是:

Local hour is: 23:17:47

UTC hour is: 15:17:47

6. 固定的時(shí)間格式

我們可以通過(guò)asctime()函數(shù)和ctime()函數(shù)將時(shí)間以固定的格式顯示出來(lái),兩者的返回值都是char*型的字符串。返回的時(shí)間格式為:

星期幾 月份 日期 時(shí):分:秒 年

例如:Wed Jan 02 02:03:55 1980

其中 是一個(gè)換行符,是一個(gè)空字符,表示字符串結(jié)束。下面是兩個(gè)函數(shù)的原型:

char * asctime(const struct tm * timeptr);

char * ctime(const time_t *timer);

其中asctime()函數(shù)是通過(guò)tm結(jié)構(gòu)來(lái)生成具有固定格式的保存時(shí)間信息的字符串,而ctime()是通過(guò)日歷時(shí)間來(lái)生成時(shí)間字符串。這樣的話,asctime()函數(shù)只是把tm結(jié)構(gòu)對(duì)象中的各個(gè)域填到時(shí)間字符串的相應(yīng)位置就行了,而ctime()函數(shù)需要先參照本地的時(shí)間設(shè)置,把日歷時(shí)間轉(zhuǎn)化為本地時(shí)間,然后再生成格式化后的字符串。在下面,如果t是一個(gè)非空的time_t變量的話,那么:

printf(ctime(t));

等價(jià)于:

struct tm *ptr;

ptr=localtime(t);

printf(asctime(ptr));

那么,下面這個(gè)程序的兩條printf語(yǔ)句輸出的結(jié)果就是不同的了(除非你將本地時(shí)區(qū)設(shè)為世界標(biāo)準(zhǔn)時(shí)間所在的時(shí)區(qū)):

//本地時(shí)間,世界標(biāo)準(zhǔn)時(shí)間

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm *ptr;

time_t lt;

lt =time(NULL);

ptr=gmtime();

printf(asctime(ptr));

printf(ctime());

return 0;

}

運(yùn)行結(jié)果:

Sat Jul 30 08:43:03 2005

Sat Jul 30 16:43:03 2005

7. 自定義時(shí)間格式

我們可以使用strftime()函數(shù)將時(shí)間格式化為我們想要的格式。它的原型如下:

size_t strftime(

char *strDest,

size_t maxsize,

const char *format,

const struct tm *timeptr

);

我們可以根據(jù)format指向字符串中格式命令把timeptr中保存的時(shí)間信息放在strDest指向的字符串中,最多向strDest中存放maxsize個(gè)字符。該函數(shù)返回向strDest指向的字符串中放置的字符數(shù)。

函數(shù)strftime()的操作有些類(lèi)似于sprintf():識(shí)別以百分號(hào)(%)開(kāi)始的格式命令集合,格式化輸出結(jié)果放在一個(gè)字符串中。格式化命令說(shuō)明串strDest中各種日期和時(shí)間信息的確切表示方法。格式串中的其他字符原樣放進(jìn)串中。格式命令列在下面,它們是區(qū)分大小寫(xiě)的。

%a 星期幾的簡(jiǎn)寫(xiě)

%A 星期幾的全稱(chēng)

%b 月分的簡(jiǎn)寫(xiě)

%B 月份的全稱(chēng)

%c 標(biāo)準(zhǔn)的日期的時(shí)間串

%C 年份的后兩位數(shù)字

%d 十進(jìn)制表示的每月的第幾天

%D 月/天/年

%e 在兩字符域中,十進(jìn)制表示的每月的第幾天

%F 年-月-日

%g 年份的后兩位數(shù)字,使用基于周的年

%G 年分,使用基于周的年

%h 簡(jiǎn)寫(xiě)的月份名

%H 24小時(shí)制的小時(shí)

%I 12小時(shí)制的小時(shí)

%j 十進(jìn)制表示的每年的第幾天

%m 十進(jìn)制表示的月份

%M 十時(shí)制表示的分鐘數(shù)

%n 新行符

%p 本地的AM或PM的等價(jià)顯示

%r 12小時(shí)的時(shí)間

%R 顯示小時(shí)和分鐘:hh:mm

%S 十進(jìn)制的秒數(shù)

%t 水平制表符

%T 顯示時(shí)分秒:hh:mm:ss

%u 每周的第幾天,星期一為第一天 (值從0到6,星期一為0)

%U 第年的第幾周,把星期日做為第一天(值從0到53)

%V 每年的第幾周,使用基于周的年

%w 十進(jìn)制表示的星期幾(值從0到6,星期天為0)

%W 每年的第幾周,把星期一做為第一天(值從0到53)

%x 標(biāo)準(zhǔn)的日期串

%X 標(biāo)準(zhǔn)的時(shí)間串

%y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)

%Y 帶世紀(jì)部分的十進(jìn)制年份

%z,%Z 時(shí)區(qū)名稱(chēng),如果不能得到時(shí)區(qū)名稱(chēng)則返回空字符。

%% 百分號(hào)

如果想顯示現(xiàn)在是幾點(diǎn)了,并以12小時(shí)制顯示,就象下面這段程序:

//顯示現(xiàn)在是幾點(diǎn)了,并以12小時(shí)制顯示

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm *ptr;

time_t localTime;

char str[80];

localTime=time(NULL);

ptr=localtime(localTime);

strftime(str,100,"It is now %I %p ",ptr);

printf(str);

return 0;

}

其運(yùn)行結(jié)果為:

It is now 4PM

而下面的程序則顯示當(dāng)前的完整日期:

//顯示當(dāng)前的完整日期

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

void main( void )

{

struct tm *newtime;

char tmpbuf[128];

time_t localTime1;

time( localTime1 );

newtime=localtime(localTime1);

strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y. ", newtime);

printf(tmpbuf);

}

運(yùn)行結(jié)果:

Today is Saturday, day 30 of July in the year 2005.

8. 計(jì)算持續(xù)時(shí)間的長(zhǎng)度

有時(shí)候在實(shí)際應(yīng)用中要計(jì)算一個(gè)事件持續(xù)的時(shí)間長(zhǎng)度,比如計(jì)算打字速度。在第1節(jié)計(jì)時(shí)部分中,我已經(jīng)用clock函數(shù)舉了一個(gè)例子。Clock()函數(shù)可以精確到毫秒級(jí)。同時(shí),我們也可以使用difftime()函數(shù),但它只能精確到秒。該函數(shù)的定義如下:

double difftime(time_t time1, time_t time0);

雖然該函數(shù)返回的以秒計(jì)算的時(shí)間間隔是double類(lèi)型的,但這并不說(shuō)明該時(shí)間具有同double一樣的精確度,這是由它的參數(shù)覺(jué)得的(time_t是以秒為單位計(jì)算的)。比如下面一段程序:

//計(jì)算持續(xù)時(shí)間的長(zhǎng)度

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

time_t start,end;

start = time(NULL);

system("pause");

end = time(NULL);

printf("The pause used %f seconds. ",difftime(end,start));//-

system("pause");

return 0;

}

運(yùn)行結(jié)果為:

請(qǐng)按任意鍵繼續(xù). . .

The pause used 2.000000 seconds.

請(qǐng)按任意鍵繼續(xù). . .

可以想像,暫停的時(shí)間并不那么巧是整整2秒鐘。其實(shí),你將上面程序的帶有“//-”注釋的一行用下面的一行代碼替換:

printf("The pause used %f seconds. ",end-start);

其運(yùn)行結(jié)果是一樣的。

9. 分解時(shí)間轉(zhuǎn)化為日歷時(shí)間

這里說(shuō)的分解時(shí)間就是以年、月、日、時(shí)、分、秒等分量保存的時(shí)間結(jié)構(gòu),在C/C++中是tm結(jié)構(gòu)。我們可以使用mktime()函數(shù)將用tm結(jié)構(gòu)表示的時(shí)間轉(zhuǎn)化為日歷時(shí)間。其函數(shù)原型如下:

time_t mktime(struct tm * timeptr);

其返回值就是轉(zhuǎn)化后的日歷時(shí)間。這樣我們就可以先制定一個(gè)分解時(shí)間,然后對(duì)這個(gè)時(shí)間進(jìn)行操作了,下面的例子可以計(jì)算出1997年7月1日是星期幾:

//計(jì)算出1997年7月1日是星期幾

/* Date : 10/24/2007 */

/* Author: Eman Lee */

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

int main(void)

{

struct tm time;

time_t t_of_day;

time.tm_year=1997-1900;

time.tm_mon=6;

time.tm_mday=1;

time.tm_hour=0;

time.tm_min=0;

time.tm_sec=1;

time.tm_isdst=0;

t_of_day=mktime(time);

printf(ctime(t_of_day));

return 0;

}

運(yùn)行結(jié)果:

Tue Jul 01 00:00:01 1997

有了mktime()函數(shù),是不是我們可以操作現(xiàn)在之前的任何時(shí)間呢?你可以通過(guò)這種辦法算出1945年8月15號(hào)是星期幾嗎?答案是否定的。因?yàn)檫@個(gè)時(shí)間在1970年1月1日之前,所以在大多數(shù)編譯器中,這樣的程序雖然可以編譯通過(guò),但運(yùn)行時(shí)會(huì)異常終止。

注:linux系統(tǒng)時(shí)間如果轉(zhuǎn)換為 time_t 類(lèi)型,都是從1970-01-01 08:00:00 開(kāi)始計(jì)算

C語(yǔ)言時(shí)間函數(shù)time_t

1、time_t // 時(shí)間類(lèi)型(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

2、time函數(shù)使用示例

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

}

C語(yǔ)言的時(shí)間函數(shù)

C語(yǔ)言的建時(shí)間函數(shù)是 mktime(),原型在 time.h 里

調(diào)用有點(diǎn)繁。

下面,用我的程序輸入 年月日時(shí)分秒,調(diào)用mktime(), 就得 C語(yǔ)言 可直接使用的 時(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));

}

當(dāng)前題目:c語(yǔ)言取時(shí)間函數(shù) c語(yǔ)言獲取當(dāng)前時(shí)間的函數(shù)
網(wǎng)頁(yè)URL:http://muchs.cn/article10/dosccdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站策劃、建站公司虛擬主機(jī)、靜態(tài)網(wǎng)站品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化