Datetime.now() in transform script returning wrong time

I am using datetime.now() in a transform script to make some transformations based on the current time.
In the script the returned time is off by 1 hour.

datetime.now() in script console returns: 2021-11-11 15:25:400
datetime.now() in transform script returns: 2021-11-11 16:25:00

Does this have to to with daylight saving time or some local time settings? I am based in Germany.

Hmm, well one difference is that in the transform is running on your gateway but the script console is running on the PC you launched the Designer on.

Thanks for your response!
I just checked Gateway time and PC time are in sync.

Check the [System]Gateway/CurrentDateTime tag specifically; the Gateway JVM itself could have a different time than the underlying system (for a variety of reasons).

Thanks for that hint. But now I am only more confused.
[System]Gateway/CurrentDateTime shows the same time as my PC.In the property browser the time is still correct, but as soon as I visualize the time on a view the time is off by one hour.

Please have a look at the screenshots:

The ‘Z’ at the end refers to ‘Zero offset hour’, or UTC (Or Zulu time, if you’ve ever heard the phrase).

ISO 8601 Standard

t = system.date.now()

print t
print t.toInstant()

Output:

Fri Nov 12 07:11:18 EST 2021
2021-11-12T12:11:18.656Z

I can see the difference. But how can I know what time is actualy being used.
In the text propery the time is shown without formatnig it to Zulu time. On the view it suddenly is displayed as Zulu time.

And why is the Zulu time differnt if I use a transform (which should be running on the Gatway) and when I use a tag binding to [System]Gateway/CurrentDateTime.

edit: And my problem was not the Zulu time, I think. my Problem is that my time that is used in a Script- Transform with datetime.now() adds 1 hour to the actual local time.

edit2: I guess for this specific case I could switch from datetime.now() to system.date.now(). But I would really like to understand why both methodes return different times.

It’s because datetime is not time zone aware. It’s also as you have found, a huge pain to use. I recommend using java date types whenever possible.

3 Likes

:100:

How do you force the use of Java date types from an Ignition Python script?

Ignore the existence of datetime. Use system.date functions, or native java types directly (e.g. Calendar for more complex operations than system.date provides).

If you’re using a library that returns datetime types, avoid it or refactor it or convert to java types as soon as possible.

2 Likes