From bcdc797058ae2ed82f01da9adba5f1d485f24125 Mon Sep 17 00:00:00 2001 From: armando2 Date: Thu, 9 Nov 2017 15:13:16 +0800 Subject: [PATCH 1/2] manually check for unsupported datetime formats and raise parseerror accordingly --- arrow/parser.py | 9 +++++++++ tests/parser_tests.py | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/arrow/parser.py b/arrow/parser.py index e4f368ad5..af27ed58d 100644 --- a/arrow/parser.py +++ b/arrow/parser.py @@ -101,6 +101,7 @@ def parse_iso(self, string): formats = ['YYYY-MM-DDTHH:mm:ss'] else: formats = ['YYYY-MM-DDTHH:mm'] + else: has_tz = False # generate required formats: YYYY-MM-DD, YYYY-MM-DD, YYYY @@ -109,6 +110,14 @@ def parse_iso(self, string): formats = [separator.join(self.MARKERS[:l-i]) for i in range(l) for separator in self.SEPARATORS] + date_string = string + + for separator in self.SEPARATORS: + date_components = date_string.split(separator) + if len(date_components) == 1: + continue + if date_components[len(date_components) - 1] > date_components[0]: + raise ParserError('Could not match input to any of {} on {}'.format(formats, string)) if has_time and has_tz: formats = [f + 'Z' for f in formats] diff --git a/tests/parser_tests.py b/tests/parser_tests.py index 7682df8c3..f59086837 100644 --- a/tests/parser_tests.py +++ b/tests/parser_tests.py @@ -236,6 +236,10 @@ def test_parse_subsecond(self): assertEqual(self.parser.parse('2013-01-01 12:30:45.987654', 'YYYY-MM-DD HH:mm:ss.SSSSSS'), expected) assertEqual(self.parser.parse_iso('2013-01-01 12:30:45.987654'), expected) + def test_parse_unsupported_iso(self): + with assertRaises(ParserError): + self.parser.parse_iso('03.04.2017') + def test_parse_subsecond_rounding(self): expected = datetime(2013, 1, 1, 12, 30, 45, 987654) format = 'YYYY-MM-DD HH:mm:ss.S' From 1339553678a6bd62c04af70ef70223b58fbce0bb Mon Sep 17 00:00:00 2001 From: armando2 Date: Thu, 9 Nov 2017 15:22:22 +0800 Subject: [PATCH 2/2] make error message work for python 2.6 --- arrow/parser.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arrow/parser.py b/arrow/parser.py index af27ed58d..498f42854 100644 --- a/arrow/parser.py +++ b/arrow/parser.py @@ -101,7 +101,6 @@ def parse_iso(self, string): formats = ['YYYY-MM-DDTHH:mm:ss'] else: formats = ['YYYY-MM-DDTHH:mm'] - else: has_tz = False # generate required formats: YYYY-MM-DD, YYYY-MM-DD, YYYY @@ -117,7 +116,7 @@ def parse_iso(self, string): if len(date_components) == 1: continue if date_components[len(date_components) - 1] > date_components[0]: - raise ParserError('Could not match input to any of {} on {}'.format(formats, string)) + raise ParserError('Could not match input to any of {0} on \'{1}\''.format(formats, string)) if has_time and has_tz: formats = [f + 'Z' for f in formats]