Commits

Felix Schwarz  committed 892424d

Fix bad check in format_time (closes #257), reported with patch and tests by jomae

  • Participants
  • Parent commits 745adc9
  • Branches stable-0.9.x

Comments (0)

Files changed (3)

  * 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

File babel/dates.py

     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:

File babel/tests/dates.py

 
 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):
                           "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__':