# HG changeset patch # User cmlenz # Date 1184567401 0 # Node ID 4de6f46048301eb84d3ff68e16b6473b5a038b0f # Parent 1f3c3924b1b56cd336d2b6d436ee943737e5494e Implement milliseconds in day (#48). diff --git a/babel/dates.py b/babel/dates.py --- a/babel/dates.py +++ b/babel/dates.py @@ -469,7 +469,9 @@ elif char == 's': return self.format(self.value.second, num) elif char == 'S': - return self.format_frac_seconds(self.value.microsecond, num) + return self.format_frac_seconds(num) + elif char == 'A': + return self.format_milliseconds_in_day(num) elif char in ('z', 'Z', 'v'): return self.format_timezone(char, num) else: @@ -520,10 +522,15 @@ 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): + def format_frac_seconds(self, num): value = str(self.value.microsecond) return self.format(round(float('.%s' % value), num) * 10**num, num) + def format_milliseconds_in_day(self, num): + msecs = self.value.microsecond // 1000 + self.value.second * 1000 + \ + self.value.minute * 60000 + self.value.hour * 3600000 + return self.format(msecs, 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 @@ -23,17 +23,17 @@ class DateTimeFormatTestCase(unittest.TestCase): def test_week_of_year(self): - d = datetime(2007, 4, 1) + d = date(2007, 4, 1) fmt = dates.DateTimeFormat(d, locale='en_US') self.assertEqual('13', fmt['w']) def test_week_of_month(self): - d = datetime(2007, 4, 1) + d = date(2007, 4, 1) fmt = dates.DateTimeFormat(d, locale='en_US') self.assertEqual('1', fmt['W']) def test_local_day_of_week(self): - d = datetime(2007, 4, 1) # a sunday + d = date(2007, 4, 1) # a sunday fmt = dates.DateTimeFormat(d, locale='de_DE') self.assertEqual('7', fmt['e']) # monday is first day of week fmt = dates.DateTimeFormat(d, locale='en_US') @@ -41,7 +41,7 @@ fmt = dates.DateTimeFormat(d, locale='dv_MV') self.assertEqual('03', fmt['ee']) # friday is first day of week - d = datetime(2007, 4, 2) # a monday + d = date(2007, 4, 2) # a monday fmt = dates.DateTimeFormat(d, locale='de_DE') self.assertEqual('1', fmt['e']) # monday is first day of week fmt = dates.DateTimeFormat(d, locale='en_US') @@ -50,7 +50,7 @@ self.assertEqual('04', fmt['ee']) # friday is first day of week def test_local_day_of_week_standalone(self): - d = datetime(2007, 4, 1) # a sunday + d = date(2007, 4, 1) # a sunday fmt = dates.DateTimeFormat(d, locale='de_DE') self.assertEqual('7', fmt['c']) # monday is first day of week fmt = dates.DateTimeFormat(d, locale='en_US') @@ -58,7 +58,7 @@ fmt = dates.DateTimeFormat(d, locale='dv_MV') self.assertEqual('3', fmt['c']) # friday is first day of week - d = datetime(2007, 4, 2) # a monday + d = date(2007, 4, 2) # a monday fmt = dates.DateTimeFormat(d, locale='de_DE') self.assertEqual('1', fmt['c']) # monday is first day of week fmt = dates.DateTimeFormat(d, locale='en_US') @@ -67,14 +67,24 @@ 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') + t = time(15, 30, 12, 34567) + fmt = dates.DateTimeFormat(t, locale='en_US') self.assertEqual('3457', fmt['SSSS']) def test_fractional_seconds_zero(self): - d = time(15, 30, 0) + t = time(15, 30, 0) + fmt = dates.DateTimeFormat(t, locale='en_US') + self.assertEqual('0000', fmt['SSSS']) + + def test_milliseconds_in_day(self): + t = time(15, 30, 12, 345000) + fmt = dates.DateTimeFormat(t, locale='en_US') + self.assertEqual('55812345', fmt['AAAA']) + + def test_milliseconds_in_day_zero(self): + d = time(0, 0, 0) fmt = dates.DateTimeFormat(d, locale='en_US') - self.assertEqual('0000', fmt['SSSS']) + self.assertEqual('0000', fmt['AAAA']) def test_timezone_rfc822(self): tz = timezone('Europe/Berlin')