Make jQuery time widget input even more flexible

e.g. allow any of:

* 01:30 PM
* 1:30pm
* 11 AM
* 11am
This commit is contained in:
Lance Edgar 2017-12-20 21:36:11 -06:00
parent 42b97d1e1a
commit e58ca10e25

View file

@ -37,11 +37,18 @@ class JQueryTime(colander.Time):
def deserialize(self, node, cstruct): def deserialize(self, node, cstruct):
if not cstruct: if not cstruct:
return colander.null return colander.null
try:
return colander.timeparse(cstruct, '%I:%M %p') formats = [
except ValueError: '%I:%M %p',
'%I:%M%p',
'%I %p',
'%I%p',
]
for fmt in formats:
try: try:
return colander.timeparse(cstruct, '%I:%M%p') return colander.timeparse(cstruct, fmt)
except: except ValueError:
# re-try first format, for "better" error pass
return colander.timeparse(cstruct, '%I:%M %p')
# re-try first format, for "better" error message
return colander.timeparse(cstruct, formats[0])