freezegun icon indicating copy to clipboard operation
freezegun copied to clipboard

Inconsistent behaviour for freeze_time

Open gonvaled opened this issue 4 years ago • 1 comments

from datetime import datetime

from freezegun import freeze_time


now = datetime.now()
epochtime1 = now.timestamp()

with freeze_time(now):
    epochtime2 = datetime.now().timestamp()
    print(epochtime2 - epochtime1)

The output is:

3600.0

I would expect the output to be 0

I have a workaround, but feels weird:

from datetime import datetime

from freezegun import freeze_time


now = datetime.now()
# epochtime1 = now.timestamp()  # This is of no use, because of the freezegun bug

with freeze_time(now):
    now = datetime.now()  # Because of a (potential?) freezegun bug, we need to get "now" again after freezing
    epochtime1 = now.timestamp()  # Now we get the reference epoch again
    epochtime2 = datetime.now().timestamp()  # This happens deep in my code. Here for demonstration purposes
    print(epochtime2 - epochtime1)

gonvaled avatar Dec 24 '20 09:12 gonvaled

I am also puzzled by the fact that, while time is frozen, datetime.datetime.utcnow() and datetime.datetime.now() are both equal. I'm not sure if my issue is related to OP's, but it surely feels like it has something to do with the timezone.

I have tried setting a tz_offset when freezing time, but that would just give wrong results when dealing with daylight saving time (DST).

ccrisan avatar Dec 24 '20 23:12 ccrisan