[Zope] Validating a Date

Thierry FLORAC thierry.florac@onf.fr
Fri, 28 Feb 2003 09:42:50 +0100


On Friday 28 February 2003 09:01, Edward Muller wrote:
> Does anyone know of a good module/product/hack that I can feed a date to
> and it will let me know if it's valid or not?
>
> It should check leap years ... end days of the month and the like...
>

A simple option would just be to :
 - enter date in a string variable
 - extract components (day, month and year) from this date (supposing a given 
format)
 - try to generate a DateTime object with these values.
An exception is raised if given values are incorrect...

For example (with a date entered in 'd/m/y' format) :

  from DateTime import DateTime

  def checkDate (value):
    d,m,y = value.split('/')
    test = DateTime ('%s/%s/%s' % (y, m, d))

You can, of course, add several checks to take care of input formats you want 
to make available...
It also depends of your locale settings, and if DateTime can convert directly 
your input format or not (that's not the case for me with a french locale).

Thierry