gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:58 25 4
gpt4 key购买 nike

man 2 stat 显示 struct 中返回的值 st_atimest_mtimest_ctime stat 属于 time_t 类型,具有秒分辨率。如何以更高的分辨率获得相同的值,可能是 struct timespecstruct timeval,或者只是缺少的亚秒部分?

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

最佳答案

根据 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.

关于c - 如何以最大分辨率获取文件时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21787497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com