[Zope] Date addition in a Python script

Carlson, Christopher W. Christopher.W.Carlson@saint-gobain.com
Wed, 16 Jan 2002 07:47:37 -0800


I'm trying to add an arbitrary number of months to a DateTime value, and
keep running into problems.

My code looks like this:

-----
## Script (Python) "NewDate"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=Member, Effective_Date, Months_Added
##title=
##
Old_Date = Member.PaidThrough
Eff_Date = DateTime(Effective_Date)

if Eff_Date > Old_Date:
  Old_Date = Eff_Date

Month = Old_Date.month()
Day = Old_Date.day()
Year = Old_Date.year()

NewDate = DateTime(Year,Month+int(Months_Added),Day)

return NewDate
-----

The problem arises when DateTime() tries to create a new date out of values
like 2002,21,15 (reached by adding 15 months to 2002-06-15).  Is there a way
to coerce DateTime() to handle situations like this, or an algorithm for
'fixing' such date addition?

--- Chris Carlson