Handling different timezones on both frontend & backend


We are in the beginning stages of a project that will need to handle time-based calculations in different timezones, but in a singular Ignition Project.

For example, let’s say I need to trigger a Python script at midnight, but I need to trigger the script at midnight UTC+0, then UTC+2, UTC+8 etc.
The naive solution would be just storing the number of hours relative to UTC needed for each calculation/script - but there are things like Daylight Savings Time & more. (yourcalendricalfallacyis.com)

I know Ignition handles things like Daylight Savings Time automatically, but I can’t figure out how to use it in an expression.
If I write midnight(now(0)), I get midnight for the Gateway’s timezone only.
An ideal scenario would be to just specify for which timezone I want to get midnight for - like midnight(now(0), "Europe/Berlin")

If it weren’t for Daylight Savings Time, I would just “hardcode” the value.
But that’s not a good idea.

Also, as far as I can tell, Perspective Session can either use the Gateway’s timezone, the client’s timezone or a timezone picked by hand.
The problem is, we need to be able to change this value on the frontend, so the operators can switch between their local time & the time of the project they are monitoring at the moment, so for example the Time Series Chart displays the time accordingly.

To summarize, our problem is:

  1. we can’t just hardcode time offsets for the calculations & scripts, we need a way to get that offset from a library, so we can be sure that things like Daylight Savings Time are taken care of
  2. we need to be able to change in which timezone the time on a graph is displayed dynamically on the frontend

Thanks for any suggestions!

P.S.: I know that a “cron” feature is already on the roadmap, but unless 8.1.6 is coming out by the end of the week, I need to solve this in a different way for now :smiley:
P.P.S.: Ideally, we would like to not use Python for this, since it is by far not the fastest language & we will need to run a lot of calculations that are more complex than my simplified midnight example

Don’t use any python libraries for this task. Python’s datetime implementation in Jython is flaky.

You should familiarize yourself with Java’s various date and time classes. The core for Ignition is java.util.Date, which internally is milliseconds of the Unix Epoch in UTC. Ignition date/time values convert to/from the local timezone automatically because that’s what Java’s Date class does. Java’s Calendar and TimeZone classes can be used to render or convert from other time zones programmatically. This may be all you need. Just keep in mind that the internal representation is always UTC.

Java has a newer set of date/time classes with more features and more precision. If you need it (I haven’t), start with Instant.

For your application, I would highly recommend running the gateway server itself in UTC, and if the database is MS SQL Server, you pretty much have to run it in UTC. (Microsoft’s column types that are timezone-aware are under-constrained and poorly supported by JDBC.)

Thank you for your reply! :slight_smile:

I’m not sure I understand it correctly though - I wanted to know if there’s a possible solution that we can use within an Expression.
From what you wrote I understand that we should write our own Java module to get the functionality we need - is there really no other way?


No, no java module required, just use of java datatypes within Ignition’s scripting. Expressions are already using java datatypes. Which means they are already functioning with UTC at the core. (When java dates/times are serialized in a network connection, the serialized form is UTC, and each end of the network connection localizes separately.)

Run your gateway(s) and database(s) with UTC. Set up all gateway time triggers to work from UTC times, or dynamically check (in scripting) with the timezone manipulations I referenced. Let your clients use local time.

I recommend you run some experiments with a gateway datetime expression tag that just uses now(), and a test Vision project with time behavior set to client timezone. Make a single window that shows that time tag in a label. Start Vision clients from multiple client machines with different timezone settings.

Note: Ignition uses Jython for its scripting. In addition to having most of the python standard library, jython can use java classes as if they were python.