# HG changeset patch # User cmlenz # Date 1184537342 0 # Node ID 1f3c3924b1b56cd336d2b6d436ee943737e5494e # Parent d37628e752c438ba69aca54a1ee31149cdb8cd89 Support for fractional seconds field in date formatting. Closes #47. diff --git a/babel/dates.py b/babel/dates.py --- 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'): diff --git a/babel/tests/dates.py b/babel/tests/dates.py --- 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)