[Zope3-checkins] CVS: StandaloneZConfig/ZConfig - datatypes.py:1.28

Barry Warsaw barry at zope.com
Mon Apr 19 09:47:57 EDT 2004


Update of /cvs-repository/StandaloneZConfig/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv7727/ZConfig

Modified Files:
	datatypes.py 
Log Message:
Added the timedelta standard datatype, which is similar to time-interval, but
returns a datetime.timedelta instead of a float.

Added test case and documentation.


=== StandaloneZConfig/ZConfig/datatypes.py 1.27 => 1.28 ===
--- StandaloneZConfig/ZConfig/datatypes.py:1.27	Mon Apr 12 11:27:00 2004
+++ StandaloneZConfig/ZConfig/datatypes.py	Mon Apr 19 09:47:10 2004
@@ -16,6 +16,7 @@
 import os
 import re
 import sys
+import datetime
 
 # types.StringTypes was added in Python 2.2; basestring in 2.3
 try:
@@ -277,6 +278,44 @@
                 return int(v[:-self._keysz]) * m
         return int(v) * self._default
 
+
+def timedelta(s):
+    # Unlike the standard time-interval data type, which returns a float
+    # number of seconds, this datatype takes a wider range of syntax and
+    # returns a datetime.timedelta
+    #
+    # Accepts suffixes:
+    #    w - weeks
+    #    d - days
+    #    h - hours
+    #    m - minutes
+    #    s - seconds
+    #
+    # and all arguments may be integers or floats, positive or negative.
+    # More than one time interval suffix value may appear on the line, but
+    # they should all be separated by spaces, e.g.:
+    #
+    # sleep_time 4w 2d 7h 12m 0.00001s
+    weeks = days = hours = minutes = seconds = 0
+    for part in s.split():
+        val = float(part[:-1])
+        suffix = part[-1]
+        if suffix == 'w':
+            weeks = val
+        elif suffix == 'd':
+            days = val
+        elif suffix == 'h':
+            hours = val
+        elif suffix == 'm':
+            minutes = val
+        elif suffix == 's':
+            seconds = val
+        else:
+            raise TypeError('bad part %s in %s' % (part, s))
+    return datetime.timedelta(weeks=weeks, days=days, hours=hours,
+                              minutes=minutes, seconds=seconds)
+
+
 stock_datatypes = {
     "boolean":           asBoolean,
     "dotted-name":       DottedNameConversion(),
@@ -306,10 +345,11 @@
                                            'h': 60*60,
                                            'd': 60*60*24,
                                            }),
+    "timedelta":         timedelta,
     }
 
-class Registry:
 
+class Registry:
     def __init__(self, stock=None):
         if stock is None:
             stock = stock_datatypes.copy()




More information about the Zope3-Checkins mailing list