time.h 详细介绍
time.h 詳細介紹
< ctime> (time.h)包含獲得和使用日期和時間信息的函數的定義。一、Macro constants(宏常量)
- CLOCKS_PER_SEC:滴答聲/秒,時間的單位
- NULL:空指針
二、types(類型)
clock_t:時鐘類型,表示時鐘滴答數的基本數據類型
size_t:無符號整型
time_t:時間類型,表示時間
struct tm:時間結構,包含日歷、時間
| 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 <stdio.h> /* printf */ #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ #include <math.h> /* 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...\n");f = frequency_of_primes (99999);printf ("The number of primes lower than 100,000 is: %d\n",f);t = clock() - t;printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);return 0; }2、double difftime (time_t end, time_t beginning);
描述:計算從beginning到end的時間(end-beginning)(單位/秒)。
/* difftime example */ #include <stdio.h> /* 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.\n", seconds);return 0; }3、time_t mktime (struct tm * timeptr);
描述:
返回timeptr指針描述的時間,如果時間未描述,返回-1。
localtime的逆變換
忽略結構成員tm_wday and tm_yday;其他成員及時超出有效范圍,也將解釋。
4、time_t time (time_t* timer);
描述:
獲取當前時間。
如果時間指針不為NULL,將返回其指向的時間。
不能取得時間,返回-1.
返回的時間基于: 00:00 hours, Jan 1, 1970 UTC
四、轉換
1、char* asctime (const struct tm * timeptr);
描述:
返回timeptr指針的時間,用C-字符串描述返回類型為:Www Mmm dd hh:mm:ss yyyy(星期 月 日 時分秒 年)輸出在新一行,以空字符串結束 /* asctime example */ #include <stdio.h> /* printf */ #include <time.h> /* 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 <stdio.h> /* printf */ #include <time.h> /* 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 <stdio.h> /* puts, printf */ #include <time.h> /* 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\n", (ptm->tm_hour+MST)%24, ptm->tm_min);printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);printf ("Beijing (China) : %2d:%02d\n", (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 <stdio.h> /* puts, printf */ #include <time.h> /* 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 <stdio.h> /* puts */ #include <time.h> /* 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; }| %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 |
總結
以上是生活随笔為你收集整理的time.h 详细介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于运算符operator重载出错解决方
- 下一篇: stdio.h: C++输入输出操作