]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
format_datetime: using 'S...S' format is incorrect 360/head
authoriamshubh22 <sgshubham03@gmail.com>
Mon, 29 Feb 2016 16:20:10 +0000 (21:50 +0530)
committeriamshubh22 <sgshubham03@gmail.com>
Mon, 29 Feb 2016 16:23:51 +0000 (21:53 +0530)
Fixes https://github.com/python-babel/babel/issues/350

babel/dates.py
tests/test_dates.py

index 1af9955a592180991f6da010d816023315cc489b..bd496cbd1848b6a47ff5f1bbc6e7b396d470aa40 100644 (file)
@@ -1287,8 +1287,13 @@ class DateTimeFormat(object):
         return get_period_names(locale=self.locale)[period]
 
     def format_frac_seconds(self, num):
-        value = str(self.value.microsecond)
-        return self.format(round(float('.%s' % value), num) * 10**num, num)
+        """ Return fractional seconds.
+
+        Rounds the time's microseconds to the precision given by the number \
+        of digits passed in.
+        """
+        value = self.value.microsecond / 1000000
+        return self.format(round(value, num) * 10**num, num)
 
     def format_milliseconds_in_day(self, num):
         msecs = self.value.microsecond // 1000 + self.value.second * 1000 + \
index 5155c0cca66b015579d2450361cb82b8988694bc..3b1ea36d3790eab2c4f63914538fb547df2fa84f 100644 (file)
@@ -156,9 +156,21 @@ class DateTimeFormatTestCase(unittest.TestCase):
         self.assertEqual('4', fmt['c']) # friday is first day of week
 
     def test_fractional_seconds(self):
-        t = time(15, 30, 12, 34567)
+        t = time(8, 3, 9, 799)
         fmt = dates.DateTimeFormat(t, locale='en_US')
-        self.assertEqual('3457', fmt['SSSS'])
+        self.assertEqual('0', fmt['S'])
+        t = time(8, 3, 1, 799)
+        fmt = dates.DateTimeFormat(t, locale='en_US')
+        self.assertEqual('0008', fmt['SSSS'])
+        t = time(8, 3, 1, 34567)
+        fmt = dates.DateTimeFormat(t, locale='en_US')
+        self.assertEqual('0346', fmt['SSSS'])
+        t = time(8, 3, 1, 345678)
+        fmt = dates.DateTimeFormat(t, locale='en_US')
+        self.assertEqual('345678', fmt['SSSSSS'])
+        t = time(8, 3, 1, 799)
+        fmt = dates.DateTimeFormat(t, locale='en_US')
+        self.assertEqual('00080', fmt['SSSSS'])
 
     def test_fractional_seconds_zero(self):
         t = time(15, 30, 0)