Commits

Felix Schwarz committed 5ba68e4

format_time() and format_datetime() now accept also floats (#242)

Comments (0)

Files changed (3)

    attributes (language, territory, script, variant) are equal
  * resort to hard-coded message extractors/checkers if pkg_resources is 
    installed but no egg-info was found (#230)
+ * format_time() and format_datetime() now accept also floats (#242)
 
 
 Version 0.9.6
     """
     if datetime is None:
         datetime = datetime_.utcnow()
-    elif isinstance(datetime, (int, long)):
+    elif isinstance(datetime, (int, long, float)):
         datetime = datetime_.utcfromtimestamp(datetime)
     elif isinstance(datetime, time):
         datetime = datetime_.combine(date.today(), datetime)
     """
     if time is None:
         time = datetime.utcnow()
-    elif isinstance(time, (int, long)):
+    elif isinstance(time, (int, long, float)):
         time = datetime.utcfromtimestamp(time)
     if time.tzinfo is None:
         time = time.replace(tzinfo=UTC)

babel/tests/dates.py

 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://babel.edgewall.org/log/.
 
+import calendar
 from datetime import date, datetime, time, timedelta
 import doctest
 import new
         self.assertEqual('14', dates.format_date(d, 'w', locale='en_US'))
 
 
+class FormatDatetimeTestCase(unittest.TestCase):
+
+    def test_with_float(self):
+        d = datetime(2012, 4, 1, 15, 30, 29, tzinfo=timezone('UTC'))
+        epoch = float(calendar.timegm(d.timetuple()))
+        formatted_string = dates.format_datetime(epoch, format='long', locale='en_US')
+        self.assertEqual(u'April 1, 2012 3:30:29 PM +0000', formatted_string)
+                         
+
+
 class FormatTimeTestCase(unittest.TestCase):
 
     def test_with_naive_datetime_and_tzinfo(self):
                                    locale='en')
         self.assertEqual('11:30:00 AM EDT', string)
 
+    def test_with_float(self):
+        d = datetime(2012, 4, 1, 15, 30, 29, tzinfo=timezone('UTC'))
+        epoch = float(calendar.timegm(d.timetuple()))
+        formatted_time = dates.format_time(epoch, format='long', locale='en_US')
+        self.assertEqual(u'3:30:29 PM +0000', formatted_time)
+                    
+
     def test_with_date_fields_in_pattern(self):
         self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01),
                           "yyyy-MM-dd HH:mm", locale='en_US')
     suite.addTest(doctest.DocTestSuite(dates))
     suite.addTest(unittest.makeSuite(DateTimeFormatTestCase))
     suite.addTest(unittest.makeSuite(FormatDateTestCase))
+    suite.addTest(unittest.makeSuite(FormatDatetimeTestCase))
     suite.addTest(unittest.makeSuite(FormatTimeTestCase))
     suite.addTest(unittest.makeSuite(FormatTimedeltaTestCase))
     suite.addTest(unittest.makeSuite(TimeZoneAdjustTestCase))