Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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[0].year, x[0].month, x[0].day], l2n)
- [[2011, 11, 1], [2011, 12, 1], [2012, 1, 1]]
- >>> map(lambda x: [x[1].year, x[1].month, x[1].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[0].day == 1 for x in l24n)
- True
- # every tuple ends with the last day of the month
- >>> all((x[1] +timedelta(days=1)).month != x[1].month for x in l24n)
- True
- # every tuple is the same month
- >>> all(x[0].month == x[1].month for x in l24n)
- True
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement