最近的近似“创建日期”是在struct stat的st_ctime成员,但实际上记录最后一次inode的改变。如果您创建该文件并且从不修改其大小或权限,那么该文件将用作创建时间。否则,至少在标准的Unix系统中,没有创建文件的时间记录。
出于您的目的,按st_mtime排序...或获取名称中带有时间戳的文件。
请注意,如果您是在达尔文(Mac OS X上),创建时间是可用的。从手册页stat(2):
然而,宏定义_DARWIN_FEATURE_64_BIT_INODE时,stat结构将现在被定义为:
struct stat { /* when _DARWIN_FEATURE_64_BIT_INODE is defined */
dev_t st_dev; /* ID of device containing file */
mode_t st_mode; /* Mode of file (see below) */
nlink_t st_nlink; /* Number of hard links */
ino_t st_ino; /* File serial number */
uid_t st_uid; /* User ID of the file */
gid_t st_gid; /* Group ID of the file */
dev_t st_rdev; /* Device ID */
struct timespec st_atimespec; /* time of last access */
struct timespec st_mtimespec; /* time of last data modification */
struct timespec st_ctimespec; /* time of last status change */
struct timespec st_birthtimespec; /* time of file creation(birth) */
off_t st_size; /* file size, in bytes */
blkcnt_t st_blocks; /* blocks allocated for file */
blksize_t st_blksize; /* optimal blocksize for I/O */
uint32_t st_flags; /* user defined flags for file */
uint32_t st_gen; /* file generation number */
int32_t st_lspare; /* RESERVED: DO NOT USE! */
int64_t st_qspare[2]; /* RESERVED: DO NOT USE! */
};
注意st_birthtimespec领域。请注意,所有时间都在struct timespec值中,因此存在亚秒时间(tv_nsec给出纳秒分辨率)。 POSIX 2008 要求struct timespec时间保持在标准时间;达尔文遵循这一点。