Hi, I understand you added this module as a way to make datetime parsing faster but with the caveat that we only deal with one input format. I was looking for a fast module that does the same thing as datetime to convert from multiple string formats.. unfortunately, I cannot control the input formats in this case.
I stumbled upon Arrow but noticed its very slow. Since this is not a straight drop in from datetime would you be able to point me in the right direction to speedup ? Thanks
PS. Here is what I want to achieve, sadly I cannot with udatetime
STR_KNOWN_DATE_FORMATS = ['%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', '%y/%m/%d']
STR_KNOWN_TIME_FORMATS = ['%I:%M:%S %p', '%H:%M:%S', '%I:%M %p', '%H:%M %p',
'%I:%M%p', '%H:%M',
'%I:%M:%S%p']
# Known date and time formats, all combos
STR_KNOWN_DATETIME_FORMATS = []
for kd in STR_KNOWN_DATE_FORMATS:
for kt in STR_KNOWN_TIME_FORMATS:
STR_KNOWN_DATETIME_FORMATS.append('{} {}'.format(kd, kt))
STR_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
def date_and_time(given_date, given_time):
"""
Normalize date and time values
:param given_date: str, iso date i.e. 2016-03-01 (YYYY-MM-DD)
:param given_time: str, iso time or None i.e 23:33:44 (HH:mm:ss)
:return: str, date and time (YYYY-MM-DD HH:mm:ss)
"""
# Remove spaces
given_time = given_time.strip()
given_date = given_date.strip()
# Join the date and time
t_date = given_date + ' ' + given_time
event_date = None
for datetime_fmt in STR_KNOWN_DATETIME_FORMATS:
try:
event_date = datetime.datetime.strptime(t_date, datetime_fmt).strftime(STR_DATETIME_FORMAT)
break
except ValueError:
continue
except Exception as e: # pragma: no cover
logging.exception("date_and_time unhandled exception for date {}, format {}. {}"
"".format(t_date, datetime_fmt, e))
continue
# Unable to find the format given, we may need to add a new format
if not event_date:
logging.error("date_and_time unable to parse date '{}', time '{}'".format(given_date, given_time))
return event_date
Hi, I understand you added this module as a way to make datetime parsing faster but with the caveat that we only deal with one input format. I was looking for a fast module that does the same thing as datetime to convert from multiple string formats.. unfortunately, I cannot control the input formats in this case.
I stumbled upon Arrow but noticed its very slow. Since this is not a straight drop in from datetime would you be able to point me in the right direction to speedup ? Thanks
PS. Here is what I want to achieve, sadly I cannot with udatetime