0

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 ?

Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
  • 1
    may this be what your'e looking for ? https://stackoverflow.com/questions/9507648/datetime-from-string-in-python-best-guessing-string-format – Valentin C Jun 15 '21 at 09:52
  • 1
    Nit-pick: you're overwriting the built-in `hash` function ;-) And a side note on dateutil; it's very convenient but comes at a cost: performance. – FObersteiner Jun 15 '21 at 11:18
  • @MrFuppes Thanks. I wasn't aware there was a built-in `hash` function in Python. I should choose a different name then :) Good point with the performance as well. That might become an issue. But without this `dateutil` library, I am not sure how to generally catch all the timestamps and convert. Any ideas ? – Gustav Rasmussen Jun 15 '21 at 11:22
  • 1
    I guess you'll have to decide for your use-case. The more specific you make the format check & validation, the more you can optimize for performance. For your example, I think a compiled regex + validation via [datetime.fromisoformat](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat) will be a performant match (or even without the re), but the accepted formats pretty limited. – FObersteiner Jun 15 '21 at 12:06

2 Answers2

0

I think you can use the strptime() function for converting a string to datetime by specifying the string format.

from datetime import datetime

datetime_str = '2021-05-12 11:22:14'

datetime_object = datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S')
print(datetime_object)
0

Following the post datetime from string in Python, best-guessing string format suggested in the comments from @Valentin C, I got the general datetime conversion from strings that I was looking for. After a quick pip install python-dateutil, I was able to parse the strings correctly:

Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
>>> import dateutil.parser
>>> my_date = dateutil.parser.parse('2020-02-03 11:31:24')
>>> my_date
datetime.datetime(2020, 2, 3, 11, 31, 24)

Also, dateutil.parser breaks when attempting to parse non-timestamp-like strings as desired.

Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53