Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect iso week-years (w) #887

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,16 @@ def format_week(self, char: str, num: int) -> str:
date = self.value - datetime.timedelta(days=day_of_year)
week = self.get_week_number(self.get_day_of_year(date),
date.weekday())
elif self.locale.min_week_days == 4 and self.locale.first_week_day == 0:
# HACK: EOY days being punted into next year can occur
# in all week numbering situations, it's the
# reverse (underflowing the current year) which
# can't (requires min_week_days > 1, which is
# only widely used by ISO calendaring), so
# checking for ISO-ish locales is not correct
_, num_weeks, _ = self.value.replace(month=12, day=28).isocalendar()
if week > num_weeks:
week = 1
return self.format(week, num)
else: # week of month
week = self.get_week_number(self.value.day)
Expand Down Expand Up @@ -1637,16 +1647,6 @@ def get_week_number(self, day_of_period: int, day_of_week: int | None = None) ->
if 7 - first_day >= self.locale.min_week_days:
week_number += 1

if self.locale.first_week_day == 0:
# Correct the weeknumber in case of iso-calendar usage (first_week_day=0).
# If the weeknumber exceeds the maximum number of weeks for the given year
# we must count from zero.For example the above calculation gives week 53
# for 2018-12-31. By iso-calender definition 2018 has a max of 52
# weeks, thus the weeknumber must be 53-52=1.
max_weeks = datetime.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
24 changes: 12 additions & 12 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,18 @@ def test_abbreviated_month_alias(self):

def test_week_of_year_first(self):
d = date(2006, 1, 8)
assert dates.DateTimeFormat(d, locale='de_DE')['w'] == '1'
assert dates.DateTimeFormat(d, locale='en_US')['ww'] == '02'

def test_week_of_year_first_with_year(self):
d = date(2006, 1, 1)
fmt = dates.DateTimeFormat(d, locale='de_DE')
assert fmt['w'] == '52'
assert fmt['YYYY'] == '2005'

def test_week_of_year_last(self):
d = date(2006, 12, 26)
assert dates.DateTimeFormat(d, locale='de_DE')['w'] == '52'
assert dates.DateTimeFormat(d, locale='en_US')['w'] == '52'

def test_week_of_year_last_us_extra_week(self):
d = date(2005, 12, 26)
assert dates.DateTimeFormat(d, locale='de_DE')['w'] == '52'
assert dates.DateTimeFormat(d, locale='en_US')['w'] == '53'

def test_week_of_year_de_first_us_last_with_year(self):
d = date(2018, 12, 31)
fmt = dates.DateTimeFormat(d, locale='de_DE')
assert fmt['w'] == '1'
assert fmt['YYYY'] == '2019'
fmt = dates.DateTimeFormat(d, locale='en_US')
assert fmt['w'] == '53'
assert fmt['yyyy'] == '2018'
Expand Down Expand Up @@ -742,3 +730,15 @@ def test_en_gb_first_weekday():

def test_issue_798():
assert dates.format_timedelta(timedelta(), format='narrow', locale='es_US') == '0s'

# 200 years + 20 days / year (-10 +10) ~ 4000 tests
@pytest.mark.parametrize('date,weekyear,week', [
pytest.param(day, *day.isocalendar()[:2], id=str(day))
for year in range(1900, 2101)
# check +- 10 days around jan 1st
for d in range(-10, 11)
for day in [date(year, 1, 1) + timedelta(days=d)]
])
def test_iso_week_exhaustive(date, weekyear, week):
fmt = dates.DateTimeFormat(date, locale='de_DE')
assert (fmt['YYYY'], fmt['w']) == (str(weekyear), str(week))
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ deps =
allowlist_externals = make
commands = make clean-cldr test
setenv =
PYTEST_FLAGS=--cov=babel --cov-report=xml:{env:COVERAGE_XML_PATH:.coverage_cache}/coverage.{envname}.xml
PYTEST_FLAGS={env:PYTEST_FLAGS:} --cov=babel --cov-report=xml:{env:COVERAGE_XML_PATH:.coverage_cache}/coverage.{envname}.xml
passenv =
BABEL_*
PYTEST_*
Expand Down