From: Christopher Lenz Date: Tue, 7 Aug 2007 20:08:42 +0000 (+0000) Subject: Implement day-of-week-in-month field in date formatting. Closes #50. X-Git-Tag: 1.0~408 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4646125684f216ece4e38da5e42f93844286e12e;p=thirdparty%2Fbabel.git Implement day-of-week-in-month field in date formatting. Closes #50. --- diff --git a/babel/dates.py b/babel/dates.py index 2f876812..12ad104e 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -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] diff --git a/babel/tests/dates.py b/babel/tests/dates.py index 41cde4cd..9bf7d635 100644 --- a/babel/tests/dates.py +++ b/babel/tests/dates.py @@ -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')