Skip to content

Commit

Permalink
Merge pull request #194 from scrapinghub/more-predictable-strptime-wi…
Browse files Browse the repository at this point in the history
…th-formats

More predictable strptime like functionality when using date_formats
  • Loading branch information
waqasshabbir authored Jun 16, 2016
2 parents 83a7b33 + c577891 commit 6f16f6b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions dateparser/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,13 @@ def get_date_data(self, date_string, date_formats=None):
{'date_obj': datetime.datetime(2000, 3, 23, 14, 21), 'period': 'day'}
"""
try:
date_string = date_string.strip()
except AttributeError:
if not(isinstance(date_string, six.text_type) or isinstance(date_string, six.string_types)):
raise TypeError('Input type must be str or unicode')

res = parse_with_formats(date_string, date_formats or [])
if res['date_obj']:
return res

if self._settings.NORMALIZE:
date_string = normalize_unicode(date_string)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ def test_get_date_data_should_not_strip_timezone_info(self, date_string):
param(date_string="14 giu 13", date_formats=["%y %B %d"], expected_result=datetime(2014, 6, 13)),
param(date_string="14_luglio_15", date_formats=["%y_%B_%d"], expected_result=datetime(2014, 7, 15)),
param(date_string="14_LUGLIO_15", date_formats=["%y_%B_%d"], expected_result=datetime(2014, 7, 15)),
param(date_string="02/12/2014 \xe0 15:08", date_formats=["%d/%m/%Y \xe0 %H:%M"], expected_result=datetime(2014, 12, 2, 15, 8)),
param(date_string="10.01.2016, 20:35", date_formats=["%d.%m.%Y, %H:%M"], expected_result=datetime(2016, 1, 10, 20, 35)),
])
def test_parse_date_using_format(self, date_string, date_formats, expected_result):
self.given_local_tz_offset(0)
Expand Down

0 comments on commit 6f16f6b

Please sign in to comment.