time.h 是ISO C99 標準日期頭文件。
sys/time.h 是Linux系統的日期頭文件。
注: sys/time.h 通常會包含include "time.h"
一、time.h
編寫的代碼如果與平臺無關的,則需要在代碼裏 #include "time.h" 但這樣使用time_t等數據結構的話需要手動
#define __need_time_t
#define __need_timespec
二、sys/time.h
gettimeofday() 函數在sys/time.h 內。
定義 : int gettimeofday(struct timeval * tv, struct timezone * tz);
函數說明:gettimeofday()會把目前的時間用__restrict結構體返回,當地時區的信息則放到tz的結構中國。。
結構體:
1. timeval
struct timeval
{
long tv_sec; /* 秒 */
long tv_usec; /* 微秒 */
};
2. timezone
struct timezone{
int tz_minuteswest;/* 和 greenwich 時間差來多少分鐘*/
int tz_dsttime;/*type of DST correction */
}
3. 在 gettimeofday()函數中tv 或者 tz 都可以爲空。 如果爲空就不返回其對應的結構體。
4. 函數在執行成功後返回0, 失敗後返回-1, 錯誤代碼存在於errno 中。