Linux下time_t時間類型
time_t類型定義在time.h標頭檔中:
#ifndef __TIME_T #define __TIME_T typedef long time_t; #endif
可見,time_t實際是一個長整型。其值表示為從UTC(coordinated universal time)時間1970年1月1日00時00分00秒(也稱為Linux系統的Epoch時間)到當前時刻的秒數。由於time_t類型長度的限制,它所表示的時間不能晚於2038年1月19日03時14分07秒(UTC)。為了能夠表示更久遠的時間,可用64位或更長的整形數來儲存日曆時間,這裡不作詳述。
使用time()函數擷取目前時間的time_t值,使用ctime()函數將time_t轉為當地時間字串。
備忘:UTC時間有時也稱為GMT時間,其實UTC和GMT兩者幾乎是同一概念。它們都是指格林尼治標準時間,只不過UTC的稱呼更為正式一點。兩者區別在於前者是天文上的概念,而後者是基於一個原子鐘。
Linux下列印時間的格式為:
printf(“%lu\n”,time);
Linux下通常會涉及到時間參數的列印:比如擷取檔案屬性stat函數,該函數原型為:
int stat(const char *path, struct stat *buf); 其中buf是一個檔案的資訊結構體,該結構體內成員如下: struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };
共用記憶體中的shmctl函數中也會涉及到時間time參數,所以linux開發中難免會用到time參數列印;