Repository: adamchainz/time-machine

Issue:
datetime.datetime.utcnow() should not have tzinfo
Hi, I find this library very neat and have started using it in multiple tests.
But there is one issue:

```python
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"



Discussion:

Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 485c825..f5a2f8c 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -1,6 +1,12 @@
 History
 =======
 
+* Correctly return naive datetimes from ``datetime.utcnow()`` whilst time
+  travelling.
+
+  Thanks to Søren Pilgård and Bart Van Loon for the report in
+  `Issue #52 <https://github.com/adamchainz/time-machine/issues/52>`__.
+
 1.2.0 (2020-07-08)
 ------------------
 
diff --git a/src/time_machine.py b/src/time_machine.py
index 0832f6a..d0ed6ff 100644
--- a/src/time_machine.py
+++ b/src/time_machine.py
@@ -206,7 +206,7 @@ def utcnow():
     if not coordinates_stack:
         return _time_machine.original_utcnow()
     else:
-        return dt.datetime.fromtimestamp(time(), dt.timezone.utc)
+        return dt.datetime.utcfromtimestamp(time())
 
 
 # time module


Test patch:
diff --git a/tests/test_time_machine.py b/tests/test_time_machine.py
index bf85da5..4eacde2 100644
--- a/tests/test_time_machine.py
+++ b/tests/test_time_machine.py
@@ -88,6 +88,7 @@ def test_datetime_utcnow():
         assert now.minute == 0
         assert now.second == 0
         assert now.microsecond == 0
+        assert now.tzinfo is None
     assert dt.datetime.utcnow() >= LIBRARY_EPOCH_DATETIME