In Linux, it seems that filesystem time is always some milliseconds behind system time, leading to inconsistencies if you want to check if a file has been modified before or after a given time in very narrow time ranges (milliseconds).
In any Linux system with a filesystem that supports nanosecond resolution (I tried with ext4 with 256-byte inodes and ZFS), if you try to do something like:
date +%H:%M:%S.%N; echo "hello" > test1; stat -c %y test1 | cut -d" " -f 2 the second output value (file modification time) is always some milliseconds behind the first one (system time), e.g.:
17:26:42.400823099 17:26:42.395348462 while it should be the other way around, since the file test1 is modified after calling the date command.
You can get the same result in python:
import os, time def test(): print(time.time()) with open("test1", "w") as f: f.write("hello") print(os.stat("test1").st_mtime) test() 1698255477.3125281 1698255477.3070245 Why is it so, and is there a way to avoid it, so that system time is consistent with filesystem time? The only workaround I found so far is to get filesystem "time" (whatever that means in practice) by creating a dummy temporary file and getting its modification time, like this:
def get_filesystem_time(): """ get the current filesystem time by creating a temporary file and getting its modification time. """ with tempfile.NamedTemporaryFile() as f: return os.stat(f.name).st_mtime but I wonder if there is a cleaner solution.