[Python-Dev] Issue5434: datetime.monthdelta

glyph at divmod.com glyph at divmod.com
Fri Apr 17 04:53:32 CEST 2009


On 16 Apr, 11:11 pm, foom at fuhm.net wrote:
>On Apr 16, 2009, at 5:47 PM, Antoine Pitrou wrote:

>It's a human-interface operation, and as such, everyone (ahem) "knows 
>what it means" to say "2 months from now", but the details don't 
>usually have to be thought about too much. Of course when you have a 
>computer program, you actually need to tell it what you really mean.
>
>I do a fair amount of date calculating, and use two different kinds of 
>"add-month":
>
>Option 1)
>Add n to the month number, truncate day number to fit the month you 
>end up in.
>
>Option 2)
>As above, but with the additional caveat that if the original date is 
>the last day of its month, the new day should also be the last day of 
>the new month. That is:
>April 30th + 1 month = May 31st, instead of May 30th.
>
>They're both useful behaviors, in different circumstances.

I don't have a third option, but something that would be useful to 
mention in the documentation for "monthdelta": frequently users will 
want a recurring "monthly" event.  It's important to note that you need 
to keep your original date around if you want these rules to be 
consistently applied.  For example, if you have a monthly billing cycle 
that starts on May 31, you need to keep the original May 31 around to 
add monthdelta(X) if you want it to be May 31 when it rolls around next 
year; otherwise every time February rolls around all of your end-of- 
month dates get clamped to the 28th of every month.  (Unless you're 
following James's option 2, of course, in which case things which are 
normally on the 28th will get clamped to the 31st of following months.)

My experience with month-calculating software suggests that this is 
something very easy to screw up.


More information about the Python-Dev mailing list