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

raise ParserError if year, month, or date is not found in string #482

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
7 changes: 5 additions & 2 deletions arrow/parser.py
Expand Up @@ -180,7 +180,7 @@ def parse(self, string, fmt):
else:
value = match.group(token)
self._parse_token(token, value, parts)
return self._build_datetime(parts)
return self._build_datetime(parts, fmt_pattern_re.pattern, string)

def _parse_token(self, token, value, parts):

Expand Down Expand Up @@ -247,7 +247,7 @@ def _parse_token(self, token, value, parts):
parts['am_pm'] = 'pm'

@staticmethod
def _build_datetime(parts):
def _build_datetime(parts, pattern, string):

timestamp = parts.get('timestamp')

Expand All @@ -263,6 +263,9 @@ def _build_datetime(parts):
elif am_pm == 'am' and hour == 12:
hour = 0

if not parts.get('year') or not parts.get('month') or not parts.get('day'):
raise ParserError('Could not match input to any of {0} on \'{1}\''.format(pattern, string))

return datetime(year=parts.get('year', 1), month=parts.get('month', 1),
day=parts.get('day', 1), hour=hour, minute=parts.get('minute', 0),
second=parts.get('second', 0), microsecond=parts.get('microsecond', 0),
Expand Down
4 changes: 4 additions & 0 deletions tests/parser_tests.py
Expand Up @@ -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'
Expand Down