Skip to content
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
5 changes: 4 additions & 1 deletion dateparser/freshness_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ def get_kwargs(self, date_string):

if "decades" in kwargs:
kwargs["years"] = 10 * kwargs["decades"] + kwargs.get("years", 0)
if "decades" in explicit_signs:
# Only propagate the decades sign to years when years was not
# itself explicitly signed; prevents overwriting a user-supplied
# "+"/"-" on the years component (fixes #1304).
if "decades" in explicit_signs and not explicit_signs.get("years"):
explicit_signs["years"] = explicit_signs["decades"]
del kwargs["decades"]
explicit_signs.pop("decades", None)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_freshness_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def test_relative_past_dates_with_time_as_period(self, date_string, ago, period)
param("last decade", ago={"years": 10}, period="year"),
param("a decade ago", ago={"years": 10}, period="year"),
param("100 decades", ago={"years": 1000}, period="year"),
# Regression tests for #1304: explicit signs preserved with decades + years
param("-1 decade", ago={"years": 10}, period="year"),
param("-1 decade 2 years", ago={"years": 8}, period="year"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t the expectation be 12 here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wasn't sure about this.

param("-1 decade +2 years", ago={"years": 8}, period="year"),
param("yesterday", ago={"days": 1}, period="day"),
param("the day before yesterday", ago={"days": 2}, period="day"),
param("4 days before", ago={"days": 4}, period="day"),
Expand Down Expand Up @@ -1738,6 +1742,10 @@ def test_normalized_relative_dates(self, date_string, ago, period):
param("in 1 decade 12 years", in_future={"years": 22}, period="year"),
param("next decade", in_future={"years": 10}, period="year"),
param("in a decade", in_future={"years": 10}, period="year"),
# Regression tests for #1304: explicit signs preserved with decades + years
param("+2 years", in_future={"years": 2}, period="year"),
param("1 decade +2 years", in_future={"years": 12}, period="year"),
param("+1 decade -2 years", in_future={"years": 8}, period="year"),
param("tomorrow", in_future={"days": 1}, period="day"),
param("day after tomorrow", in_future={"days": 2}, period="day"),
param("after 4 days", in_future={"days": 4}, period="day"),
Expand Down
Loading