]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Dummy/stub implementation for week-in-year and week-in-month date format fields....
authorChristopher Lenz <cmlenz@gmail.com>
Fri, 13 Jul 2007 22:22:03 +0000 (22:22 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Fri, 13 Jul 2007 22:22:03 +0000 (22:22 +0000)
babel/dates.py
babel/tests/dates.py

index d9dd63468a6d73dac17686276e1227e04c8838bb..38950cb46abcc850e43f2441d6306d6a956620fb 100644 (file)
@@ -438,17 +438,18 @@ class DateTimeFormat(object):
         self.locale = Locale.parse(locale)
 
     def __getitem__(self, name):
-        # TODO: a number of fields missing here
         char = name[0]
         num = len(name)
         if char == 'G':
             return self.format_era(char, num)
-        elif char in ('y', 'Y'):
+        elif char in ('y', 'Y', 'u'):
             return self.format_year(char, num)
         elif char in ('Q', 'q'):
             return self.format_quarter(char, num)
         elif char in ('M', 'L'):
             return self.format_month(char, num)
+        elif char in ('w', 'W'):
+            return self.format_week(char, num)
         elif char == 'd':
             return self.format(self.value.day, num)
         elif char in ('E', 'e', 'c'):
@@ -494,6 +495,14 @@ class DateTimeFormat(object):
         context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num]
         return get_month_names(width, context, self.locale)[self.value.month]
 
+    def format_week(self, char, num):
+        # FIXME: this should really be based on the first_week_day and
+        #        min_week_days locale data
+        if char.islower():
+            return self.value.strftime('%W')
+        else:
+            return '%d' % ((self.value.day + 6 - self.value.weekday()) / 7 + 1)
+
     def format_weekday(self, char, num):
         if num < 3:
             if char.islower():
@@ -542,7 +551,6 @@ class DateTimeFormat(object):
             pattern = {3: '%+03d%02d', 4: 'GMT %+03d:%02d'}[max(3, num)]
             return pattern % (hours, seconds // 60)
 
-
     def format(self, value, length):
         return ('%%0%dd' % length) % value
 
index 50db9c20dbe34a6053128caaf5c379ff6130ba62..5c55f1501b447ec0f0eb6f8654dbc83f8faf7d7d 100644 (file)
@@ -22,6 +22,16 @@ from babel import dates
 
 class DateTimeFormatTestCase(unittest.TestCase):
 
+    def test_week_of_year(self):
+        d = datetime(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)
+        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
         fmt = dates.DateTimeFormat(d, locale='de_DE')