[Python-Dev] Issue5434: datetime.monthdelta

Jess Austin jess.austin at gmail.com
Thu Apr 16 20:47:42 CEST 2009


On Thu, Apr 16, 2009 at 4:54 AM, Amaury Forgeot d'Arc
<amauryfa at gmail.com> wrote:
> 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')


My proposal has the "calendar sensitive" semantics you describe.  It
will not raise an exception in this case.


> 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)


I disagree with this point, in that I really like the pythonic date
calculations we have with timedelta.  It is easier to reason about
adding and subtracting objects than it is to reason about method
invocations.  Also, you can store a monthdelta in a variable, which is
sometimes convenient, and which is difficult to emulate with function
calls.

Except in certain particular cases, I'm not fond of last_day_of_month,
following_weekday, etc. functions.  Much in the way that timezone
considerations have been factored out of the core through the use of
tzinfo, I think these problems are more effectively addressed at the
level of detail one finds at the application level.  On the other
hand, it seems like effective month calculations could be useful in
the core.

cheers,
Jess


More information about the Python-Dev mailing list