diff options
| author | Paul Ganssle <paul@ganssle.io> | 2021-01-19 11:16:01 +0100 |
|---|---|---|
| committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-01-19 12:00:40 +0100 |
| commit | a5d70cca12aaa1067871eb4e67ab2ed088d1edd6 (patch) | |
| tree | 9cee8f3ffda53629160df0fab580d8e03ae13ce7 /django/utils/dateformat.py | |
| parent | de4e854f079dd3a638b9919ad73e5835d5e90d3f (diff) | |
[3.2.x] Refs #32365 -- Allowed use of non-pytz timezone implementations.
Backport of 10d126198434810529e0220b0c6896ed64ca0e88 from master
Diffstat (limited to 'django/utils/dateformat.py')
| -rw-r--r-- | django/utils/dateformat.py | 40 |
1 files changed, 14 insertions, 26 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index 9bd05a437b..38e89c47bb 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -20,7 +20,8 @@ from django.utils.dates import ( ) from django.utils.regex_helper import _lazy_re_compile from django.utils.timezone import ( - get_default_timezone, is_aware, is_naive, make_aware, + _datetime_ambiguous_or_imaginary, get_default_timezone, is_aware, is_naive, + make_aware, ) from django.utils.translation import gettext as _ @@ -160,15 +161,9 @@ class TimeFormat(Formatter): if not self.timezone: return "" - name = None - try: + if not _datetime_ambiguous_or_imaginary(self.data, self.timezone): name = self.timezone.tzname(self.data) - except Exception: - # pytz raises AmbiguousTimeError during the autumn DST change. - # This happens mainly when __init__ receives a naive datetime - # and sets self.timezone = get_default_timezone(). - pass - if name is None: + else: name = self.format('O') return str(name) @@ -184,16 +179,13 @@ class TimeFormat(Formatter): If timezone information is not available, return an empty string. """ - if not self.timezone: + if ( + not self.timezone or + _datetime_ambiguous_or_imaginary(self.data, self.timezone) + ): return "" - try: - offset = self.timezone.utcoffset(self.data) - except Exception: - # pytz raises AmbiguousTimeError during the autumn DST change. - # This happens mainly when __init__ receives a naive datetime - # and sets self.timezone = get_default_timezone(). - return "" + offset = self.timezone.utcoffset(self.data) # `offset` is a datetime.timedelta. For negative values (to the west of # UTC) only days can be negative (days=-1) and seconds are always @@ -232,16 +224,12 @@ class DateFormat(TimeFormat): def I(self): # NOQA: E743, E741 "'1' if Daylight Savings Time, '0' otherwise." - try: - if self.timezone and self.timezone.dst(self.data): - return '1' - else: - return '0' - except Exception: - # pytz raises AmbiguousTimeError during the autumn DST change. - # This happens mainly when __init__ receives a naive datetime - # and sets self.timezone = get_default_timezone(). + if ( + not self.timezone or + _datetime_ambiguous_or_imaginary(self.data, self.timezone) + ): return '' + return '1' if self.timezone.dst(self.data) else '0' def j(self): "Day of the month without leading zeros; i.e. '1' to '31'" |
