C Linux stat () getting atime / mtime with nsec precision

Learning C, and I'm trying to get a visual comparison of the types and sizes of variables returned stat()for atime / mtime attributes and for nsec precision values.

I run stat()in a file and want to get the mtime and mtime nsec values โ€‹โ€‹from the returned stat structure, and then save these values โ€‹โ€‹in separate variables (which I then want to pass into utimes()... a long story!).
According to http://www.kernel.org/doc/man-pages/online/pages/man2/stat.2.html#NOTES I can get a value from st_mtim.tv_nsecor st_mtimensecdepending on various OS / build conditions. In my real program, I will check both options and use what is installed, or just lean back on the usual second accuracyst_mtime

What type and size of variable must be declared in order to keep the normal timestamp returned st_mtime?

What type and size of variable must be specified to declare an nsec value from st_mtim.tv_nsecor st_mtimensec?
Are they decimal, including the number of whole seconds of time? Or do they just return the nsec part of the time?

Do I need to declare different sizes of variables for nsecs depending on my system architecture?

And finally, what conversion qualifiers do I need to output these variables with printf()?

Cheers, B

+3
source share
2 answers
  • st_mtimeshould be time_t.
  • According to POSIX, the <time.h>type tv_nsecis simple long.
  • , st_mtim.tv_nsec, .
  • long l; time_t, , AFAIK.
+4

st_mtim.tv_nsec [0,999999999]. tv_sec. 1000000000 64- , .

+1

All Articles