Skip to main content
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Tweeted twitter.com/#!/StackCodeReview/status/283388451259305984
Source Link

Building a list of dates between two dates

My solution to this feels 'icky' and I've got calendar math falling out of my ears after working on similar problems for a week so I can't think straight about this.

Is there a better way to code this?

import datetime
from dateutil.relativedelta import relativedelta


def date_count(start, end, day_of_month=1):
    """
    Return a list of datetime.date objects that lie in-between start and end.

    The first element of the returned list will always be start and the last
    element in the returned list will always be:
        datetime.date(end.year, end.month, day_of_month)

    If start.day is equal to day_of_month the second element will be:
        start + 1 month

    If start.day is after day_of_month then the second element will be:
        the day_of_month in the next month

    If start.day is before day_of_month then the second element will be:
        datetime.date(start.year, start.month, day_of_month)


    >>> start = datetime.date(2012, 1, 15)
    >>> end = datetime.date(2012, 4, 1)
    >>> date_count(start, end, day_of_month=1) #doctest: +NORMALIZE_WHITESPACE
    [datetime.date(2012, 1, 15), datetime.date(2012, 2, 1),
    datetime.date(2012, 3, 1), datetime.date(2012, 4, 1)]

    Notice that it's not a full month between the first two elements in the
    list.

    If you have a start day before day_of_month:
    >>> start = datetime.date(2012, 1, 10)
    >>> end = datetime.date(2012, 4, 1)
    >>> date_count(start, end, day_of_month=15) #doctest: +NORMALIZE_WHITESPACE
    [datetime.date(2012, 1, 10), datetime.date(2012, 1, 15),
    datetime.date(2012, 2, 15), datetime.date(2012, 3, 15),
    datetime.date(2012, 4, 15)]

    Notice that it's not a full month between the first two elements in the
    list and that the last day is rounded to
    datetime.date(end.year, end.month, day_of_month)
    """
    last_element = datetime.date(end.year, end.month, day_of_month)

    if start.day == day_of_month:
        second_element = start + relativedelta(start, months=+1)
    elif start.day > day_of_month:
        _ = datetime.date(start.year, start.month, day_of_month)
        second_element = _ + relativedelta(_, months=+1)
    else:
        second_element = datetime.date(start.year, start.month, day_of_month)

    dates = [start, second_element]

    if last_element <= second_element:
        return dates

    while dates[-1] < last_element:
        next_date = dates[-1] + relativedelta(dates[-1], months=+1)
        next_date = datetime.date(next_date.year, next_date.month, day_of_month)
        dates.append(next_date)
    dates.pop()

    dates.append(last_element)

    return dates