diff --git a/arrow/parser.py b/arrow/parser.py index df8cc1a47..9eadaf2b2 100644 --- a/arrow/parser.py +++ b/arrow/parser.py @@ -165,6 +165,19 @@ def _generate_pattern_re(self, fmt): return tokens, re.compile(final_fmt_pattern, flags=re.IGNORECASE) + def check_incorrect_year_placement(self, string, fmt): + """ + Determines wheather a year (YYYY) is incorrectly formatted. + """ + if fmt == 'YYYY-MM-DD' or fmt == 'YYYY/MM/DD' or fmt == 'YYYY.MM.DD': + # Ensuring that the 3rd and 4th number of a date is a number + # instead of a '-', '/', or '.' + if not string[2].isdigit() or not string[3].isdigit(): + return True + return False + else: + return False + def parse(self, string, fmt): if isinstance(fmt, list): @@ -280,7 +293,8 @@ def _parse_multiformat(self, string, formats): _datetime = self.parse(string, fmt) break except ParserError: - pass + if self.check_incorrect_year_placement(string, fmt): + break if _datetime is None: raise ParserError('Could not match input to any of {0} on \'{1}\''.format(formats, string)) diff --git a/tests/parser_tests.py b/tests/parser_tests.py index a179be4e6..d666d9bdc 100644 --- a/tests/parser_tests.py +++ b/tests/parser_tests.py @@ -375,6 +375,32 @@ def test_YYYY_MM_DD(self): self.parser.parse_iso(separator.join(('2013', '02', '03'))), datetime(2013, 2, 3) ) + + def test_incorrect_year_placement(self): + """ + Tests when year is in the wrong slot. + """ + + with assertRaises(ParserError): + self.parser.parse_iso('02-2018-03') + + with assertRaises(ParserError): + self.parser.parse_iso('02-03-2018') + + with assertRaises(ParserError): + self.parser.parse_iso('02/2018/03') + + with assertRaises(ParserError): + self.parser.parse_iso('02/03/2018') + + with assertRaises(ParserError): + self.parser.parse_iso('02.2018.03') + + with assertRaises(ParserError): + self.parser.parse_iso('02.03.2018') + + + def test_YYYY_MM_DDTHH_mmZ(self):