Skip to content

Commit

Permalink
consistent week number parsing (#1807)
Browse files Browse the repository at this point in the history
* convert week parsing to match d3.js
* corrected the test to show difference between iso and %W week numbering
  • Loading branch information
mailhot001 authored Jun 22, 2024
1 parent 74c71d7 commit 134d40a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/fava/help/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ current year up to today, or `year-1 - year` for all entries of the last and
current year. To prevent subtraction, use parentheses: `(month)-10` refers to
the 10th of this month, whereas `month-10` would be 10 months ago.

**Week number** Week number of the year (Monday as the first day of the week) as
a decimal number. All days in a new year preceding the first Monday are
considered to be in week 0.

### Account

Filter entries by account, matching any entry this account is part of. The
Expand Down
6 changes: 5 additions & 1 deletion src/fava/util/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ def parse_date( # noqa: PLR0911
match = WEEK_RE.match(string)
if match:
year, week = map(int, match.group(1, 2))
start = datetime.date.fromisocalendar(year, week + 1, 1)
start = (
datetime.datetime.strptime(f"{year}-W{week}-1", "%Y-W%W-%w")
.replace(tzinfo=datetime.timezone.utc)
.date()
)
return start, get_next_interval(start, Interval.WEEK)

match = QUARTER_RE.match(string)
Expand Down
1 change: 1 addition & 0 deletions tests/test_util_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def test_fiscal_substitute(
("2010-10-01", "2010-11-01", "2010-10"),
("2000-01-03", "2000-01-04", "2000-01-03"),
("2015-01-05", "2015-01-12", "2015-W01"),
("2025-01-06", "2025-01-13", "2025-W01"),
("2015-04-01", "2015-07-01", "2015-Q2"),
("2014-01-01", "2016-01-01", "2014 to 2015"),
("2014-01-01", "2016-01-01", "2014-2015"),
Expand Down

0 comments on commit 134d40a

Please sign in to comment.