From 0fdc5d79b6cfe7317fd6b19f4cdf2183f2d048d1 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Wed, 12 Oct 2022 13:23:04 +0100 Subject: [PATCH] Simplified django.utils.formats.date_format()/time_format() calls. This removes redundant get_format() calls and passing a default value for the format argument. --- django/utils/formats.py | 2 +- tests/i18n/tests.py | 25 ++++++------------------- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/django/utils/formats.py b/django/utils/formats.py index b0a66e4e25..85d24b9443 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -217,7 +217,7 @@ def localize(value, use_l10n=None): elif isinstance(value, datetime.date): return date_format(value, use_l10n=use_l10n) elif isinstance(value, datetime.time): - return time_format(value, "TIME_FORMAT", use_l10n=use_l10n) + return time_format(value, use_l10n=use_l10n) return value diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 7d2300c1b5..0adacb765e 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -550,26 +550,13 @@ class FormattingTests(SimpleTestCase): self.assertIn( "23", time_format(some_datetime) ) # Uses TIME_FORMAT by default + self.assertIn("2017", date_format(some_datetime, "DATETIME_FORMAT")) + self.assertIn("2017", date_format(some_date, "YEAR_MONTH_FORMAT")) + self.assertIn("14", date_format(some_date, "MONTH_DAY_FORMAT")) + self.assertIn("2017", date_format(some_date, "SHORT_DATE_FORMAT")) self.assertIn( "2017", - date_format(some_datetime, format=get_format("DATETIME_FORMAT")), - ) - self.assertIn( - "2017", - date_format(some_date, format=get_format("YEAR_MONTH_FORMAT")), - ) - self.assertIn( - "14", date_format(some_date, format=get_format("MONTH_DAY_FORMAT")) - ) - self.assertIn( - "2017", - date_format(some_date, format=get_format("SHORT_DATE_FORMAT")), - ) - self.assertIn( - "2017", - date_format( - some_datetime, format=get_format("SHORT_DATETIME_FORMAT") - ), + date_format(some_datetime, "SHORT_DATETIME_FORMAT"), ) def test_locale_independent(self): @@ -961,7 +948,7 @@ class FormattingTests(SimpleTestCase): Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt), ) self.assertEqual( - date_format(datetime.datetime.now(), "DATE_FORMAT"), + date_format(datetime.datetime.now()), Template('{% now "DATE_FORMAT" %}').render(self.ctxt), )