question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FakeDatetime timestamps don't match those generated from real datetime in some instances.

See original GitHub issue

Came across a strange bug while writing tests where there is an 8 hours (likely from PST - UTC) offset between timestamps generated by FakeDatetime objects vs real Datetime objects.
I found a case where these objects compare equally, have every time variable equal, yet don’t generate matching timestamps.

from datetime import datetime
from freezegun import freeze_time

def timestamp_since(in_time):
    print(f"got {in_time!r} and {in_time.timestamp()}")
    print(f"currently {datetime.now()!r} and {datetime.now().timestamp()}")
    print(f"tzinfo equal: {in_time.tzinfo == datetime.now().tzinfo}")
    print(f"time equal: {in_time == datetime.now()}")
    print(f"timestamps equal: {in_time.timestamp() == datetime.now().timestamp()}")
    return datetime.now().timestamp() - in_time.timestamp()

initial_time = datetime.fromtimestamp(0)

with freeze_time(initial_time) as frozen_datetime:
    print(timestamp_since(initial_time))

Running this on my python3.8 timezone PST system yields this

got datetime.datetime(1969, 12, 31, 16, 0) and 0.0
currently FakeDatetime(1969, 12, 31, 16, 0) and -28800.0
tzinfo equal: True
time equal: True
timestamps equal: False
-28800.0

So the two objects compare equally yet generate very different timestamps. I had a failing test in an interview due to this. If it’s useful to see this bug in the context of a real unit test I can provide that.

In the docs for this library you suggest making real datetimes and passing them into freeze_time(), but then if you compare the timestamps the passed-in datetime generate, they don’t match the datetimes as seen by the calling function. I found this counterintuitive.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mcscopecommented, Apr 14, 2020

I’d be happy to contribute a patch for this issue, if/when I figure out what the right fix is.

0reactions
spuleccommented, Apr 26, 2020

I think this is running into the same issue as here: https://github.com/spulec/freezegun/pull/136

My best suggestion is probably to not pass in an actual datetime to freeze_time(), but pass in a string representation.

We can talk about whether or not we think there is a bigger issue to fix, though it would be backwards-incompatible as mentioned in the PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - FakeDatetime timestamps don't match those generated ...
FakeDatetime timestamps don't match those generated from real datetime in some instances. ... Came across a strange bug while writing tests where there...
Read more >
Trying to mock datetime.date.today(), but not working
UPDATE: It's OK, I just went ahead and imported the datetime module in the test file. I thought the trick was some how...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
The problem with this solution is that it can give an inconsistent length to the dates because some months and days of the...
Read more >
How to use the freezegun.api.FakeDatetime function in ... - Snyk
api.FakeDatetime function in freezegun. To help you get started, we've selected a few freezegun examples, based on popular ways it is used in ......
Read more >
Using Python datetime to Work With Dates and Times
Fortunately, datetime provides several other convenient ways to create datetime instances. These methods don't require you to use integers to specify each ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found