標籤:
Linux下time函數都在time.h標頭檔中。
1、標頭檔
和時間有關的標頭檔有以下幾個:
time.h
sys/time.h
sys/times.h
sys/timeb.h
sys/timex.h
time.h是C標準庫的標頭檔,其餘sys開頭的都是Linux系統自己的標頭檔。
/usr/include/time.h定義了常用的time函數。
到/usr/include/sys目錄下查看這幾個檔案:
sys/time.h定義了timezone結構體和Linux系統的時間函數。
sys/times.h定義了進程使用CPU時間的結構體tms。
sys/timeb.h定義了ftime函數的傳回值的結構體timeb。
sys/timex.h定義了關於時鐘調整演算法的結構體timex。
2、常用函數和結構體
time函數原型(time.h中):
time_t time(time_t *calptr);
參數:
time_t類型變數的指標。
傳回值:
time_t類型相當於一個long,time用於取Epoch記年以來到現在經過的秒數(系統目前時間),Epoch記年從1970年1月1日開始。把取到的時間存在指標指向的變數中。
localtime函數原型(time.h中):
struct tm *localtime(const time_t *calptr);
參數:
time_t類型變數的指標。
傳回值:
指向tm結構體的指標類型。
作用是將time_t的值轉換為tm結構體。然後可以列印輸出。
tm結構體(time.h中):
/* Used by other time functions. */struct tm{ int tm_sec;/* Seconds.[0-60] (1 leap second) */ int tm_min;/* Minutes.[0-59] */ int tm_hour;/* Hours.[0-23] */ int tm_mday;/* Day.[1-31] */ int tm_mon;/* Month.[0-11] */ int tm_year;/* Year- 1900. */ int tm_wday;/* Day of week.[0-6] */ int tm_yday;/* Days in year.[0-365]*/ int tm_isdst;/* DST.[-1/0/1]*/#ifdef__USE_BSD long int tm_gmtoff;/* Seconds east of UTC. */ __const char *tm_zone;/* Timezone abbreviation. */#else long int __tm_gmtoff;/* Seconds east of UTC. */ __const char *__tm_zone;/* Timezone abbreviation. */#endif};
ftime函數原型(timeb.h):
int ftime(struct timeb *tp);
參數:
指向timeb結構體變數的指標。
傳回值:
將當前系統時間存入timeb結構體中,包括了秒和毫秒。作用就是能擷取目前時間精確到毫秒。
timeb結構體(sys/timeb.h):
/* Structure returned by the `ftime‘ function. */struct timeb { time_t time;/* Seconds since epoch, as from `time‘. */ unsigned short int millitm;/* Additional milliseconds. */ short int timezone;/* Minutes west of GMT. */ short int dstflag;/* Nonzero if Daylight Savings Time used. */ };
times函數原型:
clock_t times(struct tms *buf);
參數:
指向tms結構體變數的指標。
傳回值:
clock_t等同於long類型。用於獲得進程運行時的CPU時間。
tms結構體(sys/times.h中):
/* Structure describing CPU time used by a process and its children. */struct tms { clock_t tms_utime;/* User CPU time. */ clock_t tms_stime;/* System CPU time. */ clock_t tms_cutime;/* User CPU time of dead children. */ clock_t tms_cstime;/* System CPU time of dead children. */ };
#include <stdio.h>#include <time.h>#include <sys/time.h>#include <sys/times.h>#include <sys/timeb.h>#include <unistd.h>int main(void){ int i = 0; int sum = 0; long tck = 0; long lBeginTime = 0; long lEndTime = 0; time_t curr; struct tm * tTM; struct tms tTMS; struct timeb tTimeB; tzset(); //time函數獲得秒數 time(&curr); printf("current time is %ld seconds\n", curr); //localtime函數轉換time_t tTM = localtime(&curr); printf("%4d-%02d-%02d %02d:%02d:%02d\n", tTM->tm_year + 1900, tTM->tm_mon + 1, tTM->tm_mday, tTM->tm_hour, tTM->tm_min, tTM->tm_sec); //ftime函數獲得時間包括毫秒 ftime(&tTimeB); tTM = localtime(&tTimeB.time); printf("%4d-%02d-%02d %02d:%02d:%02d :%3d\n", tTM->tm_year + 1900, tTM->tm_mon + 1, tTM->tm_mday, tTM->tm_hour, tTM->tm_min, tTM->tm_sec, tTimeB.millitm); //用timesFunction Compute以下迴圈運行花費的時間 lBeginTime = times(&tTMS); printf("lBeginTime = %ld\n", lBeginTime); while (1) { i = i + 1; if (i == 0) break; } lEndTime = times(&tTMS); printf("lEndTime = %ld\n", lEndTime); printf("迴圈使用的CPU時間為: %ld\n", lEndTime - lBeginTime); tck = sysconf(_SC_CLK_TCK);//擷取系統時鐘(1秒裡有多少個) printf("轉換為秒: %f\n", ((lEndTime - lBeginTime) / (double)tck)); return 0;}
執行結果:
[[email protected] ~]# ./test10
current time is 1421644980 seconds
2015-01-19 00:23:00
2015-01-19 00:23:00 :781
lBeginTime = 708268851
lEndTime = 708270107
迴圈使用的CPU時間為: 1256
轉換為秒: 12.560000
Linux time函數