Skip to content

Commit

Permalink
Consider RETURN_TIME_AS_PERIOD for timestamp times (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
onlynone authored May 25, 2021
1 parent 255c421 commit 803d445
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit 803d445

Please sign in to comment.