469,946 Members | 1,936 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

calendar (date) iterator?

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!

Mar 6 '07 #1
4 2097
Oops-- the iter needs to work better than I do! :)
iter = calendarIterator("03-12-2006")
print iter.next()

03-12-2006
^^^^^^^^^^
03-13-2006

iter = calendarIterator("03-12-2006 01:00:00", "minutes")
print iter.next()

03-12-2006 01:01:00
^^^^^^^^^^
03-13-2006

Mar 6 '07 #2

MarcusI'm looking for useful starting points, suggestions, and sample
Marcuscode, to implement a calendar iterator.

Have you looked at dateutil?

http://labix.org/python-dateutil
>>from dateutil.rrule import rrule, DAILY
from dateutil.parser import parse
rule = rrule(DAILY, count=5, dtstart=parse("2006-03-12"))
for d in rule:
... 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
Mar 6 '07 #3
On Mar 6, 3:19 pm, s...@pobox.com wrote:
MarcusI'm looking for useful starting points, suggestions, and sample
Marcuscode, to implement a calendar iterator.

Have you looked at dateutil?

http://labix.org/python-dateutil
>>from dateutil.rrule import rrule, DAILY
>>from dateutil.parser import parse
>>rule = rrule(DAILY, count=5, dtstart=parse("2006-03-12"))
>>for d in rule:
... 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!!

Mar 6 '07 #4
[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()

Mar 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by cg_news | last post: by
4 posts views Thread by Michele Simionato | last post: by
10 posts views Thread by vinu | last post: by
2 posts views Thread by Caesar Augustus | last post: by
3 posts views Thread by thorpk | last post: by
1 post views Thread by swethak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.