From cbba49971bbbbe3e8c6685e4ce6ab87b1187ae87 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Tue, 24 Aug 2021 11:21:32 +0200 Subject: [PATCH] Fixed #32992 -- Restored offset extraction for fixed offset timezones. Regression in 10d126198434810529e0220b0c6896ed64ca0e88. --- django/utils/timezone.py | 7 +++++-- docs/releases/3.2.7.txt | 3 ++- tests/utils_tests/test_timezone.py | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/django/utils/timezone.py b/django/utils/timezone.py index bb2b6b9594..0f5d012c59 100644 --- a/django/utils/timezone.py +++ b/django/utils/timezone.py @@ -72,8 +72,11 @@ def get_current_timezone_name(): def _get_timezone_name(timezone): - """Return the name of ``timezone``.""" - return str(timezone) + """ + Return the offset for fixed offset timezones, or the name of timezone if + not set. + """ + return timezone.tzname(None) or str(timezone) # Timezone selection functions. diff --git a/docs/releases/3.2.7.txt b/docs/releases/3.2.7.txt index 0219460649..644e71dafd 100644 --- a/docs/releases/3.2.7.txt +++ b/docs/releases/3.2.7.txt @@ -9,4 +9,5 @@ Django 3.2.7 fixes several bugs in 3.2.6. Bugfixes ======== -* ... +* Fixed a regression in Django 3.2 that caused the incorrect offset extraction + from fixed offset timezones (:ticket:`32992`). diff --git a/tests/utils_tests/test_timezone.py b/tests/utils_tests/test_timezone.py index 2e28d3a9a5..0972135dcc 100644 --- a/tests/utils_tests/test_timezone.py +++ b/tests/utils_tests/test_timezone.py @@ -260,6 +260,31 @@ class TimezoneTests(SimpleTestCase): self.assertEqual(std.utcoffset(), datetime.timedelta(hours=1)) self.assertEqual(dst.utcoffset(), datetime.timedelta(hours=2)) + def test_get_timezone_name(self): + """ + The _get_timezone_name() helper must return the offset for fixed offset + timezones, for usage with Trunc DB functions. + + The datetime.timezone examples show the current behavior. + """ + tests = [ + # datetime.timezone, fixed offset with and without `name`. + (datetime.timezone(datetime.timedelta(hours=10)), 'UTC+10:00'), + (datetime.timezone(datetime.timedelta(hours=10), name='Etc/GMT-10'), 'Etc/GMT-10'), + # pytz, named and fixed offset. + (pytz.timezone('Europe/Madrid'), 'Europe/Madrid'), + (pytz.timezone('Etc/GMT-10'), '+10'), + ] + if HAS_ZONEINFO: + tests += [ + # zoneinfo, named and fixed offset. + (zoneinfo.ZoneInfo('Europe/Madrid'), 'Europe/Madrid'), + (zoneinfo.ZoneInfo('Etc/GMT-10'), '+10'), + ] + for tz, expected in tests: + with self.subTest(tz=tz, expected=expected): + self.assertEqual(timezone._get_timezone_name(tz), expected) + def test_get_default_timezone(self): self.assertEqual(timezone.get_default_timezone_name(), 'America/Chicago')