st_atime:文件中的數據最后被訪問的時間
Time when file data was last accessed. Changed by the
following functions: creat(), mknod(), pipe(),
utime(2), and read(2).
st_mtime:文件中的內容最后修改時間
Time when data was last modified. Changed by the fol-
lowing functions: creat(), mknod(), pipe(), utime(),
and write(2).
st_ctime:文件的所有者,所屬的組,鏈接數,權限最后發生改變的時間
Time when file status was last changed. Changed by the
following functions: chmod(), chown(), creat(),
link(2), mknod(), pipe(), unlink(2), utime(), and
write().
一:結構體的定義
1、time_t
typedef long time_t; /* time value */
2.struct timespec
typedef long time_t; #ifndef _TIMESPEC #define _TIMESPEC struct timespec { time_t tv_sec; // seconds long tv_nsec; // and nanoseconds }; #endif
struct timespec有兩個成員,一個是秒,一個是納秒, 所以最高精確度是納秒。
一般由函數int clock_gettime(clockid_t, struct timespec *)獲取特定時鍾的時間,常用如下4種時鍾:
- CLOCK_REALTIME 統當前時間,從1970年1.1日算起
- CLOCK_MONOTONIC 系統的啟動時間,不能被設置
- CLOCK_PROCESS_CPUTIME_ID 本進程運行時間
- CLOCK_THREAD_CPUTIME_ID 本線程運行時間
struct tm *localtime(const time_t *clock); //線程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//線程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
3.timeval
timeval是一個結構體,在time.h中定義為: struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t tv_usec; /* Microseconds. */ }; 其中,tv_sec為Epoch(1970-1-1零點零分)到創建struct timeval時的秒數,tv_usec為微秒數,即秒后面的零頭。
4.tm
struct tm { int tm_sec; /*代表目前秒數,正常范圍為0-59,但允許至61秒 */ int tm_min; /*代表目前分數,范圍0-59*/ int tm_hour; /* 從午夜算起的時數,范圍為0-23 */ int tm_mday; /* 目前月份的日數,范圍01-31 */ int tm_mon; /*代表目前月份,從一月算起,范圍從0-11 */ int tm_year; /*從1900 年算起至今的年數*/ int tm_wday; /* 一星期的日數,從星期一算起,范圍為0-6。*/ int tm_yday; /* Days in year.[0-365] */ int tm_isdst; /*日光節約時間的旗標DST. [-1/0/1]*/ };
二:函數的具體操作
time()
time_t time(time_t * timer)
clock_gettime()
#include <sys/time.h> int clock_gettime(clockid_t clock_id, struct timespec *tsp); //返回值:若成功,返回0;若出錯,返回-1
clock_settime()
要對特定的時鍾設置時間,可以調用clock_settime函數。
#include <sys/time.h> int clock_settime(clockid_t clock_id, const struct timespec *tsp); //返回值:若成功,返回0;若出錯,返回-1
gettimeofday
需要適當的權限來更改時鍾值,打哪會有些時鍾是不能修改的。
#include <sys/time.h> int gettimeofday(struct timeval *restrict tp, void *restrict tzp); //返回值:總是返回0
tzp的唯一合法值是NULL,其他值將產生不確定的結果。gettimeofday函數以距特定時間(1970年1月1日00:00:00)的秒數的方式將當前時間存放在tp指向的timeval結構中,而該結構將當前時間表示為秒和微秒。
localtime和gmtime
#include <time.h> struct tm *gmtime(const time_t *calptr); struct tm *localtime(const time_t *calptr); //返回值:指向分解的tm結構的指針;若出錯,返回NULL
兩個函數localtime和gmtime將日歷時間轉換成分解的時間,並將這些存放在一個tm結構中。
localtime()和gmtime()的區別:
localtime_r()和gmtime_r()函數
struct tm *gmtime_r(const time_t *timep, struct tm *result); struct tm *localtime_r(const time_t *timep, struct tm *result);
一個好的方法是使用gmtime_r和localtime_r,由於使用了用戶分配的內存,這兩個函數是不會出錯的。
mktime()
函數mktime以本地時間的年、月、日等作為參數,將其變換成time_t值。
#include <time.h> time_t mktime(struct tm *tmptr); //返回值:若成功,返回日歷時間;若出錯,返回-1
strftime
size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr); size_t strftime_l(char *restrict buf, size_t maxsize,const char *restrict format, const struct tm *restrict tmptr, locale_t locale); //返回值:若有空間,返回存入數組的字節數;否則,返回0
格式化結果存放在一個長度為maxsize個字符的buf數組中,如果buf長度足以存放格式化結果及一個null終止符,則返回在buf中存放的字節數;否則返回0。
format參數控制時間值的格式。
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { time_t t; struct tm *tmp; char buf1[16]; char buf2[64]; time(&t); tmp = localtime(&t); if (strftime(buf1, 16, "time and date: %r, %a %b %d, %Y",tmp) == 0) printf("buffer1 length is too small\n"); else printf("%s\n",buf1); if (strftime(buf2, 64, "time and date: %r, %a %b %d, %Y",tmp) == 0) printf("buffer2 length is too small\n"); else printf("%s\n",buf2); return 0; }
strptime
是strftime函數反過來的版本,把字符串時間轉換成分解時間。
#include <time.h> char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tmptr); //返回值:指向上次解析的字符的下一個字符的指針;否則,返回NULL
format參數給出了buf參數指向的緩沖區的字符串的格式。雖然與strftime函數的說明稍有不同,但是格式說明是類似的。
asctime()函數
功 能: 轉換日期和時間為相應的字符串(英文簡寫形式,形如:Mon Feb 16 11:29:26 2009)ctime()函數
函數返回的const char *末尾有一個\n(換行符)。man手冊給出ctime()說明: It converts the calendar time t into a null-terminated string of the form;"Wed Jun 30 21:49:08 1993\n"
而asctime是通過tm結構來生成時間字符串。
difftime()函數
這是由它的參數決定的。
三:sleep usleep clock
1..Sleep函數(不同平台、編譯器之間可能函數名,函數參數單位不一樣)
頭文件:#include<windows.h>
定義函數:unsigned sleep(unsigned seconds);
函數說明:此函數執行掛起一段時間。
example:(對於windows+codeblocks下,Sleep(),單位為ms)
#include<stdio.h> #include<windows.h> main() { int i,j; i=time((time_t*)NULL); Sleep(2000); //延遲2s j=time((time_t*)NULL); printf("延時了%d秒",j-i); }
2.clock函數
函數定義:clock_t clock(void) ;
函數說明:該程序從啟動到函數調用占用CPU的時間。
example:
#include<stdio.h> #include<windows.h> main() { int i,j; Sleep(2000); i=clock(); Sleep(2000); j=clock(); printf("開始%d\n結束%d\n經過%d\n",i,j,j-i); }
3. usleep
頭文件: #include <unistd.h>
功 能: usleep功能把進程掛起一段時間, 單位是微秒(百萬分之一秒);
語 法: void usleep(int micro_seconds);
返回值: 無
內容說明:本函數可暫時使程序停止執行。參數 micro_seconds 為要暫停的微秒數(us)。
注 意:
這個函數不能工作在windows 操作系統中。用在Linux的測試環境下面。
參 見:usleep() 與sleep()類似,用於延遲掛起進程。進程被掛起放到reday queue。
是一般情況下,延遲時間數量級是秒的時候,盡可能使用sleep()函數。
如果延遲時間為幾十毫秒(1ms = 1000us),或者更小,盡可能使用usleep()函數。這樣才能最佳的利用CPU時間
時鍾換算:
微秒,時間單位,符號us(英語:microsecond ).
1微秒等於百萬分之一秒(10的負6 次方秒)
0.000 001 微秒 = 1皮秒
0.001 微秒 = 1納秒
1,000 微秒 = 1毫秒
1,000,000 微秒 = 1秒
1s = 1000ms
1ms = 1000μs
1μs = 1000ns
1ns = 1000ps