Tableau Epoch to DateTime with TimeZone - tableau-api

Data has hour field (String datatype). The timestamp is in milliseconds. This is working -
DATEADD('second',INT(INT([Hour])/1000),DATETIME('1970-01-01'))
However, this is NOT WORKING -
DATEADD('hour',-7,(Date("1/1/1970") + (INT(INT([Hour])/(1000*86400))))
The above is returning NULL. -7 is to adjust for my Timezone.

Was able to get it done. May help someone.
Changed the second one to this -
DATEADD('hour',0,(DATETIME("1970-01-01") + INT((INT([Hour])/(86400*1000)))))

As a reference, extract from Tableau own knowledge base:
To convert the field to UTC time, use the following calculation:
DATEADD('second', [Unix time field], #1970-01-01#)
To convert the field in Unix time to a different time zone, use the
following calculation:
DATEADD('minute', INT([Unix time field]/60 + ),
#1970-01-01#)
For example, to convert the field in Unix time to India Standard Time
(IST), use the following calculation:
DATEADD('minute', INT([Unix time field]/60 + 330), #1970-01-01#)

Related

Db2: How to convert Unix / epoch time to timestamp?

I have a BIGINT value which represents a UNIX timestamp (epoch). How can I convert it to the built-in TIMESTAMP type?
As example, I want to turn 1611140400 into the related date and time. TIMESTAMP_FORMAT does not work.
You can use datetime arithmetics in Db2 and Db2 on Cloud. For Db2 on Cloud (which is running in UTC):
VALUES (TIMESTAMP('1970-01-01') + 1611140400 seconds)
Epoch is seconds since January 1st, 1970 GMT / UTC. Thus, adding your number as seconds to that date will give:
2021-01-20 11:00:00.0
If you are running in a different timezone, you need to take care of it, e.g.:
VALUES (TIMESTAMP(‘1970-01-01-00.00.00.000000’) + 1611140400 seconds + current timezone)

Function to support addition & subtraction of both numbers & date

Need of a generic Postgres function to support addition & subtraction of both number & date (timestamp without time zone).
It's expected to support number + number, date + number, date - number like formats. Type need be identified at runtime if possible. Is it feasible?
PostgreSQL has a number of operators for date arithmetic. To name the most important options for addition:
date + integer → date
date + time without time zone → timestamp without time zone
date + interval → timestamp with time zone
timestamp without time zone + interval → timestamp without time zone
timestamp with time zone + interval → timestamp without time zone
Subtraction works similar.
Multiplication and division exist for interval and double precision.
That works quite the same as in Oracle, so code should not be hard to port.
Some differences:
While Oracle's timestamp with time zone stores the time zone information along with the data, PostgreSQL doesn't. Rather, it converts the timestamp to UTC before storing it, and upon display, it is converted to the timezone setting active in the database connection.
Oracle's date (strangely) has fields for hour to second and is best translated to timestamp(0) without time stamp.
Oracle has two interval data types, but they both can be represented as PostgreSQL's interval.
Oracle does not have an integer data type, so you have to translate Oracle's number to integer for PostgreSQL. This might present a problem if you add numbers with a fraction – that would have to be translated to timestamp + interval.
Replace the Oracle-specific sysdate with clock_timestamp().

Batch conversion of epoch to datetime in TQL query

Converting epoch time to SQL datetime format. I am trying to convert the 35 000 records received from another table (other db) with epoch timestemp to a new table with SQL datetime format. Also I will need to have this updated on a daily basis so one time conversion is good but I am also open to other suggestions.
I never worked with epoch and I am not sure how to go about it.
Also later on I want to use it in SSRS with correct datetime so I am not sure should I convert it before the transfer to new table or to do it in SSRS?
Any ideas?
Presuming that the epoch timestamp you have is in seconds:
DATEADD(SECOND, epoch_col, '19700101')
This will add the epoch seconds to the start of 'epoch time' (01-01-1970 00:00:00) and give you a DATETIME.
Example with output:
SELECT DATEADD(SECOND, 1571994774, '19700101')
2019-10-25 09:12:54.000
If you have an epoch timestamp in milliseconds just use this variation:
DATEADD(MILLISECOND, epoch_col, '19700101')
In terms of your other question about when to convert the value; My view is that it would be preferable to store the value in a DATETIME column at point of insertion rather than storing the epoch value and converting it upon use.
This is just an opinion though and not a recommendation.

postgres time conversion hh:mm:ss.us to hh:mm

Using PostgreSQL database for my attendance application.
I have a table with IN and out times (hh:mm:ss.us format).
When I subtract the times (OUT -IN) to calculate the working hours, results are not as expected due to precision.
If IN Time is "22:12:56.09"
& OUT TIme is "22:14:06.06" the difference considering only HH:mm should be 00:02 but it actually shows "00:01:09.97" which becomes "00:01" in excel using only HH:mm.
I am trying to do the time conversion from hh:mm:ss.us to hh:mm (time format) so that I can subtract the time and get the desired output.
I have done similar things in SQL Server but I did not find any function in PostgreSQL. Please advise.
First you need to truncate the seconds. Then subtract to get desired result
select
to_char(
(
to_char('22:14:06.06' :: time, 'HH24:MI'):: time -
to_char('22:12:56.09' :: time, 'HH24:MI'):: time
),
'HH24:MI'
)
Result: 00:02
General Solution:
select
to_char(
(
to_char(out, 'HH24:MI'):: time - to_char(in, 'HH24:MI'):: time
),
'HH24:MI'
)
Here the purpose of to_char() is to format result to hours:minutes and not to include seconds.
Postgres includes seconds in interval by default.
You can use the date_trunc function with timestamp.
It would work something like this:
select date_trunc('minute', out) - date_trunc('minute', in)
This would set a minute level precision on the timestamp and convert HH:mm:ss to HH:mm:00

How to i get the difference of time in hours for a file created time and the current time using perl

How to i get the difference between a file created time and the current time in hours
stat($file)->mtime gives you the last modification time in seconds. Subtract that from time and divide it by 3600 ... that should do the trick.
For calculations involving time or dates you can always rely on DateTime!
For your specific cuestion, you can use the constructor from_epoch to convert from Unix timestamp to DateTime.