商務英語計算機英語

c語言中time函數的用法

本文已影響 2.89W人 

c語言中time函數的用法的用法你知道嗎?下面小編就跟你們詳細介紹下c語言中time函數的用法的用法,希望對你們有用。

ing-bottom: 56.25%;">c語言中time函數的用法

  c語言中time函數的用法的用法如下:

文件time.h

@函數名稱: localtime

函數原型: struct tm *localtime(const time_t *timer)

函數功能: 返回一個以tm結構表達的機器時間信息

函數返回: 以tm結構表達的時間,結構tm定義如下:

[cpp] view plain copy

ct tm{

02. int tm_sec;

03. int tm_min;

04. int tm_hour;

05. int tm_mday;

06. int tm_mon;

07. int tm_year;

08. int tm_wday;

09. int tm_yday;

10. int tm_isdst;

11. };

參數說明: timer-使用time()函數獲得的機器時間

[cpp] view plain copy

01.#include <time.h>

02.#include <stdio.h>

03.#include <dos.h>

main() {

05. time_t timer;

06. struct tm *tblock;

07. timer=time(NULL);

08. tblock=localtime(&timer);

09. printf("Local time is: %s",asctime(tblock));

10. return 0;

11.}

@函數名稱: asctime

函數原型: char* asctime(struct tm * ptr)

函數功能: 得到機器時間(日期時間轉換爲ASCII碼)

函數返回: 返回的時間字符串格式爲:星期,月,日,小時:分:秒,年

參數說明: 結構指針ptr應通過函數localtime()和gmtime()得到

所屬文件: <time.h>

[cpp] view plain copy

01.#include <stdio.h>

02.#include <string.h>

03.#include <time.h>

04. int main() {

05. struct tm t;

06. char str[80];

07. _sec=1;

08. _min=3;

09. _hour=7;

10. _mday=22;

11. _mon=11;

12. _year=56;

13. _wday=4;

14. _yday=0;

15. _isdst=0;

16. strcpy(str,asctime(&t));

17. printf("%s",str);

18. return 0;

19.}

@函數名稱: ctime

函數原型: char *ctime(long time)

函數功能: 得到日曆時間

函數返回: 返回字符串格式:星期,月,日,小時:分:秒,年

參數說明: time-該參數應由函數time獲得

所屬文件: <time.h>

[cpp] view plain copy

01.#include <stdio.h>

02.#include <time.h>

main() {

04. time_t t;

05. time(&t);

06. printf("Today's date and time: %s",ctime(&t));

07. return 0;

08.}

@函數名稱: difftime

函數原型: double difftime(time_t time2, time_t time1)

函數功能: 得到兩次機器時間差,單位爲秒

函數返回: 時間差,單位爲秒

參數說明: time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得

所屬文件: <time.h>

[cpp] view plain copy

01.#include <time.h>

02.#include <stdio.h>

03.#include <dos.h>

04.#include <conio.h>

main() {

06. time_t first, second;

07. clrscr();

08. first=time(NULL);

09. delay(2000);

10. second=time(NULL);

11. printf("The difference is: %f seconds",difftime(second,first));

12. getch();

13. return 0;

14.}

@函數名稱: gmtime

函數原型: struct tm *gmtime(time_t *time)

函數功能: 得到以結構tm表示的時間信息

函數返回: 以結構tm表示的時間信息指針

參數說明: time-用函數time()得到的時間信息

所屬文件: <time.h>

[cpp] view plain copy

01.#include <stdio.h>

02.#include <stdlib.h>

03.#include <time.h>

04.#include <dos.h>

*tzstr="TZ=PST8PDT";

main() {

07. time_t t;

08. struct tm *gmt, *area;

09. putenv(tzstr);

10. tzset();

11. t=time(NULL);

12. area=localtime(&t);

13. printf("Local time is:%s", asctime(area));

14. gmt=gmtime(&t);

15. printf("GMT is:%s", asctime(gmt));

16. return 0;

17.}

@函數名稱: time

函數原型: time_t time(time_t *timer)

函數功能: 得到機器的日曆時間或者設置日曆時間

函數返回: 機器日曆時間

參數說明: timer=NULL時得到機器日曆時間,timer=時間數值時,用於設置日曆時間,time_t是一個long類型

所屬文件: <time.h>

[cpp] view plain copy

01.#include <time.h>

02.#include <stdio.h>

03.#include <dos.h>

main() {

05. time_t t;

06. t=time();

07. printf("The number of seconds since January 1,1970 is %ld",t);

08. return 0;

09.}

@函數名稱: tzset

函數原型: void tzset(void)

函數功能: UNIX兼容函數,用於得到時區,在DOS環境下無用途

函數返回:

參數說明:

所屬文件: <time.h>

[cpp] view plain copy

01.#include <time.h>

02.#include <stdlib.h>

03.#include <stdio.h>

main() {

05. time_t td;

06. putenv("TZ=PST8PDT");

07. tzset();

08. time(&td);

09. printf("Current time=%s",asctime(localtime(&td)));

10. return 0;

11.}

猜你喜歡

熱點閱讀

最新文章