Using Jython 2.5.3
Isn’t dateDiff a general expression?
Traceback (most recent call last):
File “”, line 6, in
NameError: name ‘dateDiff’ is not defined
Trying to transform the [System]Gateway/CurrentDateTime to Unix time
Using Jython 2.5.3
Isn’t dateDiff a general expression?
Traceback (most recent call last):
File “”, line 6, in
NameError: name ‘dateDiff’ is not defined
Trying to transform the [System]Gateway/CurrentDateTime to Unix time
Here’s the Jython 2.5.x date stuff: http://www.jython.org/docs/library/datetime.html
I don’t see datediff in there…
In the Doc, the function is there, so I should be able to use it in the designer.
Right?
Is there an import that I can use?
https://docs.inductiveautomation.com/display/DOC/Expression+Functions
Expressions are different than scripting functions, so no, you can’t use it in Jython
Ignition uses java.util.Date objects for any tag or property defined as a DateTime, so you can simply use .getTime() on that object to get a unix timestamp in milliseconds since the epoch.
I will use the import for that class and use the getTme()
Thanks
No need to import if you have an object of a given class. Just run the method on the object. Importing java.util.Date would let you use the constructors, though.
Do you want to do this through an expression or through scripting? I'm guessing expression so this is how to convert [System]Gateway/CurrentDateTime to Unix time and format:
numberFormat(floor(toMillis({[System]Gateway/CurrentDateTime}) / 1000), '0')
Not an expression, just running scripts.
pturmel help me on that.
Thanks,
Great!
Just in case somebody stumbles across this thread in the future, here is how to do the same thing in scripts rather than expressions:
from java.text import SimpleDateFormat
inputFormat=SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy")
currentTime = system.tag.read("[System]Gateway/CurrentDateTime").value
date = SimpleDateFormat.parse(inputFormat,str(currentTime))
sec = date.getTime()/1000
system.gui.messageBox(str(sec))
It should be a little more light-weight since the object returned from CurrentDateTime is a date object already:
currentTimeMS = system.tag.read("[System]Gateway/CurrentDateTime").value.getTime()
system.gui.messageBox(str(currentTimeMS))
** whoops, removed conversion to seconds
Even better! I will leave mine up in case anybody stumbles across and wishes to convert a value that is not already a date object.
Just as a point of reference, getTime() is already in milliseconds. Both of your scripts give back seconds. Just verifying that’s what you wanted, but confusing since the variable has the ‘ms’ in it.