calendar (date) iterator?

Raymond Hettinger python at rcn.com
Tue Mar 6 17:40:57 EST 2007


[Marcus]
> I'm looking for useful starting points, suggestions, and sample code,
> to implement a calendar iterator.  Simply, the iterator is seeded with
> an initial calendar date, e.g., "03-12-2006", and then subsequent
> calls to next return subsequent dates.  The seed could be a standard
> calendar/datetime object.
>
> > iter = calendarIterator("03-12-2006")
> > print iter.next()
>
>    03-12-2006

import datetime, time

def calendarIterator(start, fmt='%m-%d-%Y'):
    curr = datetime.date(*time.strptime(start, fmt)[:3])
    one = datetime.timedelta(1)
    while 1:
        curr += one
        yield curr

if __name__ == '__main__':
    iter = calendarIterator('3-12-2006')
    print iter.next()
    print iter.next()






More information about the Python-list mailing list