Created
April 18, 2009 09:05
-
-
Save bradwright/97517 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 parse_date(date): | |
"""parse out non-standard date format used by Twitter | |
Usage: parse_date("Sun Jul 13 12:44:07 +0000 2008") | |
>>> datetime.datetime(2008, 06, 13, 12, 44, 07) | |
""" | |
date_regex = re.compile(r""" | |
^ | |
(?P<day_name>[a-zA-Z]+) | |
\s+ | |
(?P<month_name>[a-zA-Z]+) | |
\s+ | |
(?P<day>\d+) | |
\s+ | |
(?P<hours>\d+) | |
: | |
(?P<minutes>\d+) | |
: | |
(?P<seconds>\d+) | |
\s+ | |
\+\d+ | |
\s+ | |
(?P<year>\d+) | |
$ | |
""", re.VERBOSE) | |
if date_regex.match(date): | |
date_string = date_regex.sub(r'\g<year> \g<month_name> \g<day> \g<hours>:\g<minutes>:\g<seconds>', date) | |
return datetime.datetime(*time.strptime(date_string, '%Y %b %d %H:%M:%S')[:6]) | |
else: | |
raise MalformedDate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment