# HG changeset patch # User cmlenz # Date 1186517322 0 # Node ID 9fedbb6f98715f0b2ec7bacb73f0eb58ab720123 # Parent e47245bf65b36da8c3e589dd68e5a96588350a67 Implement day-of-week-in-month field in date formatting. Closes #50. diff --git a/babel/dates.py b/babel/dates.py --- 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] diff --git a/babel/tests/dates.py b/babel/tests/dates.py --- 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')