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

RETURN_TIME_AS_PERIOD for relative times with hours/minutes/seconds #1044

Open
wants to merge 1 commit into
base: master
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
13 changes: 9 additions & 4 deletions dateparser/freshness_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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)

if date:
old_date = date
Expand All @@ -112,15 +112,20 @@ def apply_time(dateobj, timeobj):

return date, period

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

kwargs = self.get_kwargs(date_string)
if not kwargs:
return None, None
period = 'day'
if 'days' not in kwargs:
if settings.RETURN_TIME_AS_PERIOD:
for k in ['seconds', 'minutes', 'hours']:
if k in kwargs:
period = 'time'
break
if period != 'time' and 'days' not in kwargs:
for k in ['weeks', 'months', 'years']:
if k in kwargs:
period = k[:-1]
Expand All @@ -129,7 +134,7 @@ def _parse_date(self, date_string, now, prefer_dates_from):

if (
re.search(r'\bin\b', date_string)
or re.search(r'\bfuture\b', prefer_dates_from)
or re.search(r'\bfuture\b', settings.PREFER_DATES_FROM)
and not re.search(r'\bago\b', date_string)
):
date = now + td
Expand Down
6 changes: 6 additions & 0 deletions tests/test_freshness_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def now_with_timezone(self, tzinfo):
# English dates
param("yesterday", ago={'days': 1}, period='day'),
param("yesterday at 11:30", ago={'hours': 23}, period='time'),
param("2 days ago", ago={'days': 2}, period='day'),
param("48 hours ago", ago={'hours': 48}, period='time'),
param("today", ago={'days': 0}, period='day'),
param("now", ago={'seconds': 0}, period='time'),
param("4 weeks 2 hours ago", ago={'weeks': 4, 'hours': 2}, period='time'),
param("3 days 2 hours ago", ago={'days': 3, 'hours': 2}, period='time'),
])
def test_relative_past_dates_with_time_as_period(self, date_string, ago, period):
self.given_parser(settings={'NORMALIZE': False, 'RETURN_TIME_AS_PERIOD': True})
Expand Down