From 21fcf255440a22344efaebe68d6ca894ced08aac Mon Sep 17 00:00:00 2001 From: Anna Date: Thu, 8 Apr 2010 11:31:58 -0400 Subject: [PATCH] Dates now print cleanly in local timezone. Probably breaks around DST changes, but oh well; python sucks at time localization --- mytwitter.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mytwitter.py b/mytwitter.py index 63d1b79..7cb2d5b 100755 --- a/mytwitter.py +++ b/mytwitter.py @@ -2,7 +2,7 @@ # # Custom twitter client... mostly for learning Python -import sys, twitter, Tkinter, Pmw, ConfigParser, os, tkMessageBox, datetime +import sys, twitter, Tkinter, Pmw, ConfigParser, os, tkMessageBox, datetime, dateutil.tz class TwitWindow: @@ -79,6 +79,9 @@ class TwitWindow: def update_window(self) : + timezone = dateutil.tz.gettz() + time_format = "%Y.%m.%d %H:%M:%S %Z" + statuses = self.api.GetFriendsTimeline(self.username, count=self.num_entries) for i in range(0, self.num_entries): if i < len(statuses): @@ -87,10 +90,11 @@ class TwitWindow: # fixme: need to find a way to detect local timezone and print in that # Also need to decide how to format the output... maybe use a config var? -# timestamp = datetime.datetime.strptime(statuses[i].created_at, "%a %b %d %H:%M:%S %z %Y") -# timestring = timestamp.strftime() + timestamp = datetime.datetime.strptime(statuses[i].created_at, "%a %b %d %H:%M:%S +0000 %Y") + timestamp = timestamp.replace(tzinfo=dateutil.tz.gettz('UTC')) + timestring = timestamp.astimezone(timezone).strftime(time_format) - labeltext = user.name + " (" + user.screen_name + ") " + statuses[i].created_at + labeltext = user.name + " (" + user.screen_name + ") " + timestring self.labels[i].config(text=labeltext) # Display the text of the tweet