datetime.iterdate

Christopher T King squirrel at WPI.EDU
Mon Jul 12 09:20:38 EDT 2004


On Sun, 11 Jul 2004, Robert Brewer wrote:

> Anyone else tired of typing date-addition logic when iterating? It would
> be nice if the datetime package had something like:
> 
> def iterdates(first, last):
>     for day in range((last - first).days + 1):
>         yield first + datetime.timedelta(day)
> 
> ...notice the inclusive boundaries (i.e. last gets returned). This
> simple construct would make ugly date loops a lot cleaner:
> 
> for day in datetime.iterdates(first_date, last_date):
>    do_something_with(day)

This is something that could be made simpler with a previous proposal I 
had made to replace xrange():

for day in first_date<=datetime.days<=last_date:
    do_something_with(day)

where datetime.days is an object representing the set of all days, and its 
comparison functions return a subset of that set. To work perfectly, my 
proposal needs only a slight change in the parser (to compile a<b<c as 
(a<b)&(b<c) rather than a<b and b<c); I'm not sure how much this would 
affect existing code though.




More information about the Python-list mailing list