Request To Fix TimeZone Transformation

I know is a bit late to implement this (or to even request something).

But would be nice to have correct TimeZones additions.
I had an open ticket with ID #147059 if you wanna take a deeper look.

Brief explanation
now1 = system.date.parse("30-03-2025 03:00:00", "dd-MM-yyyy HH:mm:ss")
now2 = system.date.parse("30-03-2025 01:59:00", "dd-MM-yyyy hh:mm:ss")
now3 = system.date.parse("30-03-2025 02:00:00", "dd-MM-yyyy hh:mm:ss")
now4 = system.date.parse("30-03-2025 03:01:00", "dd-MM-yyyy HH:mm:ss")

print now1
print now2
print now3
print now4

h1 = int(system.date.getTimezoneOffset(now1))
h2 = int(system.date.getTimezoneOffset(now2))
h3 = int(system.date.getTimezoneOffset(now3))
h4 = int(system.date.getTimezoneOffset(now4))

print h1
print h2
print h3
print h4

now5 = system.date.addHours(now1, -h1)
now6 = system.date.addHours(now2, -h2)
now7 = system.date.addHours(now3, -h3)
now8 = system.date.addHours(now4, -h4)

print now5
print now6
print now7
print now8
Sun Mar 30 03:00:00 CEST 2025
Sun Mar 30 01:59:00 CET 2025
Sun Mar 30 03:00:00 CEST 2025
Sun Mar 30 03:01:00 CEST 2025
2
1
2
2
Sun Mar 30 00:00:00 CET 2025
Sun Mar 30 00:59:00 CET 2025
Sun Mar 30 00:00:00 CET 2025  # <-
Sun Mar 30 00:01:00 CET 2025  # <-

Strange behaviour.

My request is simply having a nicer way (in scripting) of working with dates in UTC/Timezones.

1 Like

Use Java's ZonedDateTime. Ignition's datetime objects are java.util.Date which are fundamentally UTC under the hood, with no attached time zone. Which means they always use the time zone of the JVM when parsing or printing. When you manipulate them with .addHours(), you are not making a time zone correction, but are altering the underlying UTC value. Don't use .addHours() to make time zone corrections.

Read this entire topic:

Note also that DateTimeFormatter can parse, too.

3 Likes

We use something similar to that in our projects to get the correct utc (at least I think).

But... wouldn't be great to be able to have something like this?
now() # returns current gateway time
now_utc() # returns current utc time
now_client() # returns current client time
And that the corresponding operations work with it's corresponding timezone.

I know that is asking too much, but let me dream

Not possible with java.util.Date, which is what all of Ignition uses. :man_shrugging: