Determine file system timestamp precision
As far as I know, there is no place that this information is stored on. It is coded into the filesystem. However, you can manually make a list of filesystems and the corresponding precision. I would use a case
statement to test the filesystem id against your list of filesystems. You can make the default 1 since there are very few examples where precision is less than 1 second.
Older versions of FAT and current versions of zip use 2-second timestamp precision from what I have read online. However, I suggest you fact-check that.
You can get the id of a file's filsystem with the following command.
stat -f --format="%t" $file
Normally you would be able to use statvfs or pathconf, but they don't seem to support any way to find out that piece of information.
Apparently such a feature is being discussed for a future POSIX standard
we will also file some aardvarks for a pathconf enhancement to return the granularity of timestamps on a per path basis.
Unfortunately, I can't see any clean way to do that today, not even in an OS-specific manner.
Any approach that tries to build a list of file systems and whether it supports sub-second resolution is dangerous. For example, ext4
seems to support nanosecond resolution if the inodes are 256 bytes, but not if they are 128 bytes.
Compiling a comprehensive and accurate list would be hard, may require root access, and it might change tomorrow. Which sounds harder than just running stat
a few times to me.
I just implemented a detection for a file database. I first set the timestamp of a file to 1234 millis, and then read it again and check if it is 1234 (at least ms precision) or 1000 (second precision). The things I learned so far were:
- XFS and EXT3: second precision
- EXT4: millisecond precision
- NTFS: 100ns precision (OK, got this from the docs...)