]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
More work on #46 (week-of-year/week-of-month).
authorChristopher Lenz <cmlenz@gmail.com>
Tue, 7 Aug 2007 20:01:35 +0000 (20:01 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Tue, 7 Aug 2007 20:01:35 +0000 (20:01 +0000)
babel/dates.py
babel/tests/dates.py

index 5654a82322855658e7d77190b6f2895782fc1db6..2f876812f4f782615512805279fd9c7e989c6e9b 100644 (file)
@@ -746,12 +746,19 @@ class DateTimeFormat(object):
 
     def format_week(self, char, num):
         if char.islower(): # week of year
-            return self.format(self.get_week_number(self.get_day_of_year()),
-                               num)
+            week = self.get_week_number(self.get_day_of_year())
+            if week == 0:
+                # FIXME: I suppose this should return the last week number of
+                #        the previous year
+                pass
+            return self.format(week, num)
         else: # week of month
-            # FIXME: this should really be based on the first_week_day and
-            #        min_week_days locale data
-            return '%d' % ((self.value.day + 6 - self.value.weekday()) / 7 + 1)
+            week = self.get_week_number(self.value.day)
+            if week == 0:
+                # FIXME: I suppose this should return the last week number of
+                #        the previous month
+                pass
+            return '%d' % week
 
     def format_weekday(self, char, num):
         if num < 3:
index a94bfd213cd415c3883ab83e919efbbc402c1c2d..41cde4cdf049121a9b501ccf2469cb688c452e0b 100644 (file)
@@ -22,17 +22,33 @@ from babel import dates
 
 class DateTimeFormatTestCase(unittest.TestCase):
 
-    def test_week_of_year(self):
+    def test_week_of_year_first(self):
         d = date(2006, 1, 8)
         fmt = dates.DateTimeFormat(d, locale='de_DE')
         self.assertEqual('1', fmt['w'])
         fmt = dates.DateTimeFormat(d, locale='en_US')
         self.assertEqual('02', fmt['ww'])
 
-    def test_week_of_month(self):
-        d = date(2007, 4, 1)
+    def test_week_of_year_last(self):
+        d = date(2005, 12, 26)
+        fmt = dates.DateTimeFormat(d, locale='de_DE')
+        self.assertEqual('52', fmt['w'])
         fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('53', fmt['ww'])
+
+    def test_week_of_month_first(self):
+        d = date(2006, 1, 8)
+        fmt = dates.DateTimeFormat(d, locale='de_DE')
         self.assertEqual('1', fmt['W'])
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('2', fmt['W'])
+
+    def test_week_of_month_last(self):
+        d = date(2006, 1, 29)
+        fmt = dates.DateTimeFormat(d, locale='de_DE')
+        self.assertEqual('4', fmt['W'])
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('5', fmt['W'])
 
     def test_day_of_year(self):
         d = date(2007, 4, 1)