calendar (date) iterator?

M

Marcus

Hi,

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

A useful extension would be to allow specifiation of iter intervals,
e.g.,
iter = calendarIterator("03-12-2006 01:00:00", "minutes")
print iter.next()

03-12-2006 01:01:00

Thanks in advance for pointers and suggestions!
 
S

skip

Marcus> I'm looking for useful starting points, suggestions, and sample
Marcus> code, to implement a calendar iterator.

Have you looked at dateutil?

http://labix.org/python-dateutil
... print d
...
2006-03-12 00:00:00
2006-03-13 00:00:00
2006-03-14 00:00:00
2006-03-15 00:00:00
2006-03-16 00:00:00

Skip
 
M

Marcus

Marcus> I'm looking for useful starting points, suggestions, and sample
Marcus> code, to implement a calendar iterator.

Have you looked at dateutil?

http://labix.org/python-dateutil

... print d
...
2006-03-12 00:00:00
2006-03-13 00:00:00
2006-03-14 00:00:00
2006-03-15 00:00:00
2006-03-16 00:00:00

Skip


This is exactly what I was looking for! Thanks so much!!
 
R

Raymond Hettinger

[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.


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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top