handle wrong formatted date argument

The provided date argument (--date, --datebefore or --dateafter) is parsed by
datetimp.strptime(). This function throws as ValueError exception at failure.

This patch implements exception handling by catching the ValueError exception,
printing an error message and exiting with a non-zero exit code.
This commit is contained in:
Franz König 2018-05-21 01:20:05 +02:00
parent 504f20dd30
commit 4d64bc5251
1 changed files with 4 additions and 1 deletions

View File

@ -1270,7 +1270,10 @@ def date_from_str(date_str):
unit += 's'
delta = datetime.timedelta(**{unit: time})
return today + delta
return datetime.datetime.strptime(date_str, '%Y%m%d').date()
try:
return datetime.datetime.strptime(date_str, '%Y%m%d').date()
except ValueError:
sys.exit('ERROR: cannot parse datestring ' + date_str)
def hyphenate_date(date_str):