A follow on from my previous “cool things about Python”, there’s another cool thing you might not have known about. Almost all python packages are available at the Cheeseshop in Python EGG files (geddit?), and the easiest way to install them is using setuptools‘s ez_setup.py bootstrap script.
python ez_setup.py pytz
Within a very short amount of time you’ll have pytz ready and available to import and use.
Incidentally, pytz is a pretty damn cool little timezone library — if you’ve ever had to do any manipulation of dates and times across daylight saving times or time zone boundaries in Python, I recommend you take a look.
Quick caveat on using it though — to create a local time from a “non-localised”
time, don’t be tempted to just construct a default
datetime(blahblah, tzinfo=myTimeZone). Instead
make sure you use the
localize() method of the timezone. This will then take into account daylight
saving time boundaries appropriately. I use something like:
myTimeZone = pytz.timezone('Europe/London') nonLocal = datetime(2007, 10, 1, 1, 18, 0) local = myTimeZone.localize(nonLocal, is_dst=None)
is_dst=None part means in the ambiguous hour as the clocks go back, raise an exception. The other
False to break the ambiguity by assuming it was DST or not.