gfcapalbo / python-twitter

Automatically exported from code.google.com/p/python-twitter
Apache License 2.0
0 stars 0 forks source link

Improved date parsing (locale problems + the fix) follow up to issue 10 #24

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
in reference to: http://code.google.com/p/python-twitter/issues/detail?id=10

Sorry for the confusion, but the current way to parse the date does not
work with alternate locale settings.

calendar.timegm(time.strptime(self.created_at, '%a %b %d %H:%M:%S +0000 %Y'))

Since twitter appears to be using rfc822 dates, parse them as such:

import rfc822
calendar.timegm(rfc822.parsedate(self.created_at))

Example of the problem:

>>> import time
>>> mytime = time.strftime('%a %b %d %H:%M:%S +0000 %Y')
>>> time.strptime(mytime, '%a %b %d %H:%M:%S +0000 %Y')
(2008, 8, 8, 13, 22, 37, 4, 221, -1)
>>> import locale
>>> locale.setlocale(locale.LC_ALL, ('Russian_Russia', '1251'))
'Russian_Russia.1251'
>>> time.strptime(mytime, '%a %b %d %H:%M:%S +0000 %Y')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "python\lib\_strptime.py", line 330, in strptime
    (data_string, format))
ValueError: time data did not match format:  data=Fri Aug 08 13:22:37 +0000
2008 
fmt=%a %b %d %H:%M:%S +0000 %Y
>>> import rfc822
>>> rfc822.parsedate(mytime)
(2008, 8, 8, 13, 22, 37, 0, 1, 0)

Original issue reported on code.google.com by Stelmina...@gmail.com on 14 Aug 2008 at 11:09

GoogleCodeExporter commented 9 years ago

Original comment by dclinton on 9 Sep 2008 at 4:05

GoogleCodeExporter commented 9 years ago
Finally fixed this in trunk in r131
(http://code.google.com/p/python-twitter/source/detail?r=131).  Please verify.

Original comment by dclinton on 21 Feb 2009 at 5:28

GoogleCodeExporter commented 9 years ago
"passdate" was wrong, should have been "parsedate"

I'm sorry, not sure how I managed that typo.

Original comment by Stelmina...@gmail.com on 21 Feb 2009 at 11:52