Accessing subsecond timestamp values of struct stat in a portable way

263 Views Asked by At

The stat(2) manual addresses the support for nanosecond resolution for the timestamp fields, but it doesn't look trivial to test their presence or their names in a program intended to be portable: as many as four feature test macro names (_BSD_SOURCE, _SVID_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE) are mentioned. It looks like the manual is suggesting the following:

#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
    defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L || \
    defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700
    // use st_atim.tv_nsec, etc.
#elif 1 // really?
    // use st_atimensec, etc.
#else   // when?
    // no nanosecond field exists
#endif
  1. It doesn't mention any possibility of having neither st_atim.tv_nsec nor st_atimensec at all. Is one of the two names guaranteed to exist?
  2. It says the nanosecond fields are returned with the value 0, but this is indistinguishable from the actual value 0. How do I test if subsecond timestamps are really supported?
0

There are 0 best solutions below