man 2 stat
表示st_atime
中返回的值st_mtime
,st_ctime
和struct stat
属于time_t
类型,其分辨率为秒。如何以更高的分辨率获得相同的值,可能是struct timespec
或struct timeval
,或只是丢失的亚秒部分?
我需要这个用于Linux上的C ++,但我也用C标记,因为我认为两种语言的答案可能相同。
答案 0 :(得分:2)
根据man 2 stat
的注释部分:
Since kernel 2.5.48, the stat structure supports nanosecond resolution for the three file timestamp fields. Glibc
exposes the nanosecond component of each field using names of the form st_atim.tv_nsec if the _BSD_SOURCE or _SVID_SOURCE
feature test macro is defined. These fields are specified in POSIX.1-2008, and, starting with version 2.12, glibc also
exposes these field names if _POSIX_C_SOURCE is defined with the value 200809L or greater, or _XOPEN_SOURCE is defined
with the value 700 or greater. If none of the aforementioned macros are defined, then the nanosecond values are exposed
with names of the form st_atimensec. On file systems that do not support subsecond timestamps, the nanosecond fields are
returned with the value 0.