python-dateutil
It seems python-dateutil is the Python module that can bring some sanity to time handling in Python. I’ve finally found a module that can these two tasks:
- parse dates in several formats
- provide machinery to make the standard datetime talk timezones
For the first item I was using mx.DateTime, but it seems dateutil’s parser is very good too.
The second item has been bugging me for a long time. You have the datetime module in the standard library providing nice object-oriented interfaces to date and time, but what good it is if you can’t even use it to print a date in RFC822 format!
At least now it’s possible:
from datetime import datetime
from dateutil.tz import gettz
print datetime.now(tz=gettz()).strftime('... %z %Z')
(Oh, sure, that simple task can be done directly with the time module. But if you’re dealing with HTML templates, you really want to pass date objects around, and let the templates print them one or more times in whatever format they need, including %z or %Z. And with dateutil you can use any timezone, of course, not just the current one.)
The dateutil module also offers interfaces to do relative time calculations (what date will it be in 47 days?), and some stuff for recurrence rules. Check it out!