dialogs/ForceVariableDialog.py
changeset 519 722714c04dcd
parent 516 40290ddff19c
child 525 e8d5ab0855d3
--- a/dialogs/ForceVariableDialog.py	Fri Apr 01 15:33:36 2011 +0200
+++ b/dialogs/ForceVariableDialog.py	Fri Apr 01 17:19:31 2011 +0200
@@ -22,6 +22,7 @@
 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import wx
+import re
 import datetime
 
 #-------------------------------------------------------------------------------
@@ -46,10 +47,33 @@
 getfloat = gen_get_function(float)
 getstring = gen_get_function(str)
 
-def gettime(v):
-    try:
-        return datetime.timedelta(0, float(v))
-    except: 
+SECOND = 1000000
+MINUTE = 60 * SECOND
+HOUR = 60 * MINUTE
+DAY = 24 * HOUR
+IEC_TIME_MODEL = re.compile("(?:T|TIME)#(-)?(?:(%(float)s)D_?)?(?:(%(float)s)H_?)?(?:(%(float)s)M_?)?(?:(%(float)s)S_?)?(?:(%(float)s)MS)?" % {"float": "[0-9]+(?:\.[0-9]+)?"})
+
+def gettime(v):    
+    result = IEC_TIME_MODEL.match(v.upper())
+    if result is not None:
+        negative, days, hours, minutes, seconds, milliseconds = result.groups()
+        microseconds = 0
+        not_null = False
+        for value, factor in [(days, DAY),
+                              (hours, HOUR),
+                              (minutes, MINUTE),
+                              (seconds, SECOND),
+                              (milliseconds, 1000)]:
+            if value is not None:
+                microseconds += float(value) * factor
+                not_null = True
+        if not not_null:
+            return None
+        if negative is not None:
+            microseconds = -microseconds
+        return datetime.timedelta(microseconds=microseconds)
+    
+    else: 
         return None