乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C語言time.h的使用

       細寒 2014-03-22

           以前經(jīng)常在程序中獲取系統(tǒng)時間,計算時間,但是每次都是到網(wǎng)上臨時找來一些資料對付過去,今天就索性整理一下。

          關(guān)于C語言time.h的一些應(yīng)用。

          clock_t clock(void);    //得到從進程啟動到此次函數(shù)調(diào)用的累計的時鐘滴答數(shù)。每秒包含CLOCKS_PER_SEC(time.h中定義的常量,一般為1000,貌似linux下為1000000)個時鐘滴答。時鐘滴答數(shù)用數(shù)據(jù)類型clock_t表示。clock_t類型一般是32位整數(shù)類型。

          time_t time(time_t* timer);    //得到從標準計時點(一般是1970年1月1日0時0秒)到當前時間的秒數(shù)。日歷時間用數(shù)據(jù)類型time_t表示。time_t類型實際上一般是32位或64位整數(shù)類型。

          分解時間同結(jié)構(gòu)體類型表示tm表示:

          struct tm

          {

              int tm_hour;    //時    0~23

              int tm_isdst;    //夏令時是否開啟    開啟(> 0),關(guān)閉(= 0),未知(< 0)

              int tm_mday; //日    0~31

              int tm_min;    //分    0~59

              int tm_mon;   //月    0~11

              int tm_sec;    //秒    0~60(60為天文學中定義的閏秒)

              int tm_wday;  //星期,從星期天計    0~6

              int tm_yday;   //本年經(jīng)過的天數(shù)    0~365

              int tm_year;    //從1900年起經(jīng)過的年數(shù)    

          };    

          struct tm* gmtime(const time_t* timer);      //從日歷時間time_t到分解時間tm的轉(zhuǎn)換。函數(shù)返回的是一個靜態(tài)分配的tm結(jié)構(gòu)存儲空間,該存儲空間被gmtime,localtime與ctime函數(shù)所共用. 這些函數(shù)的每一次調(diào)用會覆蓋這塊tm結(jié)構(gòu)存儲空間的內(nèi)容。

          struct tm* gmtime_r(const time_t* timer , struct tm*result);    //該函數(shù)是gmtime函數(shù)的線程安全版本

          struct tm* localtime(const time_t* timer);    //從日歷時間time_t到分解時間tm的轉(zhuǎn)換,即結(jié)果數(shù)據(jù)已經(jīng)調(diào)整到本地時區(qū)與夏令時。

          time_t mktime(struct tm* ptm);    //從分解時間tm到日歷時間time_t的轉(zhuǎn)換。

          time_t timegm(struct tm* brokentime);    //從分解時間tm(被視作UTC時間,不考慮本地時區(qū)設(shè)置)到日歷時間time_t的轉(zhuǎn)換。該函數(shù)較少被使用。        

          double difftime(time_t timer2, time_t timer1);    //比較兩個日歷時間,返回double類型的秒數(shù)差。似乎用處不大,time_t可以直接相減

         

          以下是幾個把日期數(shù)據(jù)按常用格式輸出的函數(shù):

          char *asctime(const struct tm* tmptr);    //把分解時間tm輸出到字符串,結(jié)果的格式為"Www Mmm dd hh:mm:ss yyyy",即“周幾 月份數(shù) 日數(shù) 小時數(shù):分鐘數(shù):秒鐘數(shù) 年份數(shù)”。函數(shù)返回的字符串為靜態(tài)分配,長度不大于26,與ctime函數(shù)共用。函數(shù)的每次調(diào)用將覆蓋該字符串內(nèi)容。

          char* ctime(const time_t* timer);    //把日歷時間time_t timer輸出到字符串,輸出格式與asctime函數(shù)一樣。

          size_t strftime(char* s, size_t n, const char* format, const struct tm* tptr);    //把分解時間tm轉(zhuǎn)換為自定義格式的字符串,類似于常見的字符串格式輸出函數(shù)sprintf。

          char * strptime(const char* buf, const char* format, struct tm* tptr);    //strftime的逆操作,把字符串按照自定義的格式轉(zhuǎn)換為分解時間tm。恩恩……這個函數(shù)還是比較有意思的。

          其中上面的自定義格式字符串類型有自己的格式命令符,相當多,請讀者自己查閱。

          下面是我寫的代碼:

          //取按下回車前后的系統(tǒng)時間,并計算時間差   

        1 #include<stdio.h>
        2 #include<time.h>
        3 int main()
        4 {
        5     time_t now1,now2,d,h,m,s,t;
        6     struct tm *tnow1,*tnow2;
        7     time(&now1);    tnow1=localtime(&now1);
        8     printf("%s\n",asctime(tnow1));
        9     getchar();//等待回車
       10     time(&now2);    tnow2=localtime(&now2);
       11     printf("%s\n",asctime(tnow2));
       12     t=now2-now1;    d=t/86400;
       13     t%=86400;   h=t/3600;
       14     t%=3600;    m=t/60; s=t%60;
       15     printf("Time Difference: %d day  %d hour  %d mintues  %d seconds\n",d,h,m,s);
       16     return 0;
       17 }

            運行結(jié)果:

          

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多