Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions dateparser/freshness_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
class FreshnessDateDataParser:
"""Parses date string like "1 year, 2 months ago" and "3 hours, 50 minutes ago" """

def _are_all_words_units(self, date_string):
skip = [_UNITS, r"ago|in|\d+", r":|[ap]m"]
def _are_all_words_units(self, date_string, has_time):
skip = [_UNITS, r"ago|in", r":|[ap]m"]
if has_time:
skip.append(r"\d+")

date_string = PATTERN.sub("", date_string)
date_string = re.sub(r"\s+", " ", date_string.strip())

words = [x for x in re.split(r"\W", date_string) if x]
Expand Down Expand Up @@ -88,7 +91,9 @@ def apply_time(dateobj, timeobj):
else:
now = datetime.now(self.get_local_tz())

date, period = self._parse_date(date_string, now, settings.PREFER_DATES_FROM)
date, period = self._parse_date(
date_string, now, settings.PREFER_DATES_FROM, _time is not None
)

if date:
old_date = date
Expand All @@ -108,8 +113,8 @@ def apply_time(dateobj, timeobj):

return date, period

def _parse_date(self, date_string, now, prefer_dates_from):
if not self._are_all_words_units(date_string):
def _parse_date(self, date_string, now, prefer_dates_from, has_time):
if not self._are_all_words_units(date_string, has_time):
return None, None

result = self.get_kwargs(date_string)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_freshness_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,9 @@ def test_relative_future_dates(self, date_string, in_future, period):
[
param("15th of Aug, 2014 Diane Bennett"),
param("4 heures ago"),
param("the 1st of last month"),
param("the 1st of this month"),
param("the 15th of next month"),
]
)
def test_insane_dates(self, date_string):
Expand Down
Loading