how to build a list of mx.DateTime objects from a start and end date?

M.-A. Lemburg mal at lemburg.com
Tue Sep 16 17:57:22 EDT 2003


Christos TZOTZIOY Georgiou wrote:
> On Tue, 16 Sep 2003 16:02:53 -0400 (EDT), rumours say that
> python at sarcastic-horse.com might have written:
> 
> 
>>I want to make a list of mx.DateTime objects that cover all the monthly
>>intervals between two points.  For example:
>>
>>
>>>>>import mx.DateTime
>>>>>nov1999 = mx.DateTime.Date(1999, 11)
>>>>>mar2003 = mx.DateTime.Date(2003, 3)
>>>>>alldates = mk_list(startdate=nov1999, enddate=mar2003)
>>>>>alldates
>>
>>[ ... a bunch of mx.DateTime objects including and between the startdate
>>and enddate ...]
>>
>>How would I do something like this?
> 
> 
> I'm not familiar with the exact names of the mx functions, but what I
> would do, would be:
> 
> create an empty list
> set an mx.DateTime object to the start date
> while the mx.DateTime is less or equal to the end date:
>  append it to the list, add one day to it
> 
> Add some parentheses and you got the python code :)

Not the fastest, but the easy to read:

from mx.DateTime import Date, RelativeDate

def daterange(startdate, enddate, delta=RelativeDate(months=+1)):
     l = []
     date = startdate
     while date < enddate:
         l.append(date)
         date += delta
     return l

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Software directly from the Source  (#1, Sep 16 2003)
 >>> Python/Zope Products & Consulting ...         http://www.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::






More information about the Python-list mailing list