summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2021-05-07 10:42:59 +0100
committerCarlton Gibson <carlton.gibson@noumenal.es>2021-05-12 11:08:41 +0200
commitd06c5b358149c02a62da8a5469264d05f29ac659 (patch)
tree2ca0d6884062980dea5e8085af0868f9d9b5a266 /django/utils
parent69ffb1acf38bd34f76707468bb592eb4b164e2da (diff)
Fixed #32366 -- Updated datetime module usage to recommended approach.
- Replaced datetime.utcnow() with datetime.now(). - Replaced datetime.utcfromtimestamp() with datetime.fromtimestamp(). - Replaced datetime.utctimetuple() with datetime.timetuple(). - Replaced calendar.timegm() and datetime.utctimetuple() with datetime.timestamp().
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/dateformat.py11
-rw-r--r--django/utils/feedgenerator.py3
-rw-r--r--django/utils/http.py8
-rw-r--r--django/utils/timezone.py6
-rw-r--r--django/utils/version.py3
5 files changed, 13 insertions, 18 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py
index 4833b5af2c..0d4eb4bdf0 100644
--- a/django/utils/dateformat.py
+++ b/django/utils/dateformat.py
@@ -12,7 +12,6 @@ Usage:
"""
import calendar
import datetime
-import time
from email.utils import format_datetime as format_datetime_rfc5322
from django.utils.dates import (
@@ -20,7 +19,7 @@ from django.utils.dates import (
)
from django.utils.regex_helper import _lazy_re_compile
from django.utils.timezone import (
- _datetime_ambiguous_or_imaginary, get_default_timezone, is_aware, is_naive,
+ _datetime_ambiguous_or_imaginary, get_default_timezone, is_naive,
make_aware,
)
from django.utils.translation import gettext as _
@@ -295,10 +294,10 @@ class DateFormat(TimeFormat):
def U(self):
"Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
- if isinstance(self.data, datetime.datetime) and is_aware(self.data):
- return int(calendar.timegm(self.data.utctimetuple()))
- else:
- return int(time.mktime(self.data.timetuple()))
+ value = self.data
+ if not isinstance(value, datetime.datetime):
+ value = datetime.datetime.combine(value, datetime.time.min)
+ return int(value.timestamp())
def w(self):
"Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)"
diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py
index f08e89b25c..24b2d1d172 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(tz=utc)
class Enclosure:
diff --git a/django/utils/http.py b/django/utils/http.py
index 5397bb8190..6aa45a2cd6 100644
--- a/django/utils/http.py
+++ b/django/utils/http.py
@@ -1,5 +1,4 @@
import base64
-import calendar
import datetime
import re
import unicodedata
@@ -112,9 +111,10 @@ def parse_http_date(date):
else:
raise ValueError("%r is not in a valid HTTP date format" % date)
try:
+ tz = datetime.timezone.utc
year = int(m['year'])
if year < 100:
- current_year = datetime.datetime.utcnow().year
+ current_year = datetime.datetime.now(tz=tz).year
current_century = current_year - (current_year % 100)
if year - (current_year % 100) > 50:
# year that appears to be more than 50 years in the future are
@@ -127,8 +127,8 @@ def parse_http_date(date):
hour = int(m['hour'])
min = int(m['min'])
sec = int(m['sec'])
- result = datetime.datetime(year, month, day, hour, min, sec)
- return calendar.timegm(result.utctimetuple())
+ result = datetime.datetime(year, month, day, hour, min, sec, tzinfo=tz)
+ return int(result.timestamp())
except Exception as exc:
raise ValueError("%r is not a valid date" % date) from exc
diff --git a/django/utils/timezone.py b/django/utils/timezone.py
index cf22ec34d0..bb2b6b9594 100644
--- a/django/utils/timezone.py
+++ b/django/utils/timezone.py
@@ -194,11 +194,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)
- else:
- return datetime.now()
+ return datetime.now(tz=utc if settings.USE_TZ else None)
# By design, these four functions don't perform any checks on their arguments.
diff --git a/django/utils/version.py b/django/utils/version.py
index 18cd1387a0..a72d3202fa 100644
--- a/django/utils/version.py
+++ b/django/utils/version.py
@@ -89,8 +89,9 @@ def get_git_changeset():
shell=True, cwd=repo_dir, universal_newlines=True,
)
timestamp = git_log.stdout
+ tz = datetime.timezone.utc
try:
- timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
+ timestamp = datetime.datetime.fromtimestamp(int(timestamp), tz=tz)
except ValueError:
return None
return timestamp.strftime('%Y%m%d%H%M%S')