# HG changeset patch # User fschwarz # Date 1299278054 0 # Node ID f2098e9c05b483e32c67abffca83ae3597c65414 # Parent d877836a8455fc33100ae7492cdcf9c172276d4e Fix bad check in format_time (closes #257), reported with patch and tests by jomae diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -25,6 +25,8 @@ * Small speed improvement in format_date() (ticket #216). * Fix number formatting for locales where CLDR specifies alt or draft items (ticket #217) + * Fix bad check in format_time (ticket #257, reported with patch and tests by + jomae) Version 0.9.5 diff --git a/babel/dates.py b/babel/dates.py --- a/babel/dates.py +++ b/babel/dates.py @@ -578,7 +578,7 @@ if isinstance(time, datetime): if tzinfo is not None: time = time.astimezone(tzinfo) - if hasattr(tzinfo, 'localize'): # pytz + if hasattr(tzinfo, 'normalize'): # pytz time = tzinfo.normalize(time) time = time.timetz() elif tzinfo is not None: diff --git a/babel/tests/dates.py b/babel/tests/dates.py --- a/babel/tests/dates.py +++ b/babel/tests/dates.py @@ -13,11 +13,13 @@ from datetime import date, datetime, time import doctest +import new import unittest from pytz import timezone from babel import dates +from babel.util import FixedOffsetTimezone class DateTimeFormatTestCase(unittest.TestCase): @@ -247,12 +249,31 @@ "yyyy-MM-dd HH:mm", locale='en_US') +class TimeZoneAdjustTestCase(unittest.TestCase): + def _utc(self): + UTC = FixedOffsetTimezone(0, 'UTC') + def fake_localize(self, dt, is_dst=False): + raise NotImplementedError() + UTC.localize = new.instancemethod(fake_localize, UTC, UTC.__class__) + # This is important to trigger the actual bug (#257) + self.assertEqual(False, hasattr(UTC, 'normalize')) + return UTC + + def test_can_format_time_with_non_pytz_timezone(self): + # regression test for #257 + utc = self._utc() + t = datetime(2007, 4, 1, 15, 30, tzinfo=utc) + formatted_time = dates.format_time(t, 'long', tzinfo=utc, locale='en') + self.assertEqual('3:30:00 PM +0000', formatted_time) + + def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(dates)) suite.addTest(unittest.makeSuite(DateTimeFormatTestCase)) suite.addTest(unittest.makeSuite(FormatDateTestCase)) suite.addTest(unittest.makeSuite(FormatTimeTestCase)) + suite.addTest(unittest.makeSuite(TimeZoneAdjustTestCase)) return suite if __name__ == '__main__':