From c57789105a51630ab8a7d0caec2aca65864dcfc1 Mon Sep 17 00:00:00 2001 From: Waqas Shabir Date: Fri, 17 Jun 2016 00:48:32 +0500 Subject: [PATCH] More predictable strptime like functionality when using date_formats --- dateparser/date.py | 9 ++++++--- tests/test_date.py | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/dateparser/date.py b/dateparser/date.py index 3537300ed..a86656a1f 100644 --- a/dateparser/date.py +++ b/dateparser/date.py @@ -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) diff --git a/tests/test_date.py b/tests/test_date.py index 43b9c9ec3..8498dbb73 100644 --- a/tests/test_date.py +++ b/tests/test_date.py @@ -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)