The Linux Page

How do you get micro-seconds from the Linux stat(2) call?

A little while ago I noticed that I couldn't find documentation on how to get the modification time of a file in micro-seconds. The notorious stat() call returns st_mtime defined as a time_t in the documentation.

Well... The fact is that the structure is really composed of a timeval structure and not a time_t (that applies to all 3 time values: modification, last access, and last status change.) So in fact you can use the name of the field without the last letter and a sub-timeval field as in:

struct stat s;
seconds = s.st_mtim.tv_sec; // == s.st_mtime
microseconds = s.st_mtim.tv_usec;

Strange that the documentation does not even mention the fact in the Linux documentation...