Skip to content

Instantly share code, notes, and snippets.

@bradwright
Created April 27, 2009 14:36
Show Gist options
  • Save bradwright/102517 to your computer and use it in GitHub Desktop.
Save bradwright/102517 to your computer and use it in GitHub Desktop.
def iso8601_to_datetime(iso_date):
"""Converts an ISO-8601 date string to a Python datetime.
We ignore microseconds for the moment as datetime only takes milliseconds.
Usage: >>> iso8601_to_datetime('2009-04-24T15:48:26,000000Z')
datetime.datetime(2009, 04, 24, 15, 48, 26)
"""
rx = re.compile(r'^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}),?(.*)$')
matches = rx.match(iso_date)
if matches:
match_bits = [int(i) for i in matches.groups()[:6]]
return datetime.datetime(*match_bits)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment