diff options
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'" |
