Dates now print cleanly in local timezone. Probably breaks around DST changes, but oh well; python sucks at time localization

This commit is contained in:
Anna 2010-04-08 11:31:58 -04:00
parent 9e1d7b0630
commit 21fcf25544

View File

@ -2,7 +2,7 @@
# #
# Custom twitter client... mostly for learning Python # 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: class TwitWindow:
@ -79,6 +79,9 @@ class TwitWindow:
def update_window(self) : 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) statuses = self.api.GetFriendsTimeline(self.username, count=self.num_entries)
for i in range(0, self.num_entries): for i in range(0, self.num_entries):
if i < len(statuses): if i < len(statuses):
@ -87,10 +90,11 @@ class TwitWindow:
# fixme: need to find a way to detect local timezone and print in that # 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? # 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") timestamp = datetime.datetime.strptime(statuses[i].created_at, "%a %b %d %H:%M:%S +0000 %Y")
# timestring = timestamp.strftime() 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) self.labels[i].config(text=labeltext)
# Display the text of the tweet # Display the text of the tweet