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

Enhancement: add support 'last year' and 'last month' dateparsing #112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/source/dates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ With the ``DateParserPlugin``, users can use date queries such as::
-1 week to now
now to +2h
-1y6mo to +2 yrs 23d
last year

Normally, as with other types of queries containing spaces, the users need
to quote date queries containing spaces using single quotes::
Expand Down
8 changes: 8 additions & 0 deletions src/whoosh/qparser/dateparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,12 @@ def yesterday_to_date(p, dt):
thismonth = Regex(
"this month", lambda p, dt: adatetime(year=dt.year, month=dt.month)
)

lastyear = Regex("last year", lambda p, dt: adatetime(year=dt.year - 1))
lastmonth = Regex(
"last month", lambda p, dt: adatetime(year=dt.year, month=dt.month - 1)
)

today = Regex(
"today", lambda p, dt: adatetime(year=dt.year, month=dt.month, day=dt.day)
)
Expand Down Expand Up @@ -767,6 +773,8 @@ def yesterday_to_date(p, dt):
yesterday,
thisyear,
thismonth,
lastyear,
lastmonth,
today,
now,
),
Expand Down
2 changes: 2 additions & 0 deletions tests/test_dateparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def test_dmy(d=english.dmy):
assert_adatetime(d.date_from("yesterday", basedate), year=2010, month=9, day=19)
assert_adatetime(d.date_from("this month", basedate), year=2010, month=9)
assert_adatetime(d.date_from("this year", basedate), year=2010)
assert_adatetime(d.date_from("last month", basedate), year=2010, month=8)
assert_adatetime(d.date_from("last year", basedate), year=2009)

assert d.date_from("now", basedate) == basedate

Expand Down