dialogs/ForceVariableDialog.py
changeset 525 e8d5ab0855d3
parent 519 722714c04dcd
child 536 288324dddfb8
--- a/dialogs/ForceVariableDialog.py	Tue Apr 05 19:01:51 2011 +0200
+++ b/dialogs/ForceVariableDialog.py	Tue Apr 05 19:06:00 2011 +0200
@@ -51,7 +51,11 @@
 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]+)?"})
+
+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]+)?"})
+IEC_DATE_MODEL = re.compile("(?:(?:D|DATE)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})")
+IEC_DATETIME_MODEL = re.compile("(?:(?:DT|DATE_AND_TIME)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})-([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)")
+IEC_TIMEOFDAY_MODEL = re.compile("(?:(?:TOD|TIME_OF_DAY)#)?([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)")
 
 def gettime(v):    
     result = IEC_TIME_MODEL.match(v.upper())
@@ -76,6 +80,44 @@
     else: 
         return None
 
+def getdate(v):
+    result = IEC_DATE_MODEL.match(v.upper())
+    if result is not None:
+        year, month, day = result.groups()
+        try:
+            date = datetime.datetime(int(year), int(month), int(day))
+        except ValueError, e:
+            return None
+        base_date = datetime.datetime(1970, 1, 1)
+        return date - base_date
+    else: 
+        return None
+
+def getdatetime(v):
+    result = IEC_DATETIME_MODEL.match(v.upper())
+    if result is not None:
+        year, month, day, hours, minutes, seconds = result.groups()
+        try:
+            date = datetime.datetime(int(year), int(month), int(day), int(hours), int(minutes), int(float(seconds)), int((float(second) * SECOND) % SECOND))
+        except ValueError, e:
+            return None
+        base_date = datetime.datetime(1970, 1, 1)
+        return date - base_date
+    else: 
+        return None
+
+def gettimeofday(v):
+    result = IEC_TIMEOFDAY_MODEL.match(v.upper())
+    if result is not None:
+        hours, minutes, seconds = result.groups()
+        microseconds = 0
+        for value, factor in [(hours, HOUR),
+                              (minutes, MINUTE),
+                              (seconds, SECOND)]:
+            microseconds += float(value) * factor
+        return datetime.timedelta(microseconds=microseconds)
+    else:
+        return None
 
 GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False}.get(x.upper(), None),
                 "SINT": getinteger,
@@ -94,7 +136,10 @@
                 "LREAL": getfloat,
                 "STRING": getstring,
                 "WSTRING": getstring,
-                "TIME": gettime}
+                "TIME": gettime,
+                "DATE": getdate,
+                "DT": getdatetime,
+                "TOD": gettimeofday}
 
 
 class ForceVariableDialog(wx.TextEntryDialog):