# [egenix-users] RelativeDateTime

M.-A. Lemburg mal at egenix.com
Fri Jan 23 11:53:13 CET 2004

Mark McEahern wrote:
> [Francois Girault]
>
>>>"natural rules" are *very* ambigious, but playing with postgres tell
>>>
>>>=> SELECT date('2002-01-30') + "interval"('1 mon');
>>>      ?column?
>>>---------------------
>>> 2002-02-28 00:00:00
>>>(1 row)
>>>
>>>For date from january, 28th to january, 31th adding one month result in
>>>the last day of february. From a math point of view, it seems wrong, but
>>>thinking more about make me consider it 'natural'
>
>
> [M.-A. Lemburg]
>
>>The problem with this approach is obvious, e.g. what would
>>the outcome of the following variations be:
>>
>>Date(2004,1,31) + RelativeDateTime(months=+1)
>>Date(2004,1,31) + RelativeDateTime(months=+2) - RelativeDateTime(months=-2)
>>Date(2004,1,31) + RelativeDateTime(months=+3)
>
>
> "weirdmonths".  When adding/subtracting "weirdmonths", you'd do the math
> on the month part and adjust the day value accordingly in the case of
> overflow (i.e., the new month doesn't have the same number of days) or
> underflow (i.e., you were on the last day of the original month, but the
> new month has more days--should you still be on the last day?).  Hmm, I
> wonder what Postgresql does for the equivalent of this:
>
>   Date(2004, 4, 30) + RelativeDateTime(weirdmonths=-1)
>
> You'd think the answer would be Date(2004, 3, 31).  However:
>
> courier=> SELECT date('2004-04-30') - "interval"('1 mon');
>       ?column?
> ---------------------
>  2004-03-30 00:00:00
> (1 row)
>
> So, the day adjustment only seems to happen in the case of overflow.

The idea of using a new parameter is a good one; I think
"intmonths" would be a suitable name. The semantics being the
same as for month with the exception of the overflow case.
intmonths would truncate the day to the last day of the month
in case of an overflow.

The results for the above would be:

Date(2004,1,31) + RelativeDateTime(intmonths=+1)
== Date(2004,2,29)

Date(2004,1,31) + RelativeDateTime(intmonths=+1)
- RelativeDateTime(intmonths=-1)
== Date(2004,1,29)

Date(2004,1,31) + RelativeDateTime(intmonths=+2)
- RelativeDateTime(intmonths=-2)
== Date(2004,1,31)

Date(2004,1,31) + RelativeDateTime(intmonths=+3)
== Date(2004,4,30)

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 23 2004)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/