如何以最大分辨率获取文件时间戳?

时间:2014-02-14 19:03:45

标签: c linux

man 2 stat表示st_atime中返回的值st_mtimest_ctimestruct stat属于time_t类型,其分辨率为秒。如何以更高的分辨率获得相同的值,可能是struct timespecstruct timeval,或只是丢失的亚秒部分?

我需要这个用于Linux上的C ++,但我也用C标记,因为我认为两种语言的答案可能相同。

1 个答案:

答案 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.