diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2025-02-18 08:35:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-18 08:35:36 +0100 |
| commit | efb7f9ced2dcf71294353596a265e3fd67faffeb (patch) | |
| tree | b24b6127022fbe48c517d1acbe9e3c0c502391d9 /django | |
| parent | 0d1dd6bba0c18b7feb6caa5cbd8df80fbac54afd (diff) | |
Refs #36005 -- Used datetime.UTC alias instead of datetime.timezone.utc.
datetime.UTC was added in Python 3.11.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/humanize/templatetags/humanize.py | 4 | ||||
| -rw-r--r-- | django/contrib/sessions/backends/file.py | 2 | ||||
| -rw-r--r-- | django/contrib/sitemaps/views.py | 2 | ||||
| -rw-r--r-- | django/core/cache/backends/db.py | 4 | ||||
| -rw-r--r-- | django/core/files/storage/filesystem.py | 4 | ||||
| -rw-r--r-- | django/db/backends/base/base.py | 2 | ||||
| -rw-r--r-- | django/db/migrations/serializer.py | 4 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 2 | ||||
| -rw-r--r-- | django/http/response.py | 4 | ||||
| -rw-r--r-- | django/templatetags/tz.py | 6 | ||||
| -rw-r--r-- | django/utils/dateparse.py | 2 | ||||
| -rw-r--r-- | django/utils/feedgenerator.py | 2 | ||||
| -rw-r--r-- | django/utils/http.py | 6 | ||||
| -rw-r--r-- | django/utils/timezone.py | 4 | ||||
| -rw-r--r-- | django/utils/version.py | 2 | ||||
| -rw-r--r-- | django/views/decorators/http.py | 2 |
16 files changed, 25 insertions, 27 deletions
diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index 2e3f133033..f471f59389 100644 --- a/django/contrib/humanize/templatetags/humanize.py +++ b/django/contrib/humanize/templatetags/humanize.py @@ -1,5 +1,5 @@ import re -from datetime import date, datetime, timezone +from datetime import UTC, date, datetime from decimal import Decimal from django import template @@ -297,7 +297,7 @@ class NaturalTimeFormatter: if not isinstance(value, date): # datetime is a subclass of date return value - now = datetime.now(timezone.utc if is_aware(value) else None) + now = datetime.now(UTC if is_aware(value) else None) if value < now: delta = now - value if delta.days != 0: diff --git a/django/contrib/sessions/backends/file.py b/django/contrib/sessions/backends/file.py index e15b3f0141..9710cd2769 100644 --- a/django/contrib/sessions/backends/file.py +++ b/django/contrib/sessions/backends/file.py @@ -64,7 +64,7 @@ class SessionStore(SessionBase): Return the modification time of the file storing the session's content. """ modification = os.stat(self._key_to_file()).st_mtime - tz = datetime.timezone.utc if settings.USE_TZ else None + tz = datetime.UTC if settings.USE_TZ else None return datetime.datetime.fromtimestamp(modification, tz=tz) def _expiry_date(self, session_data): diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py index 166563b200..bbdcf4656e 100644 --- a/django/contrib/sitemaps/views.py +++ b/django/contrib/sitemaps/views.py @@ -35,7 +35,7 @@ def _get_latest_lastmod(current_lastmod, new_lastmod): if not isinstance(new_lastmod, datetime.datetime): new_lastmod = datetime.datetime.combine(new_lastmod, datetime.time.min) if timezone.is_naive(new_lastmod): - new_lastmod = timezone.make_aware(new_lastmod, datetime.timezone.utc) + new_lastmod = timezone.make_aware(new_lastmod, datetime.UTC) return new_lastmod if current_lastmod is None else max(current_lastmod, new_lastmod) diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py index f41105177f..d231d66b2f 100644 --- a/django/core/cache/backends/db.py +++ b/django/core/cache/backends/db.py @@ -1,7 +1,7 @@ "Database cache backend." import base64 import pickle -from datetime import datetime, timezone +from datetime import UTC, datetime from django.conf import settings from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache @@ -124,7 +124,7 @@ class DatabaseCache(BaseDatabaseCache): if timeout is None: exp = datetime.max else: - tz = timezone.utc if settings.USE_TZ else None + tz = UTC if settings.USE_TZ else None exp = datetime.fromtimestamp(timeout, tz=tz) exp = exp.replace(microsecond=0) if num > self._max_entries: diff --git a/django/core/files/storage/filesystem.py b/django/core/files/storage/filesystem.py index 54c31e536a..428ae61b40 100644 --- a/django/core/files/storage/filesystem.py +++ b/django/core/files/storage/filesystem.py @@ -1,5 +1,5 @@ import os -from datetime import datetime, timezone +from datetime import UTC, datetime from urllib.parse import urljoin from django.conf import settings @@ -215,7 +215,7 @@ class FileSystemStorage(Storage, StorageSettingsMixin): If timezone support is enabled, make an aware datetime object in UTC; otherwise make a naive one in the local timezone. """ - tz = timezone.utc if settings.USE_TZ else None + tz = UTC if settings.USE_TZ else None return datetime.fromtimestamp(ts, tz=tz) def get_accessed_time(self, name): diff --git a/django/db/backends/base/base.py b/django/db/backends/base/base.py index e6e0325d07..a1e7cf8f83 100644 --- a/django/db/backends/base/base.py +++ b/django/db/backends/base/base.py @@ -150,7 +150,7 @@ class BaseDatabaseWrapper: if not settings.USE_TZ: return None elif self.settings_dict["TIME_ZONE"] is None: - return datetime.timezone.utc + return datetime.UTC else: return zoneinfo.ZoneInfo(self.settings_dict["TIME_ZONE"]) diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py index eabdb24f20..300bde3780 100644 --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -81,8 +81,8 @@ class DatetimeDatetimeSerializer(BaseSerializer): """For datetime.datetime.""" def serialize(self): - if self.value.tzinfo is not None and self.value.tzinfo != datetime.timezone.utc: - self.value = self.value.astimezone(datetime.timezone.utc) + if self.value.tzinfo is not None and self.value.tzinfo != datetime.UTC: + self.value = self.value.astimezone(datetime.UTC) imports = ["import datetime"] return repr(self.value), set(imports) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 66d79626fd..14557527db 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -1339,7 +1339,7 @@ class CommaSeparatedIntegerField(CharField): def _to_naive(value): if timezone.is_aware(value): - value = timezone.make_naive(value, datetime.timezone.utc) + value = timezone.make_naive(value, datetime.UTC) return value diff --git a/django/http/response.py b/django/http/response.py index 4a0ea67013..6d09bc87e2 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -240,8 +240,8 @@ class HttpResponseBase: if expires is not None: if isinstance(expires, datetime.datetime): if timezone.is_naive(expires): - expires = timezone.make_aware(expires, datetime.timezone.utc) - delta = expires - datetime.datetime.now(tz=datetime.timezone.utc) + expires = timezone.make_aware(expires, datetime.UTC) + delta = expires - datetime.datetime.now(tz=datetime.UTC) # Add one second so the date matches exactly (a fraction of # time gets lost between converting to a timedelta and # then the date string). diff --git a/django/templatetags/tz.py b/django/templatetags/tz.py index f2cee2d3fe..5efd3c7fcf 100644 --- a/django/templatetags/tz.py +++ b/django/templatetags/tz.py @@ -1,7 +1,5 @@ import zoneinfo -from datetime import datetime -from datetime import timezone as datetime_timezone -from datetime import tzinfo +from datetime import datetime, tzinfo from django.template import Library, Node, TemplateSyntaxError from django.utils import timezone @@ -33,7 +31,7 @@ def utc(value): """ Convert a datetime to UTC. """ - return do_timezone(value, datetime_timezone.utc) + return do_timezone(value, datetime.UTC) @register.filter("timezone") diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py index 5cedd1affa..42c2684d7c 100644 --- a/django/utils/dateparse.py +++ b/django/utils/dateparse.py @@ -118,7 +118,7 @@ def parse_datetime(value): kw["microsecond"] = kw["microsecond"] and kw["microsecond"].ljust(6, "0") tzinfo = kw.pop("tzinfo") if tzinfo == "Z": - tzinfo = datetime.timezone.utc + tzinfo = datetime.UTC elif tzinfo is not None: offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0 offset = 60 * int(tzinfo[1:3]) + offset_mins diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index fae3271430..e52720a61e 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -277,7 +277,7 @@ class SyndicationFeed: if latest_date is None or item_date > latest_date: latest_date = item_date - return latest_date or datetime.datetime.now(tz=datetime.timezone.utc) + return latest_date or datetime.datetime.now(tz=datetime.UTC) class Enclosure: diff --git a/django/utils/http.py b/django/utils/http.py index 2098cbc36d..9ca32eab08 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -2,7 +2,7 @@ import base64 import re import unicodedata from binascii import Error as BinasciiError -from datetime import datetime, timezone +from datetime import UTC, datetime from email.utils import formatdate from urllib.parse import quote, unquote from urllib.parse import urlencode as original_urlencode @@ -115,7 +115,7 @@ def parse_http_date(date): try: year = int(m["year"]) if year < 100: - current_year = datetime.now(tz=timezone.utc).year + current_year = datetime.now(tz=UTC).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 @@ -128,7 +128,7 @@ def parse_http_date(date): hour = int(m["hour"]) min = int(m["min"]) sec = int(m["sec"]) - result = datetime(year, month, day, hour, min, sec, tzinfo=timezone.utc) + result = datetime(year, month, day, hour, min, sec, tzinfo=UTC) 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 102562b254..6d6cbf6d8f 100644 --- a/django/utils/timezone.py +++ b/django/utils/timezone.py @@ -5,7 +5,7 @@ Timezone-related classes and functions. import functools import zoneinfo from contextlib import ContextDecorator -from datetime import datetime, timedelta, timezone, tzinfo +from datetime import UTC, datetime, timedelta, timezone, tzinfo from asgiref.local import Local @@ -201,7 +201,7 @@ def now(): """ Return an aware or naive datetime.datetime, depending on settings.USE_TZ. """ - return datetime.now(tz=timezone.utc if settings.USE_TZ else None) + 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 4ef8cfbcfe..2bb650ac89 100644 --- a/django/utils/version.py +++ b/django/utils/version.py @@ -96,7 +96,7 @@ def get_git_changeset(): text=True, ) timestamp = git_log.stdout - tz = datetime.timezone.utc + tz = datetime.UTC try: timestamp = datetime.datetime.fromtimestamp(int(timestamp), tz=tz) except ValueError: diff --git a/django/views/decorators/http.py b/django/views/decorators/http.py index a4c1ebc31e..efdaa21e30 100644 --- a/django/views/decorators/http.py +++ b/django/views/decorators/http.py @@ -110,7 +110,7 @@ def condition(etag_func=None, last_modified_func=None): if last_modified_func: if dt := last_modified_func(request, *args, **kwargs): if not timezone.is_aware(dt): - dt = timezone.make_aware(dt, datetime.timezone.utc) + dt = timezone.make_aware(dt, datetime.UTC) res_last_modified = int(dt.timestamp()) # The value from etag_func() could be quoted or unquoted. res_etag = etag_func(request, *args, **kwargs) if etag_func else None |
