天天看點

time.h頭檔案函數 - 月不識己

time.h頭檔案中聲明的各種函數和資料結構

分類: c語言2010-04-10

17:45 157人閱讀 評論(0) 收藏 舉報

1.計時函數:clock_t clock( void ); 

       clock_t  在time.h檔案中定義為一個長整型:

                   #ifndef _CLOCK_T_DEFINED  

                      typedef long clock_t;  

                  #define _CLOCK_T_DEFINED  

                  #endif 

2.日期和時間的資料結構:

                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;

                         };

3.time_t資料類型

                     #ifndef  __TIME_T

                     #define  __TIME_T

                   typedef long time_t;

                      #endif

     time_t實際上是長整型,從一個時間點(1970年1月1日0時0分0秒)到那時的秒數,需要注意的是,它所表示的時間不能晚于2038年1月18日19時14分07秒。

4.日期和時間相關的函數

       time()函數來獲得月曆時間: time_t time(time_t * timer);  通過傳回值傳回現在的月曆時間,即從1970年 1月1日0時0分0秒到現在此時的秒數。如果參數為空(NUL),函數将隻通過傳回值傳回現在的月曆時間。

       gmtime()和localtime()獲得日期和時間:

       struct tm * gmtime(const time_t *timer);  

       struct tm * localtime(const time_t * timer); 

       固定的時間格式可用asctime()函數和ctime()函數将時間以固定的格式顯示出來:

       char *_Cdecl asctime (const struct tm *tblock);

       char *_Cdecl ctime (const time_t *time);

      自定義時間格式  :使用strftime()函數

       size_t strftime(  

               char *strDest,  

               size_t maxsize,  

                const char *format,  

                const struct tm *timeptr  

                ); 

       我們可以根據format指向字元串中格式指令把timeptr中儲存的時間資訊放在strDest指向的字元串中,最多向strDest中存放maxsize個字元。該函數傳回向strDest指向的字元串中放置的字元數。

       %a 星期幾的簡寫  

       %A 星期幾的全稱  

       %b 月分的簡寫  

       %B 月份的全稱  

       %c 标準的日期的時間串  

       %C 年份的後兩位數字  

       %d 十進制表示的每月的第幾天  

       %D 月/天/年  

       %e 在兩字元域中,十進制表示的每月的第幾天  

       %F 年-月-日  

       %g 年份的後兩位數字,使用基于周的年  

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

       %h 簡寫的月份名  

       %H 24小時制的小時  

       %I 12小時制的小時  

       %j 十進制表示的每年的第幾天  

       %m 十進制表示的月份  

       %M 十時制表示的分鐘數  

       %n 新行符  

       %p 本地的AM或PM的等價顯示  

       %r 12小時的時間  

       %R 顯示小時和分鐘:hh:mm  

       %S 十進制的秒數  

       %t 水準制表符  

       %T 顯示時分秒:hh:mm:ss  

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

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

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

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

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

       %x 标準的日期串  

       %X 标準的時間串  

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

       %Y 帶世紀部分的十進制年份  

       %z,%Z 時區名稱,如果不能得到時區名稱則傳回空字元。  

       %% 百分号 

       計算持續時間的長度:double difftime(time_t time1, time_t time0);  雖然該函數傳回的以秒計算的時間間隔是double類型的,但這并不說明該時間具有同double一樣的精确度,這是由它的參數覺得的(time_t是以秒為機關計算的)。

       分解時間轉化為月曆時間:time_t mktime(struct tm * timeptr);  其傳回值就是轉化後的月曆時間。

<ctime>

Visual Studio 2010

其他版本

此主題尚未評級 評價此主題

Defines the macros traditionally defined in the Standard C Library header <time.h>.

#if <TRADITIONAL C HEADERS>
   #include <time.h>
namespace std {
   using ::asctime; 
   using ::clock; 
   using ::clock_t; 
   using ::ctime;
   using ::difftime; 
   using ::gmtime; 
   using ::localtime;
   using ::mktime; 
   using ::size_t;
   using ::strftime; 
   using ::time;
   using ::time_t; 
   using ::tm;
}
#endif      
time.h頭檔案函數 - 月不識己