Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No error raised on invalid dates #527

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion arrow/parser.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down
26 changes: 26 additions & 0 deletions tests/parser_tests.py
Expand Up @@ -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):

Expand Down