Easy Chart - DB Pen - UNIX Timestamp - Local Timezone

Hi Guys,

I am logging values from an api call to our sql server database.

One of the values is a timestamp which represents the amount of seconds (not milliseconds!) since the UNIX epoch (January 1, 1970 00:00:00 UTC). This is the capture timestamp for the rest of the data.

Is there an easy way to use this in the easy chart such that the client see’s the data in their own timezone?

Take that number, multiply by 1000L (long), and create a java.util.Date from it. It will show in the user’s local timezone. And it is the datatype you need to assign to Date properties throughout Ignition.

1 Like

Hi Phil, are you suggesting I should do this when I grab the data and put it into my database or somehow do the conversion in the Easy Chart window? Not sure how to ‘create a java.util.Date from it’…

You didn’t provide enough context on your data flow to be more specific, but my expectation was you were scripting your API calls. Given your unix epoch timestamp in a python numeric variable ts, you would convert it to an Ignition date like so:

import java.util.Date

def unix2date(ts):
    return java.util.Date(ts * 1000L)

In Ignition’s expression language, with a custom property ts, it would look like this:

toDate(toLong(1000) * {Root Container.SomeComponent.ts})

Hi Phil,
How to do the opposite way from date to unix epoch timestamp?
My case:
Classic chart Extension Funtions configureChart
x = chart.plot.getDomainAxis()
startX = x.minimumDate
convert to unix epoch timestamp? ----> startX.minimumDate

To be used in:
overlay = XYBoxAnnotation(startX, bottomY, endX, topY, stroke, outline, fill
Thanks in advance
-Kjell

If you have a datetime object (java.util.Date) then the .getTime() method gives you the milliseconds of the unix epoch as a long. In jython, you can use .time as a shortcut property.

1 Like

Ohh so “easy” thanks:slightly_smiling_face:! -Kjell

How is ‘local timezone’ detected by Ignition?
I am also reading in EPOC seconds from a PLC, and when i use the following expression to convert to date, it ends up being an hour forward of actual time.

toDate({[.]Value}*1000)

Note that i am in NZ and daylight savings is active.

system.date.toMillis

(https://docs.inductiveautomation.com/display/DOC79/system.date.toMillis)
I saw this function in 7.9