From: Joshua Roesslein Date: Sat, 17 Aug 2013 09:28:06 +0000 (-0700) Subject: Refactor parse_datetime util. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a5bae83eb60ff1b478c4a39cbd29ffe12155abc8;p=tweepy.git Refactor parse_datetime util. - Avoid changing locale to parse timestamps. - Use emails RFC 2822 parser. - Added a test. --- diff --git a/run_tests.sh b/run_tests.sh index de2f210..d296c36 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,7 +1,7 @@ #! /usr/bin/env bash if [[ $TRAVIS_SECURE_ENV_VARS == "false" ]]; then - USE_REPLAY=1 nosetests -v tests.test_api + USE_REPLAY=1 nosetests -v tests.test_api tests.test_utils else - nosetests -v tests.test_api tests.test_streaming tests.test_cursors + nosetests -v tests.test_api tests.test_streaming tests.test_cursors tests.test_utils fi diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..c2ec51e --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,10 @@ +from unittest import TestCase + +from tweepy.utils import * + +class TweepyUtilsTests(TestCase): + + def testparse_datetime(self): + result = parse_datetime("Wed Aug 27 13:08:45 +0000 2008") + self.assertEqual(datetime(2008, 8, 27, 13, 8, 45), result) + diff --git a/tweepy/utils.py b/tweepy/utils.py index 46e5ac1..6f47f36 100644 --- a/tweepy/utils.py +++ b/tweepy/utils.py @@ -8,19 +8,11 @@ import htmlentitydefs import re import locale from urllib import quote +from email.utils import parsedate def parse_datetime(string): - # Set locale for date parsing - locale.setlocale(locale.LC_TIME, 'C') - - # We must parse datetime this way to work in python 2.4 - date = datetime(*(time.strptime(string, '%a %b %d %H:%M:%S +0000 %Y')[0:6])) - - # Reset locale back to the default setting - locale.setlocale(locale.LC_TIME, '') - return date - + return datetime(*(parsedate(string)[:6])) def parse_html_value(html):