[Python-Dev] Issue5434: datetime.monthdelta

Amaury Forgeot d'Arc amauryfa at gmail.com
Thu Apr 16 11:54:13 CEST 2009


On Thu, Apr 16, 2009 at 10:45,  <skip at pobox.com> wrote:
>    >>> date(2008, 1, 30) + monthdelta(1)
>    datetime.date(2008, 2, 29)
>
> What would this loop would print?
>
>    for d in range(1, 32):
>        print date(2008, 1, d) + monthdelta(1)
>
> I have this funny feeling that arithmetic using monthdelta wouldn't always
> be intuitive.

FWIW, the Oracle database has two methods for adding months:
1- the add_months() function
    add_months(to_date('31-jan-2005'), 1)
2- the ANSI interval:
    to_date('31-jan-2005') + interval '1' month

"add_months" is calendar sensitive, "interval" is not.
"interval" raises an exception if the day is not valid for the target
month (which is the case in my example)

"add_months" is similar to the proposed monthdelta(),
except that it has a special case for the last day of the month:
"""
If date is the last day of the month or if the resulting month has
fewer days than the day
component of date, then the result is the last day of the resulting month.
Otherwise, the result has the same day component as date.
"""
indeed:
    add_months(to_date('28-feb-2005'), 1) == to_date('31-mar-2005')



In my opinion:
arithmetic with months is a mess. There is no such "month interval" or
"year interval" with a precise definition.
If we adopt some kind of month manipulation, it should be a function
or a method, like you would do for features like last_day_of_month(d),
or following_weekday(d, 'monday').

    date(2008, 1, 30).add_months(1) == date(2008, 2, 29)

-- 
Amaury Forgeot d'Arc


More information about the Python-Dev mailing list