2014-02-14 86 views
1

man 2 stat顯示在struct stat中返回的值爲st_atime,st_mtimest_ctime的類型爲time_t,其具有秒分辨率。如何獲得更高分辨率的相同數值,可能是struct timespecstruct timeval,或者只是缺少的亞秒部分?如何以最大分辨率獲取文件時間戳?

我需要這個在Linux上的C++,但我也用C標記,因爲我認爲這兩種語言的答案可能是相同的。

回答

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. 
+0

有一點要記住,即使是在納秒級的分辨率是,你可能在你的硬件沒有get精確到納秒。例如,我認爲典型的英特爾硬件只能精確到1毫秒左右,所以即使你在幾納秒內得到一個值,你通常也不能相信低於1毫秒。 – jia103