change date metadata parsing to dateutil.parser

This commit is contained in:
Stefan hr Berder 2014-02-16 12:51:52 +01:00
commit 7f2bc2a23b
4 changed files with 37 additions and 38 deletions

View file

@ -15,7 +15,7 @@ import traceback
from collections import Hashable
from contextlib import contextmanager
from datetime import datetime
import dateutil.parser
from functools import partial
from itertools import groupby
from jinja2 import Markup
@ -181,39 +181,10 @@ def get_date(string):
If no format matches the given date, raise a ValueError.
"""
string = re.sub(' +', ' ', string)
formats = [
# ISO 8601
'%Y',
'%Y-%m',
'%Y-%m-%d',
'%Y-%m-%dT%H:%M%z',
'%Y-%m-%dT%H:%MZ',
'%Y-%m-%dT%H:%M',
'%Y-%m-%dT%H:%M:%S%z',
'%Y-%m-%dT%H:%M:%SZ',
'%Y-%m-%dT%H:%M:%S',
'%Y-%m-%dT%H:%M:%S.%f%z',
'%Y-%m-%dT%H:%M:%S.%fZ',
'%Y-%m-%dT%H:%M:%S.%f',
# end ISO 8601 forms
'%Y-%m-%d %H:%M',
'%Y-%m-%d %H:%M:%S',
'%Y/%m/%d %H:%M',
'%Y/%m/%d',
'%d-%m-%Y',
'%d.%m.%Y %H:%M',
'%d.%m.%Y',
'%d/%m/%Y',
]
for date_format in formats:
try:
date = datetime.strptime(string, date_format)
except ValueError:
continue
if date_format.endswith('Z'):
date = date.replace(tzinfo=pytz.timezone('UTC'))
return date
raise ValueError('{0!r} is not a valid date'.format(string))
try:
return dateutil.parser.parse(string)
except (TypeError, ValueError):
raise ValueError('{0!r} is not a valid date'.format(string))
@contextmanager