time.h詳細紹介

14870 ワード

time.h詳細紹介
< ctime> (time.h)

                    。

一、Macro constants(マクロ定数)
  • CLOCKS_PER_SEC:ポタポタ音/秒、時間の単位
  • NULL:Nullポインタ
  • 二、types(タイプ)
  • clock_t:クロックタイプ、クロックの滴下数を表す基本データタイプ
  • size_t:符号なし整数
  • time_t:時間タイプ、時間
  • を表す
  • struct tm:カレンダー、時間
  • を含む時間構造
    Member
    Type
    Meaning
    Range
    tm_sec
    int
    seconds
    after the minute0-60*
    tm_min
    int
    minutes
    after the hour 0-59
    tm_hour
    int
    hours
    since midnight 0-23
    tm_mday
    int
    day
    of the month 1-31
    tm_mon
    int
    months
    since January 0-11
    tm_year
    int
    years
    since 1900
    tm_wday
    int
    days
    since Sunday 0-6
    tm_yday
    int
    days
    since January 1 0-365
    tm_isdst
    int
    Daylight
    Saving Time flag
    三、時間操作
    1、 clock_t clock (void);
    説明:特定の時間から消費される時間で、失敗した場合は-1を返します.
    /* clock example: frequency of primes */
    #include       /* printf */
    #include        /* clock_t, clock, CLOCKS_PER_SEC */
    #include        /* sqrt */
    
    int frequency_of_primes (int n) {
      int i,j;
      int freq=n-1;
      for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
      return freq;
    }
    
    int main ()
    {
      clock_t t;
      int f;
      t = clock();
      printf ("Calculating...
    "
    ); f = frequency_of_primes (99999); printf ("The number of primes lower than 100,000 is: %d
    "
    ,f); t = clock() - t; printf ("It took me %d clicks (%f seconds).
    "
    ,t,((float)t)/CLOCKS_PER_SEC); return 0; }

    2、 double difftime (time_t end, time_t beginning);
    説明:beginningからendまでの時間(end-beginning)(単位/秒)を計算します.
    /* difftime example */
    #include       /* printf */
    #include <time.h>       /* time_t, struct tm, difftime, time, mktime */
    
    int main ()
    {
      time_t now;
      struct tm newyear;
      double seconds;
    
      time(&now);  /* get current time; same as: now = time(NULL)  */
    
      newyear = *localtime(&now);
    
      newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;
      newyear.tm_mon = 0;  newyear.tm_mday = 1;
    
      seconds = difftime(now,mktime(&newyear));
    
      printf ("%.f seconds since new year in the current timezone.
    "
    , seconds); return 0; }

    3、 time_t mktime (struct tm * timeptr);
    説明:
  • は、timeptrポインタ記述の時間を返し、時間が記述されていない場合は−1を返す.
  • localtimeの逆変換
  • 構造メンバーtm_を無視wday and tm_yday;他のメンバーは有効範囲をタイムリーに超え、説明します.
  • /* mktime example: weekday calculator */
    #include       /* printf, scanf */
    #include <time.h>       /* time_t, struct tm, time, mktime */
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
      int year, month ,day;
      const char * weekday[] = { "Sunday", "Monday",
                                 "Tuesday", "Wednesday",
                                 "Thursday", "Friday", "Saturday"};
    
      /* prompt user for date */
      printf ("Enter year: "); fflush(stdout); scanf ("%d",&year);
      printf ("Enter month: "); fflush(stdout); scanf ("%d",&month);
      printf ("Enter day: "); fflush(stdout); scanf ("%d",&day);
    
      /* get current timeinfo and modify it to the user's choice */
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      timeinfo->tm_year = year - 1900;
      timeinfo->tm_mon = month - 1;
      timeinfo->tm_mday = day;
    
      /* call mktime: timeinfo->tm_wday will be set */
      mktime ( timeinfo );
    
      printf ("That day is a %s.
    "
    , weekday[timeinfo->tm_wday]); return 0; }

    4、 time_t time (time_t* timer);
    説明:
  • 現在時刻を取得します.
  • 時間ポインタがNULLでない場合、その指定された時間が返されます.
  • は時間を取得できず、-1に戻る.
  • が返す時間は、00:00 hours,Jan 1,1970 UTC
  • に基づいています.
    /* time example */
    #include       /* printf */
    #include <time.h>       /* time_t, struct tm, difftime, time, mktime */
    
    int main ()
    {
      time_t timer;
      struct tm y2k = {0};
      double seconds;
    
      y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
      y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
    
      time(&timer);  /* get current time; same as: timer = time(NULL)  */
    
      seconds = difftime(timer,mktime(&y2k));
    
      printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);
    
      return 0;
    }

    四、変換
    1、 char* asctime (const struct tm * timeptr);
    説明:
      timeptr     , C-     
    
         :Www Mmm dd hh:mm:ss yyyy(            )
    
          ,       
    
    /* asctime example */
    #include       /* printf */
    #include        /* time_t, struct tm, time, localtime, asctime */
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
    
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      printf ( "The current date/time is: %s", asctime (timeinfo) );
    
      return 0;
    }

    2、 char* ctime (const time_t * timer);
    説明:
      timer     , C-     
    
         :Www Mmm dd hh:mm:ss yyyy(            )
    
          ,       
    
     asctime(localtime(timer))  
    
    /* ctime example */
    #include       /* printf */
    #include        /* time_t, time, ctime */
    
    int main ()
    {
      time_t rawtime;
    
      time (&rawtime);
      printf ("The current local time is: %s", ctime (&rawtime));
    
      return 0;
    }

    3、 struct tm * gmtime (const time_t * timer);
    説明:
      timer  tm   
    
     time_t     UTC time
    
    /* gmtime example */
    #include       /* puts, printf */
    #include        /* time_t, struct tm, time, gmtime */
    
    #define MST (-7)
    #define UTC (0)
    #define CCT (+8)
    
    int main ()
    {
      time_t rawtime;
      struct tm * ptm;
    
      time ( &rawtime );
    
      ptm = gmtime ( &rawtime );
    
      puts ("Current time around the World:");
      printf ("Phoenix, AZ (U.S.) :  %2d:%02d
    "
    , (ptm->tm_hour+MST)%24, ptm->tm_min); printf ("Reykjavik (Iceland) : %2d:%02d
    "
    , (ptm->tm_hour+UTC)%24, ptm->tm_min); printf ("Beijing (China) : %2d:%02d
    "
    , (ptm->tm_hour+CCT)%24, ptm->tm_min); return 0; }

    4、 struct tm * localtime (const time_t * timer);
    説明:
      timer  tm   
    
     time_t      local time
    
    /* localtime example */
    #include       /* puts, printf */
    #include        /* time_t, struct tm, time, localtime */
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
    
      time (&rawtime);
      timeinfo = localtime (&rawtime);
      printf ("Current local time and date: %s", asctime(timeinfo));
    
      return 0;
    }

    5、· size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
    説明:
     timeptr    ,      ,   ptr 
    
     format  ,    timeptr ptr,  maxsize  。
    
    ptr:    ,  C-   
    
    maxsize:       ,   ptr      
    
    format:  
    
       :     ptr    (        ),     maxsize,  0
    
    /* strftime example */
    #include       /* puts */
    #include        /* time_t, struct tm, time, localtime, strftime */
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
      char buffer [80];
    
      time (&rawtime);
      timeinfo = localtime (&rawtime);
    
      strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);
      puts (buffer);
    
      return 0;
    }
    

    specifier
    Replaced by
    Example
    %a
    Abbreviated
    weekday name *
    %A
    Full weekday name *
    Thursday
    %b
    Abbreviated month name *
    Aug
    %B
    Full month name *
    August
    %c
    Date and time representation *
    Thu Aug 23 14:55:02 2001
    %C
    Year divided by 100 and truncated to integer (00-99)
    20
    %d
    Day of the month, zero-padded (01-31)
    23
    %D
    Short MM/DD/YY date, equivalent to %m/%d/%y
    08/23/01
    %e
    Day of the month, space-padded ( 1-31)
    23
    %F
    Short YYYY-MM-DD date, equivalent to %Y-%m-%d
    2001-08-23
    %g
    Week-based year, last two digits (00-99)
    01
    %G
    Week-based year
    2001
    %h
    Abbreviated month name * (same as %b)
    Aug