Example #1
0
def add_interval_to_datetime(interval, dt):
    """
    Add a timedelta or isodate.Duration to a datetime and return the result.
    This is a workaround to a bug in isodate 0.4.7-0.4.8 that prevents Duration
    instances from being added to datetime instances.
    @param interval: interval to add to the datetime instance
    @param dt: datetime instance
    @return: new datetime instance with interval added
    @rtype: datetime.datetime
    """
    assert isinstance(interval, (datetime.timedelta, isodate.Duration))
    assert isinstance(dt, datetime.datetime)

    if isinstance(interval, datetime.timedelta):
        return interval + dt

    new_month = dt.month + interval.months
    year_carry, new_month = fquotmod(new_month, 1, 13)
    new_year = dt.year + interval.years + year_carry

    max_days = max_days_in_month(new_year, new_month)
    new_day = min(max_days, dt.day)

    new_dt = dt.replace(year=int(new_year), month=int(new_month), day=int(new_day))
    return interval.tdelta + new_dt
Example #2
0
def add_interval_to_datetime(interval, dt):
    """
    Add a timedelta or isodate.Duration to a datetime and return the result.
    This is a workaround to a bug in isodate 0.4.7-0.4.8 that prevents Duration
    instances from being added to datetime instances.
    @param interval: interval to add to the datetime instance
    @param dt: datetime instance
    @return: new datetime instance with interval added
    @rtype: datetime.datetime
    """
    assert isinstance(interval, (datetime.timedelta, isodate.Duration))
    assert isinstance(dt, datetime.datetime)

    if isinstance(interval, datetime.timedelta):
        return interval + dt

    new_month = dt.month + interval.months
    year_carry, new_month = fquotmod(new_month, 1, 13)
    new_year = dt.year + interval.years + year_carry

    max_days = max_days_in_month(new_year, new_month)
    new_day = min(max_days, dt.day)

    new_dt = dt.replace(year=int(new_year), month=int(new_month), day=int(new_day))
    return interval.tdelta + new_dt