我在我的系统 (Debian) 上查看了这个。
由于某种原因,st_mtime 被定义为宏;定义是st_mtim。
忽略标头的内容(它们对编译器的意义大于对人类读者的意义),只需遵循文档即可。 man 2 stat 会告诉您需要包含哪些标头,并且至少在我的系统上它会显示一个示例程序。
血淋淋的细节(你不需要知道正确使用它):
在/usr/include/bits/stat.h 中,struct stat 类型定义有以下成员(以及其他成员):
struct timespec st_atim; /* Time of last access. */
struct timespec st_mtim; /* Time of last modification. */
struct timespec st_ctim; /* Time of last status change. */
struct timespec 是一个结构,其中包含一个类型为 time_t 的成员,称为 tv_sec。 (其他成员允许更高分辨率的时间戳。)
接下来是以下预处理器指令:
# define st_atime st_atim.tv_sec
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
所以你可以在自己的代码中引用foo.st_mtime,它会扩展为foo.st_mtim.tv_sec,这就是你需要的time_t对象。
更新:
st_atim 等人的声明之前(在我当前的 Ubuntu 18.04 系统上)是这样评论的:
/* Nanosecond resolution timestamps are stored in a format
equivalent to 'struct timespec'. This is the type used
whenever possible but the Unix namespace rules do not allow the
identifier 'timespec' to appear in the <sys/stat.h> header.
Therefore we have to handle the use of this header in strictly
standard-compliant sources special. */