Skip to content

Commit

Permalink
Fix week period to string
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Dec 2, 2022
1 parent d9fe056 commit 15eef83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 16 additions & 4 deletions openfisca_core/periods/period_.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,31 @@ def __str__(self) -> str:

# 1 week
if (unit == DateUnit.WEEK and size == 1):
return f"{c_year}-W0{week}"
if week < 10:
return f"{c_year}-W0{week}"

return f"{c_year}-W{week}"

# several weeks
if (unit == DateUnit.WEEK and size > 1):
return f"{unit}:{c_year}-W0{week}:{size}"
if week < 10:
return f"{unit}:{c_year}-W0{week}:{size}"

return f"{unit}:{c_year}-W{week}:{size}"

# 1 weekday
if (unit == DateUnit.WEEKDAY and size == 1):
return f"{c_year}-W0{week}-{weekday}"
if week < 10:
return f"{c_year}-W0{week}-{weekday}"

return f"{c_year}-W{week}-{weekday}"

# several weekdays
if (unit == DateUnit.WEEKDAY and size > 1):
return f"{unit}:{c_year}-W0{week}-{weekday}:{size}"
if week < 10:
return f"{unit}:{c_year}-W0{week}-{weekday}:{size}"

return f"{unit}:{c_year}-W{week}-{weekday}:{size}"

# complex period
return f'{unit}:{f_year}-{month:02d}:{size}'
Expand Down
8 changes: 4 additions & 4 deletions openfisca_core/periods/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_str_with_days(date_unit, instant, size, expected):


@pytest.mark.parametrize("date_unit, instant, size, expected", [
[DateUnit.WEEK, Instant((2022, 1, 1)), 1, "2021-W052"],
[DateUnit.WEEK, Instant((2022, 1, 1)), 3, "week:2021-W052:3"],
[DateUnit.WEEK, Instant((2022, 1, 1)), 1, "2021-W52"],
[DateUnit.WEEK, Instant((2022, 1, 1)), 3, "week:2021-W52:3"],
[DateUnit.WEEK, Instant((2022, 3, 1)), 1, "2022-W09"],
[DateUnit.WEEK, Instant((2022, 3, 1)), 3, "week:2022-W09:3"],
])
Expand All @@ -44,8 +44,8 @@ def test_str_with_weeks(date_unit, instant, size, expected):


@pytest.mark.parametrize("date_unit, instant, size, expected", [
[DateUnit.WEEKDAY, Instant((2022, 1, 1)), 1, "2021-W052-6"],
[DateUnit.WEEKDAY, Instant((2022, 1, 1)), 3, "weekday:2021-W052-6:3"],
[DateUnit.WEEKDAY, Instant((2022, 1, 1)), 1, "2021-W52-6"],
[DateUnit.WEEKDAY, Instant((2022, 1, 1)), 3, "weekday:2021-W52-6:3"],
[DateUnit.WEEKDAY, Instant((2022, 3, 1)), 1, "2022-W09-2"],
[DateUnit.WEEKDAY, Instant((2022, 3, 1)), 3, "weekday:2022-W09-2:3"],
])
Expand Down

0 comments on commit 15eef83

Please sign in to comment.