changeset 218:0a30f3974997

Support for fractional seconds field in date formatting. Closes #47.
author cmlenz
date Sun, 15 Jul 2007 22:09:02 +0000
parents 15ac328954f5
children f065fb523fd6
files babel/dates.py babel/tests/dates.py
diffstat 2 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -468,6 +468,8 @@
             return self.format(self.value.minute, num)
         elif char == 's':
             return self.format(self.value.second, num)
+        elif char == 'S':
+            return self.format_frac_seconds(self.value.microsecond, num)
         elif char in ('z', 'Z', 'v'):
             return self.format_timezone(char, num)
         else:
@@ -518,6 +520,10 @@
         period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)]
         return get_period_names(locale=self.locale)[period]
 
+    def format_frac_seconds(self, char, num):
+        value = str(self.value.microsecond)
+        return self.format(round(float('.%s' % value), num) * 10**num, num)
+
     def format_timezone(self, char, num):
         if char in ('z', 'v'):
             if hasattr(self.value.tzinfo, 'zone'):
--- a/babel/tests/dates.py
+++ b/babel/tests/dates.py
@@ -66,6 +66,16 @@
         fmt = dates.DateTimeFormat(d, locale='dv_MV')
         self.assertEqual('4', fmt['c']) # friday is first day of week
 
+    def test_fractional_seconds(self):
+        d = time(15, 30, 12, 34567)
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('3457', fmt['SSSS'])
+
+    def test_fractional_seconds_zero(self):
+        d = time(15, 30, 0)
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('0000', fmt['SSSS'])
+
     def test_timezone_rfc822(self):
         tz = timezone('Europe/Berlin')
         t = time(15, 30, tzinfo=tz)
Copyright (C) 2012-2017 Edgewall Software