1

Following test is always failing (this runs on a linux system, the question is not about other OSes):

    from time import time
    from decimal import Decimal
    from pathlib import Path

    def test_timing():
        start = Decimal(time())
        p = Path(__file__).parent / 'testfile.txt'  # does not yet exist
        p.touch()
        mt = p.stat().st_mtime
        p.unlink()  # unlinked before the failing assert
>       assert start <= mt
E       AssertionError: assert Decimal('1640930671.75709438323974609375') <= 1640930671.7534654

The gap is always about 3 to 7 ms.

How is it possible that Decimal(time()) at start returns a timestamp that's later than the file that is created two lines after it?

Is there an offset between python timestamps and the linux' ones? Does python proceed creating the file before the Decimal(time()) call is finished? What am I missing here?

EDIT: I should have mentionned it's ext4 filesystem.

zezollo
  • 4,606
  • 5
  • 28
  • 59
  • Filesystems don't store timestamps with infinite precision; the original FAT filesystem had a *two second* granularity! Apparently the one you're using will at least sometimes round up to its next representable timestamp. – jasonharper Dec 31 '21 at 06:43
  • That's not it. ext4 stores timestamps to the nanosecond. I'll post the real answer. – Tim Roberts Dec 31 '21 at 06:47

1 Answers1

3

The ext4 filesystem uses current_fs_time to grab the kernel timestamp. For efficiency, the kernel timestamp is a cached value, which is only updated at scheduler intervals, which is about 10ms. So, the time could be as much as 10ms old by the time it gets stored.

As a side note, there is no point in converting the timestamp to Decimal. The value from C is a double, which only holds about 17 digits. That means you get down to microseconds. Converting to Decimal does not gain you any additional precision; the other digits are essentially random.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Nice to know, that's something that would have been hard to debug unless you know the answer ;) – mozway Dec 31 '21 at 06:50
  • Many thanks! So, I'll just remove the useless `Decimal` and simply add a `time.sleep(0.015)` for instance, to remove the problem. Speed is not a problem in my case. – zezollo Dec 31 '21 at 11:13
  • I'm curious to know how this could ever be a problem in any way. This is a picayune implementation detail. If you need to record the creation time of the file, then use `p.stat().st_mtime` and not `time.time()`. – Tim Roberts Dec 31 '21 at 23:36