views:

72

answers:

2

I've just discovered that the stat() call, and the corresponding struct stat, does not contain fields for the file times with precision greater than one second. For setting these times, there are a variety of {f,l}utime{n,}s() functions, but not for getting.

How then does obtain these times with nanosecond precision, preferably using POSIX API?

+1  A: 

Most file systems dont hold such accurate timestamps. From here:

Most file systems, including ext3, include timestamp data that is accurate to a second. Ext4 extends the accuracy of this data to a nanosecond. Some sources also indicate that the ext4 timestamps support dates through April 25, 2514, versus January 18, 2038, for ext32038, for ext3

adamk
+3  A: 

The stat structure returned by stat() itself has been upgraded for POSIX.1-2008.

The struct stat structure contains the three modification times as:

struct timespec st_atim - Last data access timestamp. 
struct timespec st_mtim - Last data modification timestamp. 
struct timespec st_ctim - Last file status change timestamp.

(from this OpenGroup link here under Headers, <sys/stat.h>) and that struct timespec is defined there (in <time.h>) as containing at least:

time_t  tv_sec          - Seconds. 
long    tv_nsec         - Nanoseconds. 

Previously, these three times were time_t values with their one-second resolution.

paxdiablo
The `struct stat` you describe here isn't in POSIX: http://www.opengroup.org/onlinepubs/000095399/basedefs/sys/stat.h.html . Care to elaborate on the source?
Dummy00001
HP-UX 11 and AIX 5.x do have only `time_t`. Linux (SLES 10) and Solaris 10 have the `struct timespec`, but its name/way to access it is not even uniform. What you quote above comes from Solaris 10 and would not work on any other system. Down-vote stands.
Dummy00001
@Dummy00001, it's in POSIX.1-2008 (issue 7). You're looking at the 2004 edition (issue 6). Follow the link I gave for details, repeated here: http://www.opengroup.org/onlinepubs/9699919799/
paxdiablo
@paxdiablo: Thanks. Duly noted. Though it still not very useful to have a solution from version of POSIX which is literally unsupported. At the moment, best portability is achieved by using POSIX 5 (or 6 with some luck). On one side. On another side, it still useless as most file systems do have only seconds precision anyway.
Dummy00001
God save the Queen.. and POSIX.
Matt Joiner
Mine has nanosecond precision, with 34 bits left to spare as padding.
Matt Joiner