Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
use timelib to parse string datetime reps
Browse files Browse the repository at this point in the history
- timelib seems to be based on PHP's strtodate, which is way more
  flexible than python-dateutil's parser.parse() method
  • Loading branch information
funkatron committed Apr 16, 2013
1 parent cfc3648 commit cf9f566
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -9,6 +9,7 @@ nose==1.3.0
pyOpenSSL==0.13
pymongo==2.5
python-dateutil==2.1
timelib==0.2.4
w3lib==1.2
wsgiref==0.1.2
zope.interface==4.0.5
6 changes: 3 additions & 3 deletions scrapy_proj/openrecipes/util.py
@@ -1,5 +1,5 @@
import isodate
from dateutil.parser import parse
import timelib
from scrapy import log
import bleach

Expand Down Expand Up @@ -45,7 +45,7 @@ def get_isodate(date_str):
except isodate.ISO8601Error, e:
# if not, try to parse it
try:
iso_date = isodate.date_isoformat(parse(date_str))
iso_date = isodate.date_isoformat(timelib.strtodatetime(date_str))
except Exception, e:
log.msg(e.message, level=log.WARNING)
return None
Expand All @@ -67,7 +67,7 @@ def get_isoduration(date_str):
except isodate.ISO8601Error, e:
# if not, try to parse it
try:
delta = (parse(date_str) - parse(''))
delta = (timelib.strtodatetime(date_str) - timelib.strtodatetime('now'))
iso_duration = isodate.duration_isoformat(delta)
except Exception, e:
log.msg(e.message, level=log.WARNING)
Expand Down
3 changes: 3 additions & 0 deletions scrapy_proj/tests/util_tests.py
Expand Up @@ -23,6 +23,9 @@ def test_parse_isoduration_2(self):
delta = datetime.timedelta(minutes=15)
self.assertEqual(delta, parse_isoduration('PT15M'))

def test_parse_isoduration_invalid(self):
self.assertEqual(None, parse_isoduration('PT1HPT1H'))


class StripHTMLTests(unittest.TestCase):

Expand Down

0 comments on commit cf9f566

Please sign in to comment.