Commits

cmlenz  committed e4c88f5

Merged [301:303] from [source:trunk] via svnmerge.

  • Participants
  • Parent commits 7267918
  • Branches stable-0.9.x

Comments (0)

Files changed (2)

File babel/dates.py

         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, num)
         elif char == 'm':
             return self.format(self.value.minute, num)
         elif char == 's':
         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):

File babel/tests/dates.py

         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), '12')
+        self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 PM')
+
 
 class FormatDateTestCase(unittest.TestCase):