1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Refs #36005 -- Used datetime.UTC alias instead of datetime.timezone.utc.

datetime.UTC was added in Python 3.11.
This commit is contained in:
Mariusz Felisiak
2025-02-18 08:35:36 +01:00
committed by GitHub
parent 0d1dd6bba0
commit efb7f9ced2
41 changed files with 271 additions and 272 deletions

View File

@@ -1,6 +1,6 @@
import platform
import unittest
from datetime import datetime, timezone
from datetime import UTC, datetime
from unittest import mock
from django.test import SimpleTestCase
@@ -329,61 +329,61 @@ class HttpDateProcessingTests(unittest.TestCase):
def test_parsing_rfc1123(self):
parsed = parse_http_date("Sun, 06 Nov 1994 08:49:37 GMT")
self.assertEqual(
datetime.fromtimestamp(parsed, timezone.utc),
datetime(1994, 11, 6, 8, 49, 37, tzinfo=timezone.utc),
datetime.fromtimestamp(parsed, UTC),
datetime(1994, 11, 6, 8, 49, 37, tzinfo=UTC),
)
@unittest.skipIf(platform.architecture()[0] == "32bit", "The Year 2038 problem.")
@mock.patch("django.utils.http.datetime")
def test_parsing_rfc850(self, mocked_datetime):
mocked_datetime.side_effect = datetime
now_1 = datetime(2019, 11, 6, 8, 49, 37, tzinfo=timezone.utc)
now_2 = datetime(2020, 11, 6, 8, 49, 37, tzinfo=timezone.utc)
now_3 = datetime(2048, 11, 6, 8, 49, 37, tzinfo=timezone.utc)
now_1 = datetime(2019, 11, 6, 8, 49, 37, tzinfo=UTC)
now_2 = datetime(2020, 11, 6, 8, 49, 37, tzinfo=UTC)
now_3 = datetime(2048, 11, 6, 8, 49, 37, tzinfo=UTC)
tests = (
(
now_1,
"Tuesday, 31-Dec-69 08:49:37 GMT",
datetime(2069, 12, 31, 8, 49, 37, tzinfo=timezone.utc),
datetime(2069, 12, 31, 8, 49, 37, tzinfo=UTC),
),
(
now_1,
"Tuesday, 10-Nov-70 08:49:37 GMT",
datetime(1970, 11, 10, 8, 49, 37, tzinfo=timezone.utc),
datetime(1970, 11, 10, 8, 49, 37, tzinfo=UTC),
),
(
now_1,
"Sunday, 06-Nov-94 08:49:37 GMT",
datetime(1994, 11, 6, 8, 49, 37, tzinfo=timezone.utc),
datetime(1994, 11, 6, 8, 49, 37, tzinfo=UTC),
),
(
now_2,
"Wednesday, 31-Dec-70 08:49:37 GMT",
datetime(2070, 12, 31, 8, 49, 37, tzinfo=timezone.utc),
datetime(2070, 12, 31, 8, 49, 37, tzinfo=UTC),
),
(
now_2,
"Friday, 31-Dec-71 08:49:37 GMT",
datetime(1971, 12, 31, 8, 49, 37, tzinfo=timezone.utc),
datetime(1971, 12, 31, 8, 49, 37, tzinfo=UTC),
),
(
now_3,
"Sunday, 31-Dec-00 08:49:37 GMT",
datetime(2000, 12, 31, 8, 49, 37, tzinfo=timezone.utc),
datetime(2000, 12, 31, 8, 49, 37, tzinfo=UTC),
),
(
now_3,
"Friday, 31-Dec-99 08:49:37 GMT",
datetime(1999, 12, 31, 8, 49, 37, tzinfo=timezone.utc),
datetime(1999, 12, 31, 8, 49, 37, tzinfo=UTC),
),
)
for now, rfc850str, expected_date in tests:
with self.subTest(rfc850str=rfc850str):
mocked_datetime.now.return_value = now
parsed = parse_http_date(rfc850str)
mocked_datetime.now.assert_called_once_with(tz=timezone.utc)
mocked_datetime.now.assert_called_once_with(tz=UTC)
self.assertEqual(
datetime.fromtimestamp(parsed, timezone.utc),
datetime.fromtimestamp(parsed, UTC),
expected_date,
)
mocked_datetime.reset_mock()
@@ -391,8 +391,8 @@ class HttpDateProcessingTests(unittest.TestCase):
def test_parsing_asctime(self):
parsed = parse_http_date("Sun Nov 6 08:49:37 1994")
self.assertEqual(
datetime.fromtimestamp(parsed, timezone.utc),
datetime(1994, 11, 6, 8, 49, 37, tzinfo=timezone.utc),
datetime.fromtimestamp(parsed, UTC),
datetime(1994, 11, 6, 8, 49, 37, tzinfo=UTC),
)
def test_parsing_asctime_nonascii_digits(self):
@@ -405,8 +405,8 @@ class HttpDateProcessingTests(unittest.TestCase):
def test_parsing_year_less_than_70(self):
parsed = parse_http_date("Sun Nov 6 08:49:37 0037")
self.assertEqual(
datetime.fromtimestamp(parsed, timezone.utc),
datetime(2037, 11, 6, 8, 49, 37, tzinfo=timezone.utc),
datetime.fromtimestamp(parsed, UTC),
datetime(2037, 11, 6, 8, 49, 37, tzinfo=UTC),
)