c和c的時(shí)間函數(shù)和隨機(jī)函數(shù)總結(jié)與范例_第1頁
c和c的時(shí)間函數(shù)和隨機(jī)函數(shù)總結(jié)與范例_第2頁
c和c的時(shí)間函數(shù)和隨機(jī)函數(shù)總結(jié)與范例_第3頁
c和c的時(shí)間函數(shù)和隨機(jī)函數(shù)總結(jié)與范例_第4頁
c和c的時(shí)間函數(shù)和隨機(jī)函數(shù)總結(jié)與范例_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、c/c+中時(shí)間函數(shù)和隨機(jī)函數(shù)的總結(jié)*C+的隨機(jī)函數(shù)和時(shí)間函數(shù)*隨機(jī)函數(shù)一、C+中不能使用random()函數(shù) random函數(shù)不是ANSI C標(biāo)準(zhǔn),不能在gcc,vc等編譯器下編譯通過。 可改用C+下的rand函數(shù)來實(shí)現(xiàn)。 1、C+標(biāo)準(zhǔn)函數(shù)庫提供一隨機(jī)數(shù)生成器rand,返回0RAND_MAX之間均勻分布的偽隨機(jī)整數(shù)。 RAND_MAX必須至少為32767。rand()函數(shù)不接受參數(shù),默認(rèn)以1為種子(即起始值)。 隨機(jī)數(shù)生成器總是以相同的種子開始,所以形成的偽隨機(jī)數(shù)列也相同,失去了隨機(jī)意義。(但這樣便于程序調(diào)試) 2、C+中另一函數(shù)srand(),可以指定不同的數(shù)(無符號(hào)整數(shù)變?cè)榉N子。但是如

2、果種子相同,偽隨機(jī)數(shù)列也相同。一個(gè)辦法是讓用戶輸入種子,但是仍然不理想。 3、 比較理想的是用變化的數(shù),比如時(shí)間來作為隨機(jī)數(shù)生成器的種子。 time的值每時(shí)每刻都不同。所以種子不同,所以,產(chǎn)生的隨機(jī)數(shù)也不同。 / C+隨機(jī)函數(shù)(VC program) #include <stdio.h> #include <iostream> #include <time.h> using namespace std; #define MAX 100 int main(int argc, char* argv) srand( (unsigned)time( NULL ) )

3、;/srand()函數(shù)產(chǎn)生一個(gè)以當(dāng)前時(shí)間開始的隨機(jī)種子.應(yīng)該放在for等循環(huán)語句前面 不然要很長(zhǎng)時(shí)間等待for (int i=0;i<10;i+) cout<<rand()%MAX<<endl;/MAX為最大值,其隨機(jī)域?yàn)?MAX-1 return 0; 二、rand()的用法 rand()不需要參數(shù),它會(huì)返回一個(gè)從0到最大隨機(jī)數(shù)的任意整數(shù),最大隨機(jī)數(shù)的大小通常是固定的一個(gè)大整數(shù)。 這樣,如果你要產(chǎn)生010的10個(gè)整數(shù),可以表達(dá)為: int N = rand() % 11; 這樣,N的值就是一個(gè)010的隨機(jī)數(shù),如果要產(chǎn)生110,則是這樣: int N = 1 +

4、rand() % 11; 總結(jié)來說,可以表示為: a + rand() % n 其中的a是起始值,n是整數(shù)的范圍。 a + rand() % (b-a+1) 就表示之間的一個(gè)隨機(jī)數(shù)若要01的小數(shù),則可以先取得010的整數(shù),然后均除以10即可得到隨機(jī)到十分位的10個(gè)隨機(jī)小數(shù),若要得到隨機(jī)到百分位的隨機(jī)小數(shù),則需要先得到0100的10個(gè)整數(shù),然后均除以100,其它情況依此類推。 通常rand()產(chǎn)生的隨機(jī)數(shù)在每次運(yùn)行的時(shí)候都是與上一次相同的,這是有意這樣設(shè)計(jì)的,是為了便于程序的調(diào)試。若要產(chǎn)生每次不同的隨機(jī)數(shù),可以使用srand( seed )函數(shù)進(jìn)行隨機(jī)化,隨著seed的不同,就能夠產(chǎn)生不同的隨機(jī)

5、數(shù)。 如大家所說,還可以包含time.h頭文件,然后使用srand(time(0)來使用當(dāng)前時(shí)間使隨機(jī)數(shù)發(fā)生器隨機(jī)化,這樣就可以保證每?jī)纱芜\(yùn)行時(shí)可以得到不同的隨機(jī)數(shù)序列(只要兩次運(yùn)行的間隔超過1秒)。 函數(shù)名: random ?功 能: 隨機(jī)數(shù)發(fā)生器 用 法: int random(int num); 程序例: #include <stdlib.h> #include <stdio.h> #include <time.h> /* prints a random number in the range 0 to 99 */ int main(void) ran

6、domize(); printf("Random number in the 0-99 range: %dn", random (100); return 0; 函數(shù)名: randomize 功 能: 初始化隨機(jī)數(shù)發(fā)生器 用 法: void randomize(void); 程序例: #include <stdlib.h> #include <stdio.h> #include <time.h> int main(void) int i; randomize(); printf("Ten random numbers from

7、0 to 99nn"); for(i=0; i<10; i+) printf("%dn", rand() % 100); return 0; rand(產(chǎn)生隨機(jī)數(shù)) 相關(guān)函數(shù) srand 表頭文件 #include<stdlib.h> 定義函數(shù) int rand(void) 函數(shù)說明 rand()會(huì)返回一隨機(jī)數(shù)值,范圍在0至RAND_MAX 間。在調(diào)用此函數(shù)產(chǎn)生隨機(jī)數(shù)前,必須先利用srand()設(shè)好隨機(jī)數(shù)種子,如果未設(shè)隨機(jī)數(shù)種子,rand()在調(diào)用時(shí)會(huì)自動(dòng)設(shè)隨機(jī)數(shù)種子為1。關(guān)于隨機(jī)數(shù)種子請(qǐng)參考srand()。 返回值 返回0至RAND_MAX之間

8、的隨機(jī)數(shù)值,RAND_MAX定義在stdlib.h,其值為2147483647。范例 /* 產(chǎn)生介于1 到10 間的隨機(jī)數(shù)值,此范例未設(shè)隨機(jī)數(shù)種子,完整的隨機(jī)數(shù)產(chǎn)生請(qǐng)參考 srand()*/ #include<stdlib.h> main() int i,j; for(i=0;i<10;i+) j=1+(int)(10.0*rand()/(RAND_MAX+1.0); printf("%d ",j); 執(zhí)行 9 4 8 8 10 2 4 8 3 6 9 4 8 8 10 2 4 8 3 6 srand(設(shè)置隨機(jī)數(shù)種子) 相關(guān)函數(shù) rand 表頭文件 #in

9、clude<stdlib.h> 定義函數(shù) void srand (unsigned int seed); 函數(shù)說明 srand()用來設(shè)置rand()產(chǎn)生隨機(jī)數(shù)時(shí)的隨機(jī)數(shù)種子。參數(shù)seed必須是個(gè)整數(shù),通??梢岳胓eypid()或time(0)的返回值來當(dāng)做seed。如果每次seed都設(shè)相同值,rand()所產(chǎn)生的隨機(jī)數(shù)值每次就會(huì)一樣。 返回值 返回0至RAND_MAX之間的隨機(jī)數(shù)值,RAND_MAX定義在stdlib.h,其值為2147483647。 范例 /* 產(chǎn)生介于1 到10 間的隨機(jī)數(shù)值,此范例未設(shè)隨機(jī)數(shù)種子,完整的隨機(jī)數(shù)產(chǎn)生請(qǐng)參考 srand()*/ #include

10、<stdlib.h> main() int i,j; for(i=0;i<10;i+) j=1+(int)(10.0*rand()/(RAND_MAX+1.0); printf("%d ",j); 返回值 范例 /* 產(chǎn)生介于1 到10 間的隨機(jī)數(shù)值,此范例與執(zhí)行結(jié)果可與rand()參照*/ #include<time.h> #include<stdlib.h> main() int i,j; srand(int)time(0); for(i=0;i<10;i+) j=1+(int)(10.0*rand()/(RAND_MAX

11、+1.0); printf(" %d ",j); 執(zhí)行 5 8 8 8 10 2 10 8 9 9 2 9 7 4 10 3 2 10 8 7在指定的兩個(gè)數(shù)之間產(chǎn)生隨機(jī)數(shù)#include <stdio.h>#include <stdlib.h>#include <time.h>/返回a和b之間的隨機(jī)數(shù),用時(shí)間做種子 int rand_between_a_b(int a,int b) int c ,re,temp; time_t t; if(a < b) temp = a; a = b; b = temp; c = a - b; sr

12、and(unsigned) time(&t); /使用時(shí)間做種子數(shù) re = b + (rand() % c); return re;int main() int re; re = rand_between_a_b(35,98); printf("%dn",re); getch(); return 0;注:srand()配置隨機(jī)數(shù)種子。srand(unsigned)time(NULL);產(chǎn)生偽隨機(jī)數(shù)數(shù)列rand()產(chǎn)生隨機(jī)數(shù)C庫里的Rand函數(shù)用的是偽隨機(jī)數(shù)生成算法,你不種一個(gè)新種子進(jìn)去,生成的隨機(jī)數(shù)序列都是一樣的。偽隨機(jī)的意思就是不隨機(jī),種子相同的情況下,等次數(shù)地

13、獲得的“隨機(jī)”結(jié)果都是一樣的(函數(shù)嘛.)為了讓他真隨機(jī),就得找個(gè)現(xiàn)成的隨機(jī)量作初值一般選時(shí)間作為srand的參數(shù),也就是seed時(shí)間函數(shù)一、獲取日歷時(shí)間time_t是定義在time.h中的一個(gè)類型,表示一個(gè)日歷時(shí)間,也就是從1970年1月1日0時(shí)0分0秒到此時(shí)的秒數(shù),原型是: typedef long time_t; /* time value */可以看出time_t其實(shí)是一個(gè)長(zhǎng)整型,由于長(zhǎng)整型能表示的數(shù)值有限,因此它能表示的最遲時(shí)間是2038年1月18日19時(shí)14分07秒。函數(shù)time可以獲取當(dāng)前日歷時(shí)間時(shí)間,time的定義: time_t time(time_t *)#include &

14、lt;iostream>#include <time.h>using namespace std;int main(void) time_t nowtime; nowtime = time(NULL); /獲取當(dāng)前時(shí)間 cout << nowtime << endl; return 0;輸出結(jié)果:1268575163 二、獲取本地時(shí)間 time_t只是一個(gè)長(zhǎng)整型,不符合我們的使用習(xí)慣,需要轉(zhuǎn)換成本地時(shí)間,就要用到tm結(jié)構(gòu),time.h中結(jié)構(gòu)tm的原型是:struct tm int tm_sec; /* seconds after the minute

15、- 0,59 */ 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

16、January 1 - 0,365 */ int tm_isdst; /* daylight savings time flag */ ;可以看出,這個(gè)機(jī)構(gòu)定義了年、月、日、時(shí)、分、秒、星期、當(dāng)年中的某一天、夏令時(shí)??梢杂眠@個(gè)結(jié)構(gòu)很方便的顯示時(shí)間。用localtime獲取當(dāng)前系統(tǒng)時(shí)間,該函數(shù)將一個(gè)time_t時(shí)間轉(zhuǎn)換成tm結(jié)構(gòu)表示的時(shí)間,函數(shù)原型: struct tm * localtime(const time_t *)使用gmtime函數(shù)獲取格林尼治時(shí)間,函數(shù)原型: struct tm * gmtime(const time_t *) 為了方便顯示時(shí)間,定義了一個(gè)函數(shù)void dsptim

17、e(const struct tm *);#include <iostream>#include <time.h>using namespace std;void dsptime(const struct tm *); /輸出時(shí)間。int main(void) time_t nowtime; nowtime = time(NULL); /獲取日歷時(shí)間 cout << nowtime << endl; /輸出nowtime struct tm *local,*gm; local=localtime(&nowtime); /獲取當(dāng)前系統(tǒng)時(shí)間

18、dsptime(local); gm=gmtime(&nowtime); /獲取格林尼治時(shí)間 dsptime(gm); return 0;void dsptime(const struct tm * ptm) char *pxq="日","一","二","三","四","五","六" cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1

19、 << "月" << ptm->tm_mday << "日 " ; cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ; cout << " 星期" <<pxqptm->tm_wday << " 當(dāng)年的第

20、" << ptm->tm_yday << "天 " << endl;輸出結(jié)果:12685751632010年3月14日 21:59:23 星期日 當(dāng)年的第72天2010年3月14日 13:59:23 星期日 當(dāng)年的第72天 三、輸出時(shí)間C/C+語言提供了用字符串格式表示時(shí)間的函數(shù)。char * asctime(const struct tm *)char * ctime(const time_t *)這兩個(gè)函數(shù)返回值都是一個(gè)表示時(shí)間的字符串,區(qū)別在于傳入的參數(shù)不同。#include <iostream>#inc

21、lude <time.h>using namespace std;int main(void) time_t nowtime; nowtime = time(NULL); /獲取日歷時(shí)間 cout << nowtime << endl; /輸出nowtime struct tm *local; local=localtime(&nowtime); /獲取當(dāng)前系統(tǒng)時(shí)間 cout << asctime(local) ; cout << ctime(&nowtime) ; return 0;輸出結(jié)果:1268575163Su

22、n Mar 14 13:59:23 2010Sun Mar 14 21:59:23 2010 四、計(jì)算時(shí)間間隔可以通過difftime來計(jì)算兩個(gè)時(shí)間的間隔,可以精確到秒,函數(shù)原型: double difftime(time_t, time_t)要想精確到毫秒,就要使用clock函數(shù)了,函數(shù)原型: clock_t clock(void)從定義可以看出clock返回一個(gè)clock_t類型,這個(gè)類型也定義在time.h中,原型是: typedef long clock_tclock_t也是一個(gè)長(zhǎng)整型,表示的是從程序開始運(yùn)行到執(zhí)行clock函數(shù)時(shí)所經(jīng)過的cpu時(shí)鐘計(jì)時(shí)單元數(shù)。輸出結(jié)果:請(qǐng)按任意鍵繼續(xù).

23、 . .時(shí)間差:3Clock時(shí)間差:3094 在time.h中定義了一個(gè)CLOCKS_PER_SEC /* Clock ticks macro - ANSI version */ #define CLOCKS_PER_SEC 1000表示1秒鐘內(nèi)有多少個(gè)時(shí)鐘計(jì)時(shí)單元,在標(biāo)準(zhǔn)C/C+中,最小的計(jì)時(shí)單位是1毫秒。五、自定義時(shí)間格式C/C+在time.h中提供了一個(gè)自定義時(shí)間格式的函數(shù)strftime,函數(shù)原型: size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);參

24、數(shù)說明: char *strDest:用來存放格式化后的字符串緩存, size_t maxsize:指定最多可以輸出的字符數(shù), const char *format:格式化字符串, const struct tm *timeptr:要轉(zhuǎn)換的時(shí)間。 可使用的格式化字符串:%a 星期幾的簡(jiǎn)寫 %A 星期幾的全稱 %b 月分的簡(jiǎn)寫 %B 月份的全稱 %c 標(biāo)準(zhǔn)的日期的時(shí)間串 %C 年份的后兩位數(shù)字 %d 十進(jìn)制表示的每月的第幾天 %D 月/天/年 %e 在兩字符域中,十進(jìn)制表示的每月的第幾天 %F 年-月-日 %g 年份的后兩位數(shù)字,使用基于周的年 %G 年分,使用基于周的年 %h 簡(jiǎn)寫的月份名 %

25、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í)間串

26、 %y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)%Y 帶世紀(jì)部分的十進(jìn)制年份 %z,%Z 時(shí)區(qū)名稱,如果不能得到時(shí)區(qū)名稱則返回空字符。% 百分號(hào)#include <iostream>#include <time.h>using namespace std;int main(void) time_t nowtime; nowtime = time(NULL); /獲取日歷時(shí)間 cout << nowtime << endl; /輸出nowtime struct tm *local; local=localtime(&nowtime); /獲取當(dāng)

27、前系統(tǒng)時(shí)間 char buf80; strftime(buf,80,"格式化輸出:%Y-%m-%d %H:%M:%S",local); cout << buf << endl; return 0;輸出結(jié)果:1268575163格式化輸出:2010-03-14 21:59:23*C語言的時(shí)間函數(shù)*Linux下c語言編程的時(shí)間函數(shù)詳解默認(rèn)分類 2010-03-12 10:41:35 閱讀448 /* Linux時(shí)間函數(shù) */asctime(將時(shí)間和日期以字符串格式表示); =>傳入U(xiǎn)TC(struct tm)tmp,返回char*。ctime(將時(shí)

28、間和日期以字符串格式表示); =>傳入(time_t)arg,返回char*。gettimeofday(取得目前的時(shí)間); =>傳入(time_t)arg,返回tv,tz結(jié)構(gòu)體傳入時(shí)間,時(shí)區(qū)信息。gmtime(取得目前時(shí)間和日期); =>傳入(time_t)arg,返回UTC(struct tm)tmp。localtime(取得當(dāng)?shù)啬壳皶r(shí)間和日期); =>傳入time_t,返回當(dāng)?shù)?struct tm)tmp。mktime(將時(shí)間結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成經(jīng)過的秒數(shù)); =>把(struct tm)tmp轉(zhuǎn)換為UTC(time_t)arg。settimeofday(設(shè)置目前時(shí)

29、間); =>通過tv,tz結(jié)構(gòu)體傳入時(shí)間,時(shí)區(qū)信息。time(取得目前的時(shí)間); =>非空參數(shù)(或返回值)接收(time_t)arg。×××注1:char*是字符串時(shí)間格式。如:Sat Oct 28 02:10:06 2000。×××注2:time_t是time()的返回值類型,(time_t)arg指從1970年到所指時(shí)間的秒數(shù)。×××注3:struct tm為真實(shí)世界的表示時(shí)間方式,(struct tm)tmp是指向tm的時(shí)間。×××注4:UTC,指標(biāo)準(zhǔn)時(shí)間。

30、簡(jiǎn)單的想,就是0時(shí)區(qū)的時(shí)間標(biāo)準(zhǔn)。以下是各個(gè)函數(shù)的詳解:_ asctime(將時(shí)間和日期以字符串格式表示)相關(guān)函數(shù) time,ctime,gmtime,localtime表頭文件 #include<time.h>定義函數(shù) char * asctime(const struct tm * timeptr);函數(shù)說明 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 1993n”返回值 若再調(diào)用相關(guān)的時(shí)間日期函數(shù),此字符串可

31、能會(huì)被破壞。此函數(shù)與ctime不同處在于傳入的參數(shù)是不同的結(jié)構(gòu)。附加說明 返回一字符串表示目前當(dāng)?shù)氐臅r(shí)間日期。范例#include <time.h>int main()time_t timep;time (&timep);printf(“%s”,asctime(gmtime(&timep);return 0;執(zhí)行Sat Oct 28 02:10:06 2000#_ ctime(將時(shí)間和日期以字符串格式表示)相關(guān)函數(shù) time,asctime,gmtime,localtime表頭文件 #include<time.h>定義函數(shù) char *ctime(con

32、st time_t *timep);函數(shù)說明 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 1993n”。若再調(diào)用相關(guān)的時(shí)間日期函數(shù),此字符串可能會(huì)被破壞。返回值 返回一字符串表示目前當(dāng)?shù)氐臅r(shí)間日期。范例#include<time.h>int main()time_t timep;time (&timep);printf(“%s”,ctime(&timep);return 0;執(zhí)行Sat Oct

33、 28 10 : 12 : 05 2000#_ gettimeofday(取得目前的時(shí)間)相關(guān)函數(shù) time,ctime,ftime,settimeofday表頭文件 #include <sys/time.h>& #include <unistd.h>定義函數(shù) int gettimeofday ( struct timeval * tv , struct timezone * tz )函數(shù)說明 gettimeofday()會(huì)把目前的時(shí)間有tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r(shí)區(qū)的信息則放到tz所指的結(jié)構(gòu)中。timeval 結(jié)構(gòu)定義為:struct timevallong t

34、v_sec; /*秒*/long tv_usec; /*微秒*/;timezone 結(jié)構(gòu)定義為:struct timezoneint 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 /*美國*/DST_AUST /*澳洲*/DST_WET /*西歐*/DST_MET /*中歐*/DST_EET /*東歐*/DST_CAN /*加拿大*/DST_GB /*

35、大不列顛*/DST_RUM /*羅馬尼亞*/DST_TUR /*土耳其*/DST_AUSTALT /*澳洲(1986年以后)*/返回值 成功則返回0,失敗返回1,錯(cuò)誤代碼存于errno。附加說明EFAULT指針tv和tz所指的內(nèi)存空間超出存取權(quán)限。范例#include<sys/time.h>#include<unistd.h>int main()struct timeval tv;struct timezone tz;gettimeofday (&tv , &tz);printf(“tv_sec; %dn”, tv,.tv_sec) ;printf(“t

36、v_usec; %dn”,tv.tv_usec);printf(“tz_minuteswest; %dn”, tz.tz_minuteswest);printf(“tz_dsttime, %dn”,tz.tz_dsttime);return 0;執(zhí)行tv_sec: 974857339tv_usec:136996tz_minuteswest:-540tz_dsttime:0#_ gmtime(取得目前時(shí)間和日期)相關(guān)函數(shù) time,asctime,ctime,localtime表頭文件 #include<time.h>定義函數(shù) struct tm*gmtime(const time_

37、t*timep);函數(shù)說明 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-59int tm_hour 從午夜算起的時(shí)數(shù),范圍為0-23int tm_md

38、ay 目前月份的日數(shù),范圍01-31int tm_mon 代表目前月份,從一月算起,范圍從0-11int tm_year 從1900 年算起至今的年數(shù)int tm_wday 一星期的日數(shù),從星期一算起,范圍為0-6int tm_yday 從今年1月1日算起至今的天數(shù),范圍為0-365int tm_isdst 日光節(jié)約時(shí)間的旗標(biāo)此函數(shù)返回的時(shí)間日期未經(jīng)時(shí)區(qū)轉(zhuǎn)換,而是UTC時(shí)間。返回值 返回結(jié)構(gòu)tm代表目前UTC 時(shí)間范例 #include <time.h>int main()char *wday="Sun","Mon","Tue&qu

39、ot;,"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;%dn”, wdayp->tm_wday, p->tm_hour, p->tm_min, p->tm_sec);return 0;執(zhí)行2000

40、/10/28 Sat 8:15:38#_ localtime(取得當(dāng)?shù)啬壳皶r(shí)間和日期)相關(guān)函數(shù) time, asctime, ctime, gmtime表頭文件 #include<time.h>定義函數(shù) struct tm *localtime(const time_t * timep);函數(shù)說明 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í)間。范例#include<time.h>int 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:%dn”, wdayp->tm_wday,p->tm_hour, p->

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論