]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Implement day-of-week-in-month field in date formatting. Closes #50.
authorChristopher Lenz <cmlenz@gmail.com>
Tue, 7 Aug 2007 20:08:42 +0000 (20:08 +0000)
committerChristopher Lenz <cmlenz@gmail.com>
Tue, 7 Aug 2007 20:08:42 +0000 (20:08 +0000)
babel/dates.py
babel/tests/dates.py

index 2f876812f4f782615512805279fd9c7e989c6e9b..12ad104eaa046b2f96a03fa383b486b8c85aa39f 100644 (file)
@@ -697,6 +697,8 @@ class DateTimeFormat(object):
             return self.format(self.value.day, num)
         elif char == 'D':
             return self.format_day_of_year(num)
+        elif char == 'F':
+            return self.format_day_of_week_in_month()
         elif char in ('E', 'e', 'c'):
             return self.format_weekday(char, num)
         elif char == 'a':
@@ -774,6 +776,9 @@ class DateTimeFormat(object):
     def format_day_of_year(self, num):
         return self.format(self.get_day_of_year(), num)
 
+    def format_day_of_week_in_month(self):
+        return '%d' % ((self.value.day - 1) / 7 + 1)
+
     def format_period(self, char):
         period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)]
         return get_period_names(locale=self.locale)[period]
index 41cde4cdf049121a9b501ccf2469cb688c452e0b..9bf7d6356bb74a5dbc57af932d2a2cb9c4324a9c 100644 (file)
@@ -65,6 +65,21 @@ class DateTimeFormatTestCase(unittest.TestCase):
         fmt = dates.DateTimeFormat(d, locale='en_US')
         self.assertEqual('365', fmt['DDD'])
 
+    def test_day_of_week_in_month(self):
+        d = date(2007, 4, 15)
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('3', fmt['F'])
+
+    def test_day_of_week_in_month_first(self):
+        d = date(2007, 4, 1)
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('1', fmt['F'])
+
+    def test_day_of_week_in_month_last(self):
+        d = date(2007, 4, 29)
+        fmt = dates.DateTimeFormat(d, locale='en_US')
+        self.assertEqual('5', fmt['F'])
+
     def test_local_day_of_week(self):
         d = date(2007, 4, 1) # a sunday
         fmt = dates.DateTimeFormat(d, locale='de_DE')