summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Ganssle <paul@ganssle.io>2021-01-19 09:44:26 +0100
committerCarlton Gibson <carlton.gibson@noumenal.es>2021-01-19 09:46:59 +0100
commita251ac46f7bdcae80421568e69b145b4b38f659c (patch)
tree29c0086c3a603ba6fd8d2c25cf998898e81dd278
parent8c7ff7b8cff4a62423512efc3c4fe7955edfeed5 (diff)
downloaddjango-c/replace-utcnow.tar.gz
Replaced .utcnow() call with .now(utc)c/replace-utcnow
Using .now() is the modern recommended approach. https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow More depth here: https://blog.ganssle.io/articles/2019/11/utcnow.html
-rw-r--r--django/utils/feedgenerator.py3
-rw-r--r--django/utils/timezone.py3
2 files changed, 2 insertions, 4 deletions
diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py
index f08e89b25c..1ad1802b0f 100644
--- a/django/utils/feedgenerator.py
+++ b/django/utils/feedgenerator.py
@@ -172,8 +172,7 @@ class SyndicationFeed:
if latest_date is None or item_date > latest_date:
latest_date = item_date
- # datetime.now(tz=utc) is slower, as documented in django.utils.timezone.now
- return latest_date or datetime.datetime.utcnow().replace(tzinfo=utc)
+ return latest_date or datetime.datetime.now(utc)
class Enclosure:
diff --git a/django/utils/timezone.py b/django/utils/timezone.py
index a87ec5fc33..fc1a10c390 100644
--- a/django/utils/timezone.py
+++ b/django/utils/timezone.py
@@ -190,8 +190,7 @@ def now():
Return an aware or naive datetime.datetime, depending on settings.USE_TZ.
"""
if settings.USE_TZ:
- # timeit shows that datetime.now(tz=utc) is 24% slower
- return datetime.utcnow().replace(tzinfo=utc)
+ return datetime.now(utc)
else:
return datetime.now()