Refs #32365 -- Made migration writer use datetime.timezone.utc.

This commit is contained in:
Carlton Gibson 2022-03-23 11:48:52 +01:00
parent 5cc9464e4d
commit d46e158ee2
2 changed files with 14 additions and 16 deletions

View File

@ -16,7 +16,6 @@ from django.db import models
from django.db.migrations.operations.base import Operation
from django.db.migrations.utils import COMPILED_REGEX_TYPE, RegexObject
from django.utils.functional import LazyObject, Promise
from django.utils.timezone import utc
from django.utils.version import get_docs_version
@ -68,12 +67,10 @@ class DatetimeDatetimeSerializer(BaseSerializer):
"""For datetime.datetime."""
def serialize(self):
if self.value.tzinfo is not None and self.value.tzinfo != utc:
self.value = self.value.astimezone(utc)
if self.value.tzinfo is not None and self.value.tzinfo != datetime.timezone.utc:
self.value = self.value.astimezone(datetime.timezone.utc)
imports = ["import datetime"]
if self.value.tzinfo is not None:
imports.append("from django.utils.timezone import utc")
return repr(self.value).replace("datetime.timezone.utc", "utc"), set(imports)
return repr(self.value), set(imports)
class DecimalSerializer(BaseSerializer):

View File

@ -537,8 +537,9 @@ class WriterTests(SimpleTestCase):
self.assertSerializedResultEqual(
datetime.datetime(2012, 1, 1, 1, 1, tzinfo=tzinfo),
(
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc)",
{"import datetime", "from django.utils.timezone import utc"},
"datetime.datetime"
"(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc)",
{"import datetime"},
),
)
@ -547,8 +548,8 @@ class WriterTests(SimpleTestCase):
2012, 1, 1, 2, 1, tzinfo=zoneinfo.ZoneInfo("Europe/Paris")
),
(
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc)",
{"import datetime", "from django.utils.timezone import utc"},
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc)",
{"import datetime"},
),
)
if pytz:
@ -557,8 +558,8 @@ class WriterTests(SimpleTestCase):
datetime.datetime(2012, 1, 1, 2, 1)
),
(
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc)",
{"import datetime", "from django.utils.timezone import utc"},
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc)",
{"import datetime"},
),
)
@ -888,7 +889,9 @@ class WriterTests(SimpleTestCase):
"mymodel",
"myfield",
models.DateTimeField(
default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc),
default=datetime.datetime(
2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc
),
),
),
]
@ -897,9 +900,7 @@ class WriterTests(SimpleTestCase):
writer = MigrationWriter(migration)
output = writer.as_string()
self.assertIn(
"import datetime\n"
"from django.db import migrations, models\n"
"from django.utils.timezone import utc\n",
"import datetime\nfrom django.db import migrations, models\n",
output,
)