summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Borgström <jonas@edgewall.org>2007-08-26 13:38:58 +0000
committerJonas Borgström <jonas@edgewall.org>2007-08-26 13:38:58 +0000
commitd8188245fb04230ca8cd4ad8fafe2f263ffe6b6c (patch)
treea6e1ac5f1303b63c0ba21b840523db799746b288
parent533ebf9110ae92f153259cdc6e4f373cc513398f (diff)
downloadbabel-d8188245fb04230ca8cd4ad8fafe2f263ffe6b6c.tar.gz
Fixed formatting bug with 12-hour clock patterns.
-rw-r--r--babel/dates.py14
-rw-r--r--babel/tests/dates.py13
2 files changed, 23 insertions, 4 deletions
diff --git a/babel/dates.py b/babel/dates.py
index daea499..92e9c95 100644
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -706,13 +706,19 @@ class DateTimeFormat(object):
elif char == 'a':
return self.format_period(char)
elif char == 'h':
- return self.format(self.value.hour % 12, num)
+ if self.value.hour % 12 == 0:
+ return self.format(12, num)
+ else:
+ return self.format(self.value.hour % 12, num)
elif char == 'H':
return self.format(self.value.hour, num)
elif char == 'K':
- return self.format(self.value.hour % 12 - 1, num)
+ return self.format(self.value.hour % 12, num)
elif char == 'k':
- return self.format(self.value.hour + 1, num)
+ if self.value.hour == 0:
+ return self.format(24, num)
+ else:
+ return self.format(self.value.hour + 1, num)
elif char == 'm':
return self.format(self.value.minute, num)
elif char == 's':
@@ -784,7 +790,7 @@ class DateTimeFormat(object):
return '%d' % ((self.value.day - 1) / 7 + 1)
def format_period(self, char):
- period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)]
+ period = {0: 'am', 1: 'pm'}[int(self.value.hour >= 12)]
return get_period_names(locale=self.locale)[period]
def format_frac_seconds(self, num):
diff --git a/babel/tests/dates.py b/babel/tests/dates.py
index af6fea8..b4fdc99 100644
--- a/babel/tests/dates.py
+++ b/babel/tests/dates.py
@@ -182,6 +182,19 @@ class DateTimeFormatTestCase(unittest.TestCase):
fmt = dates.DateTimeFormat(t, locale='fr_FR')
self.assertEqual(u'Heure de l’Europe centrale', fmt['vvvv'])
+ def test_hour_formatting(self):
+ l = 'en_US'
+ t = time(0, 0, 0)
+ self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 AM')
+ self.assertEqual(dates.format_time(t, 'H', locale=l), '0')
+ self.assertEqual(dates.format_time(t, 'k', locale=l), '24')
+ self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 AM')
+ t = time(12, 0, 0)
+ self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 PM')
+ self.assertEqual(dates.format_time(t, 'H', locale=l), '12')
+ self.assertEqual(dates.format_time(t, 'k', locale=l), '13')
+ self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 PM')
+
class FormatDateTestCase(unittest.TestCase):