Fast ISO datetime format parsing in python

19 Feb 2010

Python is missing a method to parse a ISO format datetime. Here's the fastest way I've found:

# PUBLIC DOMAIN.  SEASON TO TASTE.  ALWAYS TEST.
from datetime import datetime
def isoparse(s):
    try:
        return datetime(int(s[0:4]),int(s[5:7]),int(s[8:10]),
                        int(s[11:13]), int(s[14:16]), int(s[17:19]))
    except:
        return None

This "technique" can be used for other fixed-sized time formats.

Here's some crappier ways, followed by some timing results:

# similar to what is used in MySQLdb
def isoparse_mysql(s):
    if ' ' in s:
        sep = ' '
    elif 'T' in s:
        sep = 'T'
    else:
        return None

    try:
        d, t = s.split(sep, 1)
        return datetime(*[ int(x) for x in d.split('-')+t.split(':') ])
    except:
        return None

# Using Strptime
def isoparse_strptime(s):
    try:
        return datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
    except:
        return None
$ python isoformat_parse.py 
Version1 mysql: 11.59
Version2 strptime: 50.63
Version3 fixed: 7.48