[egenix-users] Apparent bug in RelativeDateDiff() routine

M.-A. Lemburg mal at egenix.com
Fri Dec 10 12:03:56 CET 2004


Bob Gibson wrote:
> 
> 
> 
> 
>   Am I missing something?  It seems that the RelativeDateDiff() routine
> ignores the actual number of days in the current month when calculating the
> difference.  It seems to use a fixed number of days (30) for each month.

Thanks for pointing this out. There are a few problems with the
algorithm used in RelativeDateDiff(). We should probably find a
more robust and faster one.

Any suggestions ?

Note that the output of the function is not properly defined,
since there are many ways you can represent the relative date/time
difference between two dates.

Here are some requirements:
* years should be preferred over months; months over days
* all deltas must have the same sign
* date2 + RelativeDateDiff(date1,date2) == date1 must hold for all
   RelativeDateTimeDiff(date1,date2) calls

> Here is a script that supports this theory.
> 
> ----------------------------------------------------------------------
> from mx.DateTime import *
> 
> #         Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
> actual = [ 30, 28, 30, 29, 30, 29, 30, 30, 29, 30, 29 ]
> 
> stop   = Date( 2000, 12, 1 )
> finish = stop.strftime( '%m/%d/%Y' )
> for mon in range( 11, 0, -1 ) :
>   start = Date( 2000, mon, 2 )
>   begin = start.strftime( '%m/%d/%Y' )
>   delta = RelativeDateDiff( stop, start )
>   correct = actual.pop()
>   print "%s - %s = %2d months, and %d days [%d]" % ( finish, begin,
> delta.months, delta.days, correct )
> ----------------------------------------------------------------------
> 
>   The output of this script is as follows:
> ----------------------------------------------------------------------
> 12/01/2000 - 11/02/2000 =  0 months, and 29 days [29]
> 12/01/2000 - 10/02/2000 =  1 months, and 29 days [30]
> 12/01/2000 - 09/02/2000 =  2 months, and 29 days [29]
> 12/01/2000 - 08/02/2000 =  3 months, and 29 days [30]
> 12/01/2000 - 07/02/2000 =  4 months, and 29 days [30]
> 12/01/2000 - 06/02/2000 =  5 months, and 29 days [29]
> 12/01/2000 - 05/02/2000 =  6 months, and 29 days [30]
> 12/01/2000 - 04/02/2000 =  7 months, and 29 days [29]
> 12/01/2000 - 03/02/2000 =  8 months, and 29 days [30]
> 12/01/2000 - 02/02/2000 =  9 months, and 29 days [28]
> 12/01/2000 - 01/02/2000 = 10 months, and 29 days [30]
> ----------------------------------------------------------------------
> 
>   The value in brackets '[]' is the correct number of days that should be
> returned by RelativeDateDiff(), based upon the actual number of days in the
> specified month.
> 
>   For example, since 2000 was a leap year, there were 29 days in Feb, 2000.
> So the number of days from Feb 2, 2000 until the 1st of Mar, should be
> calculated as follows:
> 
>   29 - 2 + 1 = 28
> 
>   (# of days in current month) - (current day of current month ) + (# of
> days into target day in next month)
> 
> Thanks
> Bob
> 
> Ps 37:16  It is better to be godly and have little than to be evil and
> possess much.
> 
> 
> ------------------------------------------------------------------------
> 
> 
> _______________________________________________________________________
> eGenix.com User Mailing List                     http://www.egenix.com/
> http://lists.egenix.com/mailman/listinfo/egenix-users

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 07 2004)
 >>> Python/Zope Consulting and Support ...        http://www.egenix.com/
 >>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::



More information about the egenix-users mailing list