from_unixtime still displaying 19 hours past expected time

70 Views Asked by At

I currently have some cleaned data with new columns and formatted correctly, however the clause regarding from_unixtime is stumping me. I have tried the following:

from_unixtime(`time@timestamp`, '%H:%i:%s') AS Date_12am,

Is the main statement that I'm trying to get to display '00:03:02' where the time is starting at midnight for a 24hr period. I'm in the eastern timezone so I first tried recommended shell commands and verified that my MySQL workbench and config files were also the correct time. I tried setting the above clause to local time, 'SET AS', 'CONVERT', and playing around with the format to see if I could get the right combination. Nothing is seeming to work, so any help in this matter will be greatly appreciated!

SELECT 
from_unixtime(`time@timestamp`),
data_format_0, data_format_1, data_format_2, data_format_3
FROM my_table_name;

/*The above is for checking to see if data is populated.*/

ALTER TABLE my_table_name
CHANGE data_format_0 OneMinuteAverage DOUBLE,
CHANGE data_format_1 ProductName VARCHAR(250),
CHANGE data_format_2 measruement1 DOUBLE,
CHANGE data_format_3 measurement2 DOUBLE;

SELECT 
from_unixtime(`time@timestamp`, '%Y_%m_%d') AS `Date`,
from_unixtime(`time@timestamp`, '%H:%i:%s') AS Date_12am,
ROUND(OneMinuteAverage, 2) AS OneMinuteAverage, 
ProductName, measurement1, measurement2
FROM my_table_name;

Results from above query displaying unix time

1

There are 1 best solutions below

4
ysth On

unix_timestamp returns a localtime for the given epoch seconds, where local means in the timezone of your session/connection, which defaults to your database's default time zone (but some ORMs or clients will set for you based on their environment). So it is expected that unix_timestamp(1705968054) would return 19:00:54 when in American Eastern Standard Time. I recommend avoiding unix_timestamp and all other functions that use the session time zone. Instead you can do:

select date_format('1970-01-01 00:00:00' + interval 1705968054.906 second, '%H:%i:%s')