From d46e158ee21cb067a65d44699e6dce00b3dab8e3 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Wed, 23 Mar 2022 11:48:52 +0100 Subject: [PATCH] Refs #32365 -- Made migration writer use datetime.timezone.utc. --- django/db/migrations/serializer.py | 9 +++------ tests/migrations/test_writer.py | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py index fb4a1964d9..38da1e6ecc 100644 --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -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): diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 56ad3a07cf..07a2db989c 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -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, )