An overview of the capabilities of dateutil, a powerful set of extensions for Python's datetime library. Presented to ClePy on February 5, 2007, and at PyCon 2007 on February 24, 2007.
BlueMountain.com (3M+ users at launch) • Replace a third-party Perl+ASP app • More sophisticated recurrence • Hands-off maintenance of recurring dates • Handle time zones properly 2
Python’s datetime module • Recurrence: iCal (RFC 2445) and more • Time zones: internal and from Olson • Parse date strings in any format • Relative timedeltas 3
Comfortable, Pythonic use • Time zones work right (pytz DST issues) • Recurrence is a snap • Nice extras that I didn’t know I needed • No dependence on other products (Zope) 4
instance • Can include times, time zones, and more in the parsed string • Very helpful for making usable values from some data source (database, iCal file, etc.) 5
day, hour, minute, second, microsecond • Relative values: years, months, days, hours, minutes, seconds, microseconds • weekday: can specify the Nth weekday, e.g. MO(+3) is three Mondays forward >>> from dateutil.relativedelta import MO, TU, WE, TH, FR, SA, SU 11
zoneinfo.gettz('US/Central') >>> dt = datetime.now(zone) >>> dt.astimezone(zoneinfo.gettz('US/Eastern')) Use dateutil.zoneinfo module for direct access to compiled time zone information 14
activity • Many ways to get a tzinfo to use: from tzfiles, TZ environment string, specified ranges using relative deltas, local machine time, fixed offset, UTC, iCal... 15
= rrule(MONTHLY, byweekday=MO(+1), dtstart=datetime(2005, 6, 6, 18, 30)) >>> rr.before(datetime(2007, 2, 23)) datetime.datetime(2007, 2, 5, 18, 30) My local Python group meets on the first Monday of each month at 18:30... 24
dtstart=datetime(2000, 11, 7)) >>> rr[0:3] [datetime.datetime(2000, 11, 7, 0, 0), datetime.datetime(2004, 11, 2, 0, 0), datetime.datetime(2008, 11, 4, 0, 0)] US Presidential Election Day, every four years on the first Tuesday after the first Monday of November... 26
rr = rrulestr(""" ... DTSTART:19480224 ... RRULE:FREQ=YEARLY;INTERVAL=1 ... """) >>> rr.after(datetime(2007,1,1)) datetime.datetime(2007, 2, 24, 0, 0) Use rrule.rrulestr() to make an rrule or rruleset from an iCal-style string! 30
dtstart=datetime(2000,2,29)) >>> rr[21] datetime.datetime(2084, 2, 29, 0, 0) You’re stuck with the pirates... and you don’t get the girl (at least not right away) 32
Figure out how much time we care about • Use a simple rrule to generate Feb. 28 datetimes for this range (or DIY) • Attach these dates as rdates to the rruleset • The rruleset now produces occurrences every calendar year 33
Helped my project launch on time • Many more examples at the dateutil site • Would be nice to have in the standard library, or at least better advertised • Read your Python Cookbook! 35