gfcapalbo / python-twitter

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

Status relative time not working correctly (daylight saving issue) #10

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Here in France, a new status is reported to have been created one hour late.

As I get a server error when I try to attach a file, here is a quick patch
against 0.5 to correct this :

Index: twitter.py
===================================================================
@@ -92,7 +92,8 @@
     Returns:
       The time this status message was posted, in seconds since the epoch.
     '''
-    return time.mktime(time.strptime(self.created_at, '%a %b %d %H:%M:%S
+0000 %Y'))
+    #added +' GMT' to avaoid daylight saving errors
+    return time.mktime(time.strptime(self.created_at+' GMT', '%a %b %d
%H:%M:%S +0000 %Y'))

   created_at_in_seconds = property(GetCreatedAtInSeconds,
                                    doc="The time this status message was "

Original issue reported on code.google.com by pierrejean.coudert on 14 Sep 2007 at 5:18

GoogleCodeExporter commented 9 years ago
Sorry, the last line should be
+    return time.mktime(time.strptime(self.created_at+' GMT', '%a %b %d %H:%M:%S
+0000 %Y %Z'))

Original comment by pierrejean.coudert on 14 Sep 2007 at 6:16

GoogleCodeExporter commented 9 years ago

Original comment by dclinton on 15 Sep 2007 at 7:15

GoogleCodeExporter commented 9 years ago
the above fix is incorrect.

instead, simply replace 'time.mktime' with 'calendar.timegm' in the original 
code.

Original comment by Stelmina...@gmail.com on 13 Mar 2008 at 1:32

GoogleCodeExporter commented 9 years ago
you'll also need to do the same for the relevant tests

Original comment by Stelmina...@gmail.com on 13 Mar 2008 at 1:40

GoogleCodeExporter commented 9 years ago
attached a patch that keeps all timestamps in UTC.  This eliminates any need to 
deal
with DST.

Original comment by Stelmina...@gmail.com on 13 Mar 2008 at 2:06

Attachments:

GoogleCodeExporter commented 9 years ago
Looking at the patch right now.  Thanks!

Original comment by dclinton on 13 Mar 2008 at 2:10

GoogleCodeExporter commented 9 years ago
Patch applied in SVN trunk.   Please verify.

Original comment by dclinton on 13 Mar 2008 at 3:17

GoogleCodeExporter commented 9 years ago
revision 103 matched exactly with my working copy.

Original comment by Stelmina...@gmail.com on 13 Mar 2008 at 3:53

GoogleCodeExporter commented 9 years ago
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 comment by Stelmina...@gmail.com on 8 Aug 2008 at 5:31