views:

1454

answers:

5

How do I iterate over a timespan after days, hours, weeks or months?

Something like:

for date in foo(from_date, to_date, delta=HOURS):
    print date

Where foo is a function, returning an iterator. I've been looking at the calendar module, but that only works for one specific year or month, not between dates.

A: 

This library provides a handy calendar tool: mxDateTime, that should be enough :)

dguaraglia
+17  A: 

I don't think there is a method in Python library, but you can easily create one yourself using datetime module:

from datetime import date, datetime, timedelta

def datespan(startDate, endDate, delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate < endDate:
        yield currentDate
        currentDate += delta

Then you could use it like this:

>>> for day in datespan(date(2007, 3, 30), date(2007, 4, 3), 
>>>                     delta=timedelta(days=1)):
>>>     print day
2007-03-30
2007-03-31
2007-04-01
2007-04-02

Or, if you wish to make your delta smaller:

>>> for timestamp in datespan(datetime(2007, 3, 30, 15, 30), 
>>>                           datetime(2007, 3, 30, 18, 35), 
>>>                           delta=timedelta(hours=1)):
>>>     print timestamp
2007-03-30 15:30:00
2007-03-30 16:30:00
2007-03-30 17:30:00
2007-03-30 18:30:00
DzinX
+3  A: 

For iterating over months you need a different recipe, since timedeltas can't express "one month".

from datetime import date

def jump_by_month(start_date, end_date, month_step=1):
    current_date = start_date
    while current_date < end_date:
        yield current_date
        carry, new_month = divmod(current_date.month - 1 + month_step, 12)
        new_month += 1
        current_date = current_date.replace(year=current_date.year + carry,
                                            month=new_month)

(NB: you have to subtract 1 from the month for the modulus operation then add it back to new_month, since months in datetime.dates start at 1.)

giltay
Two things: - this code raises `ValueError` if you set start_date's day to a number that doesn't exist in every month - probably, exactly for that reason there is no month-timedelta; I would suggest using `timedelta(days=30)` for a good approximation.
DzinX
Good catch. Sometimes, though, an approximation isn't good enough (my "pay my rent" reminder *has* to be at 6:30 on the first of every month). The recipe could be modified—probably with some extra state—to provide sane functionality, whatever that is.
giltay
You can also run into problems iterating year-by-year on February 29.
giltay
+4  A: 

Use dateutil and its rrule implementation, like so:

from dateutil import rrule
from datetime import datetime, timedelta

now = datetime.now()
hundredDaysLater = now + timedelta(days=100)

for dt in rrule.rrule(rrule.MONTHLY, dtstart=now, until=hundredDaysLater):
    print dt

Output is

2008-09-30 23:29:54
2008-10-30 23:29:54
2008-11-30 23:29:54
2008-12-30 23:29:54

Replace MONTHLY with any of YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, or SECONDLY. Replace dtstart and until with whatever datetime object you want.

This recipe has the advantage for working in all cases, including MONTHLY. Only caveat I could find is that if you pass a day number that doesn't exist for all months, it skips those months.

Thomas Vander Stichele
A: 

You should modify this line to make this work correctly:

current_date = current_date.replace(year=current_date.year + carry,month=new_month,day=1)

;)