Author: ramiro Date: 2011-09-25 10:08:31 -0700 (Sun, 25 Sep 2011) New Revision: 16903
Modified: django/trunk/django/utils/dateformat.py django/trunk/django/utils/tzinfo.py django/trunk/tests/regressiontests/utils/dateformat.py Log: Fixed #16924 -- Corrected `date` template filter handling of negative (West of UTC) timezone offsets. The 'O' format specifier output was incorrect. Thanks mrgriscom and Aymeric Augustin. Modified: django/trunk/django/utils/dateformat.py =================================================================== --- django/trunk/django/utils/dateformat.py 2011-09-25 05:21:29 UTC (rev 16902) +++ django/trunk/django/utils/dateformat.py 2011-09-25 17:08:31 UTC (rev 16903) @@ -182,9 +182,11 @@ return MONTHS_AP[self.data.month] def O(self): - "Difference to Greenwich time in hours; e.g. '+0200'" + "Difference to Greenwich time in hours; e.g. '+0200', '-0430'" seconds = self.Z() - return u"%+03d%02d" % (seconds // 3600, (seconds // 60) % 60) + sign = '-' if seconds < 0 else '+' + seconds = abs(seconds) + return u"%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60) def r(self): "RFC 2822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" @@ -275,8 +277,10 @@ if not self.timezone: return 0 offset = self.timezone.utcoffset(self.data) - # Only days can be negative, so negative offsets have days=-1 and - # seconds positive. Positive offsets have days=0 + # `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) + # Positive offsets have days=0 return offset.days * 86400 + offset.seconds def format(value, format_string): Modified: django/trunk/django/utils/tzinfo.py =================================================================== --- django/trunk/django/utils/tzinfo.py 2011-09-25 05:21:29 UTC (rev 16902) +++ django/trunk/django/utils/tzinfo.py 2011-09-25 17:08:31 UTC (rev 16903) @@ -13,7 +13,7 @@ else: self.__offset = timedelta(minutes=offset) - sign = offset < 0 and '-' or '+' + sign = '-' if offset < 0 else '+' self.__name = u"%s%02d%02d" % (sign, abs(offset) / 60., abs(offset) % 60) def __repr__(self): Modified: django/trunk/tests/regressiontests/utils/dateformat.py =================================================================== --- django/trunk/tests/regressiontests/utils/dateformat.py 2011-09-25 05:21:29 UTC (rev 16902) +++ django/trunk/tests/regressiontests/utils/dateformat.py 2011-09-25 17:08:31 UTC (rev 16903) @@ -127,3 +127,9 @@ self.assertEqual(dateformat.format(summertime, 'O'), u'+0200') self.assertEqual(dateformat.format(wintertime, 'I'), u'0') self.assertEqual(dateformat.format(wintertime, 'O'), u'+0100') + + # Ticket #16924 -- We don't need timezone support to test this + # 3h30m to the west of UTC + tz = FixedOffset(-3*60 - 30) + dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz) + self.assertEqual(dateformat.format(dt, 'O'), u'-0330') -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.