Expression tag for UTC

Is there a simple expression that gets the UTC time from whatever the time is locally on the gateway?

My now() returns eastern time.
Planning to use an expression tag for the UTC time in a transaction group.

Java date/time objects, as used in Ignition, are always UTC under the hood. They look like they are in some timezone when displayed/printed/stringified.

Just pass the date object to whatever needs it. That's it.

Worry about timezones when parsing user input, and displaying to a user. Full stop.

1 Like

What do I put in the expression for the expression tag?

Why do you need an expression tag for a transaction group? Just use an expression item within the transaction group, with now(0).

Or just use the builtin feature of transaction groups to populate a timestamp column. (Make sure that column is timezone-aware--the default may not be.)

The time will be in eastern in the table.

So, an MS SQL datetime column? Use datetime2 instead.

1 Like

I don't see datetime2 as an option in the transaction groups or the tag.

I am not permitted to change the gateway datatype for datetimes from datetime to datetime2.


I was hoping for something like:
dateFormat(now(), "UTC")

You mean the database datatype?

In that case, your best bet is to add a utc timezone property to the database connection properties and run the gateway itself in UTC.

If you cannot do that, you will likely have to use java's LocalDateTime in a script to present now() in UTC, which you will have to hand to the DB as a string. Not sure you can do that with actual datetime columns in a transaction group.

Your comment about permission means that whoever you work for is handcuffing you. If they want UTC in a non-timezone-aware column in MS SQL Server, and not have everyone go through their [expletive] to get to their elbow, they need to run the database and the gateway in UTC.

1 Like

I agree.

I need a tag for now though since I don't have permission to switch the gateway to timezone aware or UTC.
Really thought this would work, but it did not:
dateArithmetic(now(),getTimezoneRawOffset((now()),'hour')

Will come back to it tomorrow.

I still have not found an expression that works.
I don't like using dateArithmetic for this because if the gateway is switched, then that will be adding to UTC.
I need something that expressly changes from one timezone to another.

 dateArithmetic(now(),if(dateIsDaylight({[.]nowTag}),4,5),'hour')

This is what I have for now though.

Java's LocalDateTime and ZonedDateTime and TimeZone classes are not available anywhere in Ignition's expression language. You must use a script, and, perhaps, call it from runScript() or objectScript().

1 Like