Home > Software engineering >  File seems to be timestamped before it is created
File seems to be timestamped before it is created

Time:12-31

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.

CodePudding user response:

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.

  • Related