Skip to content

Commit

Permalink
Merge pull request #22 from pganssle/ampm_explicit_error
Browse files Browse the repository at this point in the history
Add explicit error checking along with AM/PM flag.
  • Loading branch information
pganssle committed Jan 31, 2015
2 parents 8796174 + 5f20a74 commit 6f30d8b
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dateutil/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,14 @@ def _parse(self, timestr, dayfirst=None, yearfirst=None, fuzzy=False,
# Check am/pm
value = info.ampm(l[i])
if value is not None:
# If AM/PM is found and hour is not, raise a ValueError
if res.hour is None:
raise ValueError('No hour specified with AM or PM flag.')

# If AM/PM is found, it's a 12 hour clock, so raise an error for invalid range
if not 0 <= res.hour <= 12:
raise ValueError('Invalid hour specified for 12-hour clock.')

if value == 1 and res.hour < 12:
res.hour += 12
elif value == 0 and res.hour == 12:
Expand Down

0 comments on commit 6f30d8b

Please sign in to comment.