Created
April 27, 2009 14:36
-
-
Save bradwright/102517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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