Date Comparison and Manipulation Functions?

John Machin sjmachin at lexicon.net
Sat Aug 30 01:28:20 CEST 2008


On Aug 30, 2:32 am, "W. eWatson" <notval... at sbcglobal.net> wrote:
> I just tried the following code, and got an unexpected result.
>
> from pyfdate import *
> t = Time()
>
> ts = Time(2008, 8, 29,15,20,7)
> tnew = ts.plus(months=6)
> print "new date: ", tnew
>
> Result:
> new date:  2009-02-28 15:20:07
>
> I believe that should be April 1, 2009.

Presuming that we are talking about the Gregorian calendar, and not
one of your own invention, you are (one trusts) alone in that belief.
There are SEVEN whole months and a bit between August 29, 2008 and
April 1, 2009. Count the months: Sep, Oct, Nov, Dec, Jan, Feb, Mar.

> If I use months = 1 and day =31, I
> get Sept. 30, 2008 and not Oct. 1, 2008. Is there a way to get around this?

Because the number of days in a month is not constant, adding a number
of months to a date is capable of more than one interpretation. Most
folk are happy with adding the months on and then ensuring that the
day is not later than the last day of the resultant (year, month)
combination -- this is what the pyfdate routine appears to be doing.
However there are some interesting ideas floating around e.g. IIRC an
eminent personage once asserted in this newsgroup that adding 1 month
to 31 Jan in a non-leap year should produce 3 Mar.

There is also the general question with date intervals of whether the
first day is included in the calculation or not. E.g. work on Monday,
Tuesday, Wednesday: that's 3 days service. Put money into the bank on
Monday, withdraw it on Wednesday: that's likely to attract 2 days
interest.

One needs to understand exactly what calculation is required, and
exactly what calculation is provided by the software that is proposed
to be used.

HTH,

John



More information about the Python-list mailing list