为文件(stat?)获取纳秒级精度的atime,mtime,ctime字段 [英] Get a nanosecond-precise atime, mtime, ctime fields for file (stat?)

查看:538
本文介绍了为文件(stat?)获取纳秒级精度的atime,mtime,ctime字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些文件系统(例如ext4和JFS)提供了时间/时间字段的毫微秒分辨率。我怎样才能读ns解析字段? stat syscall 返回 time_t 这是第二个分辨率。

解决方案<第二个解决时间在字段中:

  time_t st_atime; / *上次访问时间* / 
time_t st_mtime; / *上次修改时间* /
time_t st_ctime; / *上次状态更改的时间* /

但是部分 http://www.kernel.org/doc/man-pages /online/pages/man2/stat.2.html 说:


由于内核2.5.48,stat结构支持纳秒分辨率的三个文件时间戳字段。如果定义了_BSD_SOURCE或
_SVID_SOURCE特性测试宏,则Glibc将使用st_atim.tv_nsec形式的名称公开每个
字段的纳秒组件。这些字段在
POSIX.1-2008中指定,并且从版本2.12开始,如果_POSIX_C_SOURCE用值200809L或更大值定义,或者
_XOPEN_SOURCE被定义,则glibc也暴露这些字段
的名称用值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


Some filesystems (e.g. ext4 and JFS) offer nanosecond resolution of atime/mtime fields. How can I read ns-resolution fields? The stat syscall returns time_t which is a second-resolution.

解决方案

The second-resolution times are in the fields:

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

But "NOTES" section of the man http://www.kernel.org/doc/man-pages/online/pages/man2/stat.2.html says:

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.

So, nsec parts of times are in the same "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

这篇关于为文件(stat?)获取纳秒级精度的atime,mtime,ctime字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆