Validating a Date
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... Thanks in advance... :-) -- Edward Muller Interlix - President Web Hosting - PC Service & Support Custom Programming - Network Service & Support Phone: 417-862-0573 Cell: 417-844-2435 Fax: 417-862-0572 http://www.interlix.com
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
On 28 Feb 2003, 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...
Zope's DateTime() should do the job for you. It throws an exception for bad dates.
At 02:01 2003-02-28 -0600, 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...
try: ok_date = context.ZopeTime('2003/13/13') print "Yes, date OK" except: print "No, not OK" return printed
Thanks in advance... :-) -- Edward Muller
Interlix - President
Web Hosting - PC Service & Support Custom Programming - Network Service & Support
Phone: 417-862-0573 Cell: 417-844-2435 Fax: 417-862-0572
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Peter Bengtsson wrote:
At 02:01 2003-02-28 -0600, 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...
try: ok_date = context.ZopeTime('2003/13/13') print "Yes, date OK" except: print "No, not OK"
return printed
Nope, ZopeTime(DateTime) is very (too) liberal in what it accepts for this purpose: context.ZopeTime('2002/30/3') returns "2002/03/30" cheers, oliver
If you have a fixed date format, you can split into day,month and year and pass these as dedicated arguments to the DateTime constructor. See the DateTime API for details. -aj --On Donnerstag, 6. März 2003 02:48 +0100 Oliver Bleutgen <myzope@gmx.net> wrote:
Peter Bengtsson wrote:
At 02:01 2003-02-28 -0600, 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...
try: ok_date = context.ZopeTime('2003/13/13') print "Yes, date OK" except: print "No, not OK"
return printed
Nope, ZopeTime(DateTime) is very (too) liberal in what it accepts for this purpose:
context.ZopeTime('2002/30/3')
returns "2002/03/30"
cheers, oliver
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
-- --------------------------------------------------------------------- - Andreas Jung http://www.andreas-jung.com - - EMail: andreas at andreas-jung.com - - "Life is too short to (re)write parsers" - ---------------------------------------------------------------------
Thanks for the suggestions. Since I have split d/m/y fields on the form using DateTime is a good solution. On Fri, 2003-02-28 at 02: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...
Thanks in advance... :-) -- Edward Muller
Interlix - President Web Hosting - PC Service & Support Custom Programming - Network Service & Support Phone: 417-862-0573 Cell: 417-844-2435 Fax: 417-862-0572 http://www.interlix.com
participants (6)
-
Andreas Jung -
Dennis Allison -
Edward Muller -
Oliver Bleutgen -
Peter Bengtsson -
Thierry FLORAC