[Zope] date formats

Dragos Chirila d.chirila at bucarest.finsiel.ro
Mon Nov 24 10:30:29 EST 2003


hi there

i handled this problem by writing the following 2 functions:

def tmCheckDate(self, p_datestring, p_separator='/'):
    """This functions takes a string that reprezents a date like 'dd/mm/yyyy' and tests is a valide date."""
    l_isDate = 1
    l_daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    l_dateparts = p_datestring.split(p_separator)
    if len(l_dateparts) != 3:
        l_isDate = 0
    else:
        l_intYear = l_dateparts[2]
        l_intMonth = l_dateparts[1]
        l_intDay = l_dateparts[0]
        try:
            l_intYear = int(l_intYear, 10)
            l_intMonth = int(l_intMonth, 10)
            l_intDay = int(l_intDay, 10)
        except:
            l_isDate = 0
        else:
            if (l_intYear <= 50):
                l_intYear += 2000
            if (l_intYear >= 51 and l_intYear < 100):
                l_intYear += 1900
            if ((l_intMonth==0) or (l_intMonth > 12) or (l_intDay == 0) or (l_intYear > 9999)):
                l_isDate = 0
            else:
                if (l_intMonth == 2): #february
                    if calendar.isleap(l_intYear):
                        l_nDays = 29
                    else:
                        l_nDays = 28
                else:
                    l_nDays = l_daysInMonth[l_intMonth-1]
                if (l_intDay > l_nDays):
                    l_isDate = 0
    return l_isDate

def tmConvertStringToDateTimeObj(self, p_datestring, p_separator='/'):
    """Takes a string that represents a date like 'dd/mm/yyyy' and returns a DateTime object"""
    try:
        l_dateparts = p_datestring.split(p_separator)
        l_intYear = int(l_dateparts[2], 10)
        l_intMonth = int(l_dateparts[1], 10)
        l_intDay = int(l_dateparts[0], 10)
        return DateTime('%s/%s/%s 00:00:00' % (l_intYear, l_intMonth, l_intDay))
    except:
        return None

i used the tmCheckDate function to validate strings like 'dd/mm/yyyy' that comes from a form or whatever,
and after that tmConvertStringToDateTimeObj to get the DateTime object.

you can modify the functions to work with 'YYYY/MM/DD' strings.

hope this will help

dragos

  ----- Original Message ----- 
  From: Philippe Vignaux 
  To: zope at zope.org 
  Sent: Monday, November 24, 2003 4:37 PM
  Subject: [Zope] date formats


  Hi all !



  .  can't get rid off my dates problems ..



  I would like the following python statements to apply validity checks on a date always inputed under format    YYYY  /  MM / DD

  (GMT+1)





  The 2 first examples behaves as expected.

  The last one not.  







  try:

     date=DateTime('2003/07/03')

     print date.strftime("%Y %B %d")

  except:

     print 'error'



return printed                                                      2003 July 03   (OK)





  try:

     date=DateTime('2003/03/07')

     print date.strftime("%Y %B %d")

  except:

     print 'error'



  return printed                                                            2003 March 07    (OK)









  try:

     date=DateTime('2003/13/03')

     print date.strftime("%Y %B %d")

  except:

     print 'error'



return printed                                                      2003 March 13   (NOK)



  While I'm expecting an error (no months with id 13 !!!)  it reverses the month part and the day part !!



  Why ??

  Is that the normal behaviour ??

  Or is there any parameter to provide so that python knows the format is always YYYY / MM / DD



  (the  setup:       Windows 2000  server                   Zope 2.6.2       python 2.1 )





  Thanks for any help !



  Phil.





------------------------------------------------------------------------------


  _______________________________________________
  Zope maillist  -  Zope at 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 )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.zope.org/pipermail/zope/attachments/20031124/4bd3adc9/attachment-0001.html


More information about the Zope mailing list