summaryrefslogtreecommitdiff
path: root/django/utils/dateformat.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/dateformat.py')
-rw-r--r--django/utils/dateformat.py34
1 files changed, 28 insertions, 6 deletions
diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py
index f4de4e7f7c..ea55eaaf9d 100644
--- a/django/utils/dateformat.py
+++ b/django/utils/dateformat.py
@@ -136,6 +136,8 @@ class TimeFormat(Formatter):
return ""
seconds = self.Z()
+ if seconds == "":
+ return ""
sign = '-' if seconds < 0 else '+'
seconds = abs(seconds)
return "%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60)
@@ -167,7 +169,14 @@ class TimeFormat(Formatter):
if not self.timezone:
return ""
- name = self.timezone.tzname(self.data) if self.timezone else None
+ name = None
+ try:
+ 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:
name = self.format('O')
return six.text_type(name)
@@ -188,7 +197,14 @@ class TimeFormat(Formatter):
if not self.timezone:
return ""
- offset = self.timezone.utcoffset(self.data)
+ 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` is a datetime.timedelta. For negative values (to the west of
# UTC) only days can be negative (days=-1) and seconds are always
# positive. e.g. UTC-1 -> timedelta(days=-1, seconds=82800, microseconds=0)
@@ -228,10 +244,16 @@ class DateFormat(TimeFormat):
def I(self):
"'1' if Daylight Savings Time, '0' otherwise."
- if self.timezone and self.timezone.dst(self.data):
- return '1'
- else:
- return '0'
+ 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().
+ return ''
def j(self):
"Day of the month without leading zeros; i.e. '1' to '31'"