How to manipulate datetime returned from client tag?

Further to How to have a common date range across windows question.

[code]import datetime

ctEndDate has bi-directional binding to a calendar component.

endDate = system.tag.read("[Client]ctEndDate")
print endDate

results in: [Thu Mar 09 23:59:59 GMT 2017, Good, Thu Mar 09 12:20:38 GMT 2017]

[/code]

How do I work with this value to calculate time difference and output the result to another client tag? I understand (mostly) how timedelta works. The datetime functions don’t seem to understand the data format returned by system.tag.read.

endDate = system.tag.read("[Client]ctEndDate").value

Two things:

  1. system.tag.read() returns a “Qualified Value” that includes quality and timestamp, not just the actual tag value. Use endDate.value to get the tag value itself.
  2. Since Ignition is written in Java, it uses the java.util.Date datatype everywhere, not the python datetime datatype. Consider using java.util.Calendar to manipulate your dates instead of the python date difference methods – it’ll save you a lot of grief.

@MMaynardUSG: That was the kind of prompt I was hoping for, thanks. I’m still missing something in my understanding.

import datetime
eDate = system.tag.read("[Client]ctEndDate").value
print "eDate.value: ", eDate
print "eDate year month day: ", eDate.year, eDate.month, eDate.day

results in

eDate.value:  Thu Mar 09 23:59:59 gmt 2017
eDate year month day: 117 2 4

Why is this?

@pturmel: Thanks for the tips. I had an unsuccessful look around for an example of using the Java libraries. I’ll have to do some more research.

Java dates can be weird.

The year is the number of years since 1900. So if you print year+1900, you’ll be happier with the result

The month is 0 indexed, so the third month (March) is 2.

Day is actually day of the week. You probably wanted to print date.

The docs can be found at docs.oracle.com/javase/8/docs/a … /Date.html Personally I find it easier to use the Date to create a Java Calendar object and use Calendar’s constants to get what I want. But that’s probably because I’m a Java programmer, not a Python programmer. :slight_smile:

Thanks, Kathy. I’m in business.
I’ve only one date-diff calculation to do for this project so I’ll stick with Python for now. Points about switching to Java are taken.

For anyone else this will work in the script console:

[code]import datetime
eDate = system.tag.read("[Client]ctEndDate").value
daysToSpan = 5

print "eDate.value: ", eDate
print "eDate year month day: ", eDate.year + 1900, eDate.month + 1, eDate.date
print "5 days span start date: ", datetime.date(eDate.year + 1900, eDate.month + 1, eDate.date) - datetime.timedelta(days = daysToSpan - 1)[/code]
… giving output …

eDate.value: Thu Mar 09 23:59:59 GMT 2017 eDate year month day: 2017 3 9 5 days span start date: 2017-03-05

And if I’d had enough caffeine this morning, I could also have recommended the scripting functions we provided to make this easier. (As I said, I’m a Java person not Python. :blush: )

system.date.get* and system.date.add*