changeset 243:2ed73d964211

Implement day-of-week-in-month field in date formatting. Closes #50.
author cmlenz
date Tue, 07 Aug 2007 20:08:42 +0000
parents 9b13c9a436c5
children cfb4cb07cbc7
files babel/dates.py babel/tests/dates.py
diffstat 2 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -697,6 +697,8 @@
             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 @@
     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]
--- a/babel/tests/dates.py
+++ b/babel/tests/dates.py
@@ -65,6 +65,21 @@
         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')
Copyright (C) 2012-2017 Edgewall Software