<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. C語言

    c語言中time函數的用法

    時間:2025-05-15 23:49:27 C語言 我要投稿
    • 相關推薦

    c語言中time函數的用法

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

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

      頭文件time.h

      @函數名稱: localtime

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

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

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

      [cpp] view plain copy

      01.struct 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

      02.#include

      03.#include

      04.int 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()得到

      所屬文件:

      [cpp] view plain copy

      01.#include

      02.#include

      03.#include

      04. int main() {

      05. struct tm t;

      06. char str[80];

      07. t.tm_sec=1;

      08. t.tm_min=3;

      09. t.tm_hour=7;

      10. t.tm_mday=22;

      11. t.tm_mon=11;

      12. t.tm_year=56;

      13. t.tm_wday=4;

      14. t.tm_yday=0;

      15. t.tm_isdst=0;

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

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

      18. return 0;

      19.}

      @函數名稱: ctime

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

      函數功能: 得到日歷時間

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

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

      所屬文件:

      [cpp] view plain copy

      01.#include

      02.#include

      03.int 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函數獲得

      所屬文件:

      [cpp] view plain copy

      01.#include

      02.#include

      03.#include

      04.#include

      05.int 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()得到的時間信息

      所屬文件:

      [cpp] view plain copy

      01.#include

      02.#include

      03.#include

      04.#include

      05.char *tzstr="TZ=PST8PDT";

      06.int 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類型

      所屬文件:

      [cpp] view plain copy

      01.#include

      02.#include

      03.#include

      04.int 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環境下無用途

      函數返回:

      參數說明:

      所屬文件:

      [cpp] view plain copy

      01.#include

      02.#include

      03.#include

      04.int 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.}


    【c語言中time函數的用法】相關文章:

    C語言中strpbr()函數的用法07-25

    C語言中函數的區分08-30

    c語言中bit的用法11-01

    C語言中assert用法09-22

    C語言中的assert用法10-10

    c語言中default的用法05-26

    c語言中多個if的用法11-12

    c語言中getch的用法06-01

    c語言中邏輯或的用法05-23

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人