Converting the different time formats

Converting from DateTime to Unix Epoch Time to Ticks and back

 

There are many ways to store DateTime values within any system. The most common are usually normal DateTime stamps, Unix Epoch Time and Ticks. Before we get into how to compare and convert them, we must understand the differences between them.

 

Date Time – A date time stamp is what most people see when we talk about time in general. A full TimeStamp looks like: ‘September 13, 2012 10:50:00AM’

 

Unix Epoch Time: UET is a common scenario when we are trying to save space, or compare times in a very quick manner. UET is calculated by taking the current date and time and calculating the number of seconds from “Unix Time” which is January 1, 1970 00:00:00.  For example, the date above (September 13, 2012 10:50) in UET would be 1347533400.

 

Ticks: Ticks represents the date in the number of milliseconds from 1, 1, 001, 00:00:000. So, if we were to take the above date and translate it to ticks it would be: 634831302000000000.

 

Converting from DateTime to Unix Epoch time in Verivo:

 

If you have a normal timestamp and need to compare it to Unix Epoch Time, you can use Lua scripting to accomplish this. The easiest way is to run the following line of code against the date.

 

local c = thisview:getAllControls

local d = c.DateToConvert:getValue()

 

local uet = tonumber(os.time(d))

 

After running os.time against the timestamp, lua will automatically be using the integer Unix Epoch format. Alternatively, if you want to get today’s date in Unix Epoch, you can run os.time() with no arguments.

 

Converting from Unix Epoch to Ticks in Verivo

 

Similar to using os.time(), you can also use some lua scripting to get the ticks value: 

 

local c = thisview:getAllControls

local d = c.DateToConvert:getValue()

 

local uet = tonumber(os.time(d))

local dateInTicks = (uet * 10000000) + 621355968000000000

 

This script takes the Unix Epoch in seconds, converts them to milliseconds, and adds the time from 1/1/001 to 1/1/1970.

 

Going back

 

In reality, going back from Ticks to DateTime is just a reverse process; this assumes you are starting at ticks and working backwards:

 

local c = thisview:getAllControls

local dateInTicks = c.DateInTicks:getValue()

 

 

-- this section goes from ticks to Unix Epoch

local uetFromTicks = (dateInTicks – 621355968000000000) / 10000000

 

-- this section goes from Unix Epoch to TimeStamp

local DateTime = os.date(‘%x’, uetFromTicks)

 

Where do we use all of this

If you decide to use local data store, SQLite will convert all dateTimes to ticks. This is because SQLite has no concept of a DateTime field (in order to keep the system light) and using integers to store the values. However, by default, Lua uses the Unix Epoch Time in order to store dates.

 

Our client can handle, and show both values, but if you decide to do comparisons in a way that requires scripting, you will run into issues where the literal values don’t match and your comparisons will fail. By utilizing either ticks or Unix Epoch Time (the preferred method is ticks in case you want to store the value) you will be able to successfully handle dateTime comparisons.

Have more questions? Submit a request

1 Comments

  • 0
    Avatar
    rbalasu

    Anthony - what if I want to show the time in the local timezone of the device, given the date/time I received from a service in in UTC format?

Please sign in to leave a comment.
Powered by Zendesk