Refactor parse_datetime util.
authorJoshua Roesslein <jroesslein@gmail.com>
Sat, 17 Aug 2013 09:28:06 +0000 (02:28 -0700)
committerJoshua Roesslein <jroesslein@gmail.com>
Sat, 17 Aug 2013 09:28:06 +0000 (02:28 -0700)
- Avoid changing locale to parse timestamps.
- Use emails RFC 2822 parser.
- Added a test.

run_tests.sh
tests/test_utils.py [new file with mode: 0644]
tweepy/utils.py

index de2f210860125a2bfac1b2fac9a5ec905eeb60a8..d296c36b29e69a826bf0431bb0b1f5268cf57da6 100755 (executable)
@@ -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 (file)
index 0000000..c2ec51e
--- /dev/null
@@ -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)
+
index 46e5ac13169c084e201a520c59612762bb5ce2e4..6f47f36904de3ec20c67c362f77b75427bcf53be 100644 (file)
@@ -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):