Feel free to add your favorite module, but so far we've got time, datetime, dateutil, pytz, and locale to deal with, as PEP-431 hasn't been implemented yet, giving us a uniform, concrete way of dealing with timezones and their associated oddities like ambiguous datetimes that occur twice during a daylight savings time switchover, or don't occur at all. At any rate, my question is fairly pedestrian, but I can't seem to find an answer.
The backstory: I'm developing a small plugin for Sublime Text that initially will allow the user to insert a timestamp (in a format they specify, falling back on a default) in the current document. Eventually it will update the timestamp upon saving, but that's not relevant here. Here's my short, self-contained example code:
import sublime_plugin
from datetime import datetime as dt
class TimeStampCommand(sublime_plugin.TextCommand):
def run(self, edit):
stamp = dt.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") # 2013-10-10 22:38:40 UTC
for r in self.view.sel():
if r.empty():
self.view.insert(edit, r.a, stamp)
else:
self.view.replace(edit, r, stamp)
From the list of formatting options, I should be able to add a %Z and get the local timezone:
stamp = dt.now().strftime("%Y-%m-%d %H:%M:%S %Z") # should be 2013-10-10 18:38:40 EDT
or modify it slightly and get a more universal UTC offset:
stamp = dt.now().strftime("%Y-%m-%d %H:%M:%S UTC %z") # should be 2013-10-10 18:38:40 UTC -0400
Unfortunately, this isn't the case, because the time returned by dt.now() is "naive" - it doesn't have any timezone information attached to it. I could use datetime.tzinfo, but unhelpfully this is only an abstract base class, since timezones are messy and complicated and change a lot. So, to the root of my question: How do I create an "aware" time/datetime/dateutil/whatever object that can take advantage of the full array of strftime() formatting options? I want users to be able to format the timestamp as they please, either according to their own locale (somehow I need to detect this) or according to a supplied one. Preferably I'd like it to work on Windows, Mac, and Linux (the Sublime Text supported platforms). Finally, if there are differences between Python 2.6 and 3.3 (the built-in interpreter's version for ST2 and ST3, respectively) I'd like to know about them.
Please note, I'm not asking you to write all the code for me, I want to learn this myself. I just need some good pointers to get going in the right direction, as I've been spinning my wheels for most of the day. At the absolute minimum, just figuring out how to generate 2013-10-10 18:38:40 UTC -0400 would be a great start, although the other stuff would be really nice too :)
%Zis deprecated? Besides the limitations of POSIX and Windows, a three-letter timezone is not a unique identifier—ASTis Atlantic Standard Time in Canada, Arabia Standard Time in the Middle-East, and Acre Standard Time in Brazil—that is, -4, +3, and -5, respectively.time.tzname[time.daylight]) if they really want to.