[utils] improve to verify date format

This commit is contained in:
kikuyan 2021-07-17 18:10:46 +09:00
parent a803582717
commit a58f25bc3b

View File

@ -3081,7 +3081,15 @@ 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()
if re.match(r'^\d{8}$', date_str):
try:
return datetime.datetime.strptime(date_str, '%Y%m%d').date()
except ValueError as err:
errmsg = 'Invalid date: %s (%s)' % (date_str, err)
else:
errmsg = 'Invalid date format: %s (must be 8 digit number)' % date_str
raise ValueError(errmsg)
def hyphenate_date(date_str):