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

Consider RETURN_TIME_AS_PERIOD for timestamp times #922

Merged
merged 2 commits into from
May 25, 2021
Merged
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
2 changes: 1 addition & 1 deletion dateparser/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _parse(self):
def _try_timestamp(self):
return DateData(
date_obj=get_date_from_timestamp(self.date_string, self._settings),
period='day',
period='time' if self._settings.RETURN_TIME_AS_PERIOD else 'day',
)

def _try_freshness_parser(self):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,22 @@ def test_parse_timestamp(self, date_string, expected):
self.given_parser(settings={'TO_TIMEZONE': 'UTC'})
self.when_date_is_parsed(date_string)
self.then_date_obj_exactly_is(expected)
self.then_period_is('day')

@parameterized.expand([
# Epoch timestamps.
param('1484823450', expected=datetime(2017, 1, 19, 10, 57, 30)),
param('1436745600000', expected=datetime(2015, 7, 13, 0, 0)),
param('1015673450', expected=datetime(2002, 3, 9, 11, 30, 50)),
param('2016-09-23T02:54:32.845Z', expected=datetime(2016, 9, 23, 2, 54, 32, 845000,
tzinfo=StaticTzInfo('Z', timedelta(0))))
])
def test_parse_timestamp_with_time_as_period(self, date_string, expected):
self.given_local_tz_offset(0)
self.given_parser(settings={'TO_TIMEZONE': 'UTC', 'RETURN_TIME_AS_PERIOD': True})
self.when_date_is_parsed(date_string)
self.then_date_obj_exactly_is(expected)
self.then_period_is('time')

@parameterized.expand([
param('10 December', expected=datetime(2015, 12, 10), period='day'),
Expand Down