Hi,
I'm getting a different value from datetime.datetime.now() depending on where I execute the script.
From the script console...
import datetime
dt1 = datetime.datetime.now()
print (dt1)
... the result is 2024-07-19 12:10:31.710000
In the onclick handler of a button on a Perspective view...
def runAction(self, event):
import datetime
dt1 = datetime.datetime.now()
self.getSibling("dtStart").props.value = dt1
system.perspective.print (dt1)
... the result is 2024-07-19 08:10:22.325
There is a 4 hour difference. I have a DEV server, Windows Server 2016, that Ignition is installed on. The server is set to the same time as my laptop, it has the same time zone config, etc.
Any explanation as to why I would get different results?
Regards,
Script console runs with the resources of the PC running the designer, in Perspective those scripts are running on the PC that is hosting the Ignition gateway.
Also - use system.date.* functions not datetime. Maybe something is funky is happening with the datetime library but that would be hard to debug. Use system.date.*
functions and see if you have the same issue.
You are printing datetime objects. Printing causes a string conversion. String conversions of datetime objects use the time zone of the environment where the script is running.
Designer and gateway are different JVMs, and have each their own time zone.
So, you haven't shown any actual evidence that either is wrong.
Edit: And yes, do not use jython's datetime
module--it is unfixably buggy. Always use Ignition's functions or their underlying java types.
Thank you.
I switched to system.date.now(), that at least gives the same timestamp from the button and Script Console. I still find it odd that the output format is different, but I can work with that.
Button
2024-07-19 12:49:36.807
Script Console
Fri Jul 19 12:51:15 EDT 2024
Regards