49 |
49 |
50 SECOND = 1000000 |
50 SECOND = 1000000 |
51 MINUTE = 60 * SECOND |
51 MINUTE = 60 * SECOND |
52 HOUR = 60 * MINUTE |
52 HOUR = 60 * MINUTE |
53 DAY = 24 * HOUR |
53 DAY = 24 * HOUR |
54 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]+)?"}) |
54 |
|
55 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]+)?"}) |
|
56 IEC_DATE_MODEL = re.compile("(?:(?:D|DATE)#)?([0-9]{4})-([0-9]{2})-([0-9]{2})") |
|
57 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]+)?)") |
|
58 IEC_TIMEOFDAY_MODEL = re.compile("(?:(?:TOD|TIME_OF_DAY)#)?([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]+)?)") |
55 |
59 |
56 def gettime(v): |
60 def gettime(v): |
57 result = IEC_TIME_MODEL.match(v.upper()) |
61 result = IEC_TIME_MODEL.match(v.upper()) |
58 if result is not None: |
62 if result is not None: |
59 negative, days, hours, minutes, seconds, milliseconds = result.groups() |
63 negative, days, hours, minutes, seconds, milliseconds = result.groups() |
74 return datetime.timedelta(microseconds=microseconds) |
78 return datetime.timedelta(microseconds=microseconds) |
75 |
79 |
76 else: |
80 else: |
77 return None |
81 return None |
78 |
82 |
|
83 def getdate(v): |
|
84 result = IEC_DATE_MODEL.match(v.upper()) |
|
85 if result is not None: |
|
86 year, month, day = result.groups() |
|
87 try: |
|
88 date = datetime.datetime(int(year), int(month), int(day)) |
|
89 except ValueError, e: |
|
90 return None |
|
91 base_date = datetime.datetime(1970, 1, 1) |
|
92 return date - base_date |
|
93 else: |
|
94 return None |
|
95 |
|
96 def getdatetime(v): |
|
97 result = IEC_DATETIME_MODEL.match(v.upper()) |
|
98 if result is not None: |
|
99 year, month, day, hours, minutes, seconds = result.groups() |
|
100 try: |
|
101 date = datetime.datetime(int(year), int(month), int(day), int(hours), int(minutes), int(float(seconds)), int((float(second) * SECOND) % SECOND)) |
|
102 except ValueError, e: |
|
103 return None |
|
104 base_date = datetime.datetime(1970, 1, 1) |
|
105 return date - base_date |
|
106 else: |
|
107 return None |
|
108 |
|
109 def gettimeofday(v): |
|
110 result = IEC_TIMEOFDAY_MODEL.match(v.upper()) |
|
111 if result is not None: |
|
112 hours, minutes, seconds = result.groups() |
|
113 microseconds = 0 |
|
114 for value, factor in [(hours, HOUR), |
|
115 (minutes, MINUTE), |
|
116 (seconds, SECOND)]: |
|
117 microseconds += float(value) * factor |
|
118 return datetime.timedelta(microseconds=microseconds) |
|
119 else: |
|
120 return None |
79 |
121 |
80 GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False}.get(x.upper(), None), |
122 GetTypeValue = {"BOOL": lambda x: {"TRUE": True, "FALSE": False}.get(x.upper(), None), |
81 "SINT": getinteger, |
123 "SINT": getinteger, |
82 "INT": getinteger, |
124 "INT": getinteger, |
83 "DINT": getinteger, |
125 "DINT": getinteger, |