Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime.datetime.utcnow() should not have tzinfo #52

Closed
Pilen opened this issue Jul 31, 2020 · 2 comments · Fixed by #54
Closed

datetime.datetime.utcnow() should not have tzinfo #52

Pilen opened this issue Jul 31, 2020 · 2 comments · Fixed by #54

Comments

@Pilen
Copy link

Pilen commented Jul 31, 2020

Hi, I find this library very neat and have started using it in multiple tests.
But there is one issue:

In [1]: import datetime

In [2]: import time_machine

In [3]: @time_machine.travel("2020-07-28 13:15:30.478705")
   ...: def foo():
   ...:     return datetime.datetime.utcnow()
   ...:

In [4]: datetime.datetime.utcnow()
Out[4]: datetime.datetime(2020, 7, 31, 13, 46, 25, 828701)

In [5]: foo()
Out[5]: datetime.datetime(2020, 7, 28, 13, 15, 30, 478705, tzinfo=datetime.timezone.utc)

time_machine adds a timezone on datetime instances created via .utcnow() but this is wrong according to python.
It even specifies it directly in the documentation https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow

Return the current UTC date and time, with tzinfo None.

This change in the behaviour of the datetime library can cause changes in behaviour of the tested code.
Eg. when serialized to a string it adds "+00:00"

@bbbart
Copy link

bbbart commented Aug 29, 2020

I second this. I have a test testing code including pytz.utc.localize(datetime.utcnow()...) which now raises a ValueError during time travel, making it impossible to use this library in that particular test.

@adamchainz
Copy link
Owner

Thanks for the report. I've released the fix in version 1.2.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants