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.
The line of code: date=DateTime('2003/13/03') causes zope to try to create a date variable based on the string '2003/13/03'. Zope will try a variety of formats to find one that works (zope does not know that you want yyyy/mm/dd, it just tries to make it work). The line of code: print date.strftime("%Y %B %d") just takes a date variable and prints it out according to the format supplied ("%Y %B %d"). There is no relationship between the two lines of code (ie. the second line of code does not supply the format that you want the first line of code to use). There is a function called strptime in the python Time module that will parse a string according to a given format. I have never used it, but you can find a description of this routine in the Python Library Reference manual. Jonathan ----- Original Message ----- From: Philippe Vignaux To: zope@zope.org Sent: November 24, 2003 9:37 AM 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@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 )
On Mon, 2003-11-24 at 07:16, Small Business Services wrote:
There is a function called strptime in the python Time module that will parse a string according to a given format. I have never used it, but you can find a description of this routine in the Python Library Reference manual.
IIRC, strptime is not included in Widoze distros of Python 2.1. It's easy enough to find recipes online, however. HTH, Dylan
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@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@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 )
Hi, I have a product with an folderish object. When examining the contents of the _objects attribute I see the following behavior when calling the following function through the ZMI. def test(self): Windows XP Pro Zope 2.7.0-b3 Python 2.3.2
Hi, Sorry for the incomplete posting. I hit the wrong hotkey :( I have a product with an folderish object. When examining the contents of the _objects attribute I see the following behavior when calling the following function through the ZMI. def test(self): """ """ return self._objects No items ==> nothing returned 1 *Folder* item ==> ({'meta_type': 'Folder', 'id': 'Folder'},) 2 *Folder* items ==> {'meta_type': 'Folder', 'id': 'folder1'} 3 *Folder* items ==> ({meta_type': 'Folder', 'id': 'Folder'}, {'meta_type': 'Folder', 'id': 'folder2'}) Is this correct behavior? If you delete all but one item _object will contain a sequence of dictionaries instead of a dictionary only. Delete that last item and start over and you will get the behavior shown above. Environment: Windows XP Pro Zope 2.7.0-b3 Python 2.3.2 Thanks, Mike
Why exactly are you using the _objects attribute directly? If you subclass from something like Folder you should use the API they have, such as objectIds or whatever you need. jens On Dec 28, 2003, at 14:17, Michael Long wrote:
Hi,
Sorry for the incomplete posting. I hit the wrong hotkey :(
I have a product with an folderish object. When examining the contents of the _objects attribute I see the following behavior when calling the following function through the ZMI.
def test(self): """ """ return self._objects
No items ==> nothing returned 1 *Folder* item ==> ({'meta_type': 'Folder', 'id': 'Folder'},)
2 *Folder* items ==> {'meta_type': 'Folder', 'id': 'folder1'}
3 *Folder* items ==> ({meta_type': 'Folder', 'id': 'Folder'}, {'meta_type': 'Folder', 'id': 'folder2'})
Is this correct behavior? If you delete all but one item _object will contain a sequence of dictionaries instead of a dictionary only. Delete that last item and start over and you will get the behavior shown above.
Environment: Windows XP Pro Zope 2.7.0-b3 Python 2.3.2
Thanks, Mike
Why exactly are you using the _objects attribute directly? If you subclass from something like Folder you should use the API they have, such as objectIds or whatever you need.
I am looking to get the number of objects in a folder based on meta_type with going through the expense of *waking* up each object.
Michael Long wrote at 2003-12-28 14:17 -0500:
Sorry for the incomplete posting. I hit the wrong hotkey :(
I have a product with an folderish object. When examining the contents of the _objects attribute I see the following behavior when calling the following function through the ZMI.
def test(self): """ """ return self._objects
No items ==> nothing returned 1 *Folder* item ==> ({'meta_type': 'Folder', 'id': 'Folder'},)
2 *Folder* items ==> {'meta_type': 'Folder', 'id': 'folder1'}
3 *Folder* items ==> ({meta_type': 'Folder', 'id': 'Folder'}, {'meta_type': 'Folder', 'id': 'folder2'})
Is this correct behavior?
ZPublisher treats tuples with 2 elements in a special way (using part as title and part as body). Avoid such tuples... -- Dieter
participants (7)
-
Dieter Maurer -
Dragos Chirila -
Dylan Reinhardt -
Jens Vagelpohl -
Michael Long -
Philippe Vignaux -
Small Business Services