1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

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

View File

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