]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Implement milliseconds in day (#48).
authorChristopher Lenz <cmlenz@gmail.com>
Mon, 16 Jul 2007 06:30:01 +0000 (06:30 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Mon, 16 Jul 2007 06:30:01 +0000 (06:30 +0000)
babel/dates.py
babel/tests/dates.py

index 27b9f01a0f0dfe14cb139dda7dbdc069cd00bc7a..aa5935f9bfab3ea83850ba8f296c2aabe3aa3c9f 100644 (file)
@@ -469,7 +469,9 @@ class DateTimeFormat(object):
         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 @@ class DateTimeFormat(object):
         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'):
index 705275791bc350f26fda0bec9e8f1ec4c5b6258e..abe26242ce604aa4257a123189cb22d5df652f20 100644 (file)
@@ -23,17 +23,17 @@ from babel import dates
 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 @@ class DateTimeFormatTestCase(unittest.TestCase):
         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 @@ class DateTimeFormatTestCase(unittest.TestCase):
         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 @@ class DateTimeFormatTestCase(unittest.TestCase):
         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,15 +67,25 @@ class DateTimeFormatTestCase(unittest.TestCase):
         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)
-        fmt = dates.DateTimeFormat(d, locale='en_US')
+        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['AAAA'])
+
     def test_timezone_rfc822(self):
         tz = timezone('Europe/Berlin')
         t = time(15, 30, tzinfo=tz)