Skip to content

Commit

Permalink
Implement day-of-week-in-month field in date formatting. Closes #50.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmlenz committed Aug 7, 2007
1 parent c9e12ab commit 4646125
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,8 @@ def __getitem__(self, name):
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':
Expand Down Expand Up @@ -774,6 +776,9 @@ def format_weekday(self, char, num):
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]
Expand Down
15 changes: 15 additions & 0 deletions babel/tests/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ def test_day_of_year_last(self):
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')
Expand Down

0 comments on commit 4646125

Please sign in to comment.