Skip to content

Commit

Permalink
fixes python-babel#619 wrong weeknumber for 31.12.2018
Browse files Browse the repository at this point in the history
The weeknumber was calculated to 53, but by definition the value
must compute to 1.

the fix will compute the weeknumber by using date.isocalendar if
locale.first_week_day == 0.

Also the computation of the year format 'YYYY' is replaced by isocalendar.

Tests added.
  • Loading branch information
BT-sschmid committed Jan 7, 2019
1 parent b7dda2a commit bb7e8c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions babel/dates.py
Expand Up @@ -1319,9 +1319,7 @@ def format_era(self, char, num):
def format_year(self, char, num):
value = self.value.year
if char.isupper():
week = self.get_week_number(self.get_day_of_year())
if week == 0:
value -= 1
value = self.value.isocalendar()[0]
year = self.format(value, num)
if num == 2:
year = year[-2:]
Expand Down Expand Up @@ -1505,8 +1503,15 @@ def get_week_number(self, day_of_period, day_of_week=None):
if first_day < 0:
first_day += 7
week_number = (day_of_period + first_day - 1) // 7

if 7 - first_day >= self.locale.min_week_days:
week_number += 1

if self.locale.first_week_day == 0:
max_weeks = date(year=self.value.year, day=28, month=12).isocalendar()[1]
if week_number > max_weeks:
week_number -= max_weeks

return week_number


Expand Down
9 changes: 9 additions & 0 deletions tests/test_dates.py
Expand Up @@ -79,6 +79,15 @@ def test_week_of_year_last_us_extra_week(self):
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('53', fmt['w'])

def test_week_of_year_de_first_us_last_with_year(self):
d = date(2018,12,31)
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('1', fmt['w'])
self.assertEqual('2019', fmt['YYYY'])
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('53', fmt['w'])
self.assertEqual('2018',fmt['yyyy'])

def test_week_of_month_first(self):
d = date(2006, 1, 8)
fmt = dates.DateTimeFormat(d, locale='de_DE')
Expand Down

0 comments on commit bb7e8c7

Please sign in to comment.