获取文件(stat?)的纳秒级精确的time、mtime、ctime字段



一些文件系统(例如ext4和JFS)提供纳秒级的time/mtime字段分辨率。如何读取分辨率不高的字段?stat系统调用返回time_t,这是第二个解析。

二次解析时间在以下字段中:

           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 */

但是"注意事项"部分的男子http://www.kernel.org/doc/man-pages/online/pages/man2/stat.2.html说:

从内核2.5.48开始,stat结构支持纳秒级分辨率的三个文件时间戳字段。Glibc公开了每个组件的纳秒组件字段使用表单st_tim的名称。如果_BSD_SOURCE或定义了_SVID_SOURCE特性测试宏。中指定了这些字段POSIX.1-2008,以及从2.12版本开始,glibc也公开了这些字段如果_POSIX_C_SOURCE定义的值为200809L或更大,则使用_XOPEN_SOURCE的定义值为700或更大。如果没有定义了前面提到的宏,然后使用表单st_atimensec的名称。

所以,时间的nsec部分是在同一个"struct stat": (/usr/include/asm/stat.h)

 unsigned long st_atime_nsec;
 unsigned int st_mtime_nsec;
 unsigned long st_ctime_nsec;
 #define STAT_HAVE_NSEC 1

最新更新