Skip to content

Instantly share code, notes, and snippets.

@codehack
codehack / iso8601.py
Created June 26, 2014 19:29
ISO8601 date time parser for Python using regex package
#!/usr/bin/python
import re
# i think i missed a couple of things in 8601 but this should cover 98% of cases.
iso8601 = re.compile(r'^(?P<full>((?P<year>\d{4})([/-]?(?P<mon>(0[1-9])|(1[012]))([/-]?(?P<mday>(0[1-9])|([12]\d)|(3[01])))?)?(?:T(?P<hour>([01][0-9])|(?:2[0123]))(\:?(?P<min>[0-5][0-9])(\:?(?P<sec>[0-5][0-9]([\,\.]\d{1,10})?))?)?(?:Z|([\-+](?:([01][0-9])|(?:2[0123]))(\:?(?:[0-5][0-9]))?))?)?))$')
# to perform the actual date match
m = iso8601.match('2014-12-05T12:30:45.123456-05:30')