SIEMENS Driver DATE_AND_TIME values

I did some digging in my bag of discarded solutions, so here is what i tried initially (not really nice but it works)
You need to get the plc data in two 32-bit tags (Driver format D). So when date is stored in datablock 50 at adress 0, the tags are [S7]DB50,D and [S7]DB50,D4.
Make an expression tag (e.g. S7DateVal) with following expression:

({[.]DB50DD0}<<32) + {[.]DB50DD4}

All of the above tags need to have INT8 as datatype.

Create an additional memory tag of type DATETIME with the same name added ‘DT’ at the end (S7DateValDT).

Put the following code in a tag change script:

val = newValue.value
val = val >> 4	# Ignore DayOfWeek
msec = ((val & 0xf00) / 256)*100
msec += ((val & 0xf0) / 16)*10
msec += (val & 0xf)
val = val >> 12
sec = ((val & 0xf0) / 16)*10 + (val & 0xf)
val = val >> 8
min = ((val & 0xf0) / 16)*10 + (val & 0xf)
val = val >> 8
hour = ((val & 0xf0) / 16)*10 + (val & 0xf)
val = val >> 8
day = ((val & 0xf0) / 16)*10 + (val & 0xf)
val = val >> 8
month = ((val & 0xf0) / 16)*10 + (val & 0xf) - 1	# Java month starts with 0
val = val >> 8
year = ((val & 0xf0) / 16)*10 + (val & 0xf)
if year<90:
	year += 2000
else:
	year += 1900
#print "%d %d %d %d %d %d %d" % (msec,  sec, min, hour, day, month, year)
from java.util import Calendar
cal = Calendar.getInstance()
cal.set(year, month, day, hour, min, sec)
cal.set(Calendar.MILLISECOND, msec)
#print cal.getTime()
system.tag.write(event.tagPath.toStringFull()+'DT', cal.getTime()) 

Add the expression tag to the tag change script.
Now, on every change of your plc data the …DT tag will be updated.

Be aware the access to two seperate values might not be consistent, so don’t try this with fast changing values.

Hope this helps.

2 Likes