I wish to convert any string to datetime, that follows any datetime format, without attempting conversions of other strings like regular text. What I am doing now:
from datetime import datetime
import re
pattern = r"\d{4}[-/]\d{2}[-/]\d{2}\s\d{2}:\d{2}:\d{2}"
regex = re.compile(pattern)
def cast_to_dt(TEXT):
"""Match datetimes, and convert them."""
res = regex.match(TEXT)
if res is None:
return TEXT
print(res.group(0))
hash = {"-": "%Y-%m-%d %H:%M:%S", "/": "%Y/%m/%d %H:%M:%S"}
dt_obj = ""
if "-" in res.group(0):
dt_obj = datetime.strptime(res.group(0), hash["-"])
if "/" in res.group(0):
dt_obj = datetime.strptime(res.group(0), hash["/"])
print(dt_obj, type(dt_obj))
TEXT_10 = "2021-05-12 11:22:14"
TEXT_11 = "2021/05/12 11:22:14"
TEXT_20 = "I am definitely not a datetime"
cast_to_dt(TEXT_10)
cast_to_dt(TEXT_11)
cast_to_dt(TEXT_20)
My approach to set format based on the hash
dictionary will not be so readable once it starts growing. Is there a better approach to auto-detect or choose the format ?