I am trying to get a tuple of
datetime) objects over the last
My thought was to use the
dateutil package with something like this:
def last_n_months(n=12, ending=None): """Return a list of tuples of the first/last day of the month for the last N months """ from datetime import date from dateutil.rrule import rrule, MONTHLY from dateutil.relativedelta import relativedelta if not ending: ending = date.today() # set the ending date to the last day of the month ending = ending + relativedelta(months=+1, days=-ending.day) # starting is the first day of the month N months ago starting = ending - relativedelta(months=n, day=1) months = list(rrule(MONTHLY, bymonthday=(1, -1), dtstart=starting, until=ending)) # we return pairs of dates like this: (1st day of month, last day of month) months = zip(months[::2], months[1::2]) return months
>>> from datetime import date, timedelta # get last two months as a degenerate example >>> l2n = last_n_months(2, ending=date(2012, 01, 01)) >>> map(lambda x: [x.year, x.month, x.day], l2n) [[2011, 11, 1], [2011, 12, 1], [2012, 1, 1]] >>> map(lambda x: [x.year, x.month, x.day], l2n) [[2011, 11, 30], [2011, 12, 31], [2012, 1, 31]] >>> l24n = last_n_months(24, ending=date(2012,03,16)) >>> len(l24n) # inclusive of current month 25 # every tuple starts with the first day of the month >>> all(x.day == 1 for x in l24n) True # every tuple ends with the last day of the month >>> all((x +timedelta(days=1)).month != x.month for x in l24n) True # every tuple is the same month >>> all(x.month == x.month for x in l24n) True
I am posting it here to see if anyone has better solution than this (and perhaps see if this yields some off-by-one sort of error that I haven't thought of). Is there a simpler or faster solution than this?