-1

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

6
  • 3
    What does show variables like '%time_zone'; say?
    – Barmar
    Commented Feb 1 at 20:45
  • 1
    This sounds like a time zone issue. Is your server on UTC?
    – tadman
    Commented Feb 1 at 21:13
  • what type is the time@timestamp column? can you show some sample data?
    – ysth
    Commented Feb 2 at 0:48
  • @ysth the time@timestamp column is a REAL32 variable coming from easybuilder pro SQL query. @tadman and @Barmar my 'time_zone' value is 'SYSTEM' and my 'SYSTEM_TIME_ZONE' is Eastern Standard Time (my current timezone).
    – Joey
    Commented Feb 2 at 12:15
  • PS - thank you for catching my mistake in the initial code posting.
    – Joey
    Commented Feb 2 at 12:28

1 Answer 1

0

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')
4
  • This worked, but changed all the times to 00:00:54. It may have been a little unclear on my end, but I'd like the time to count up over a 24hr period where it starts at 00:00:54 and ends at 23:59:41 for example.
    – Joey
    Commented Feb 2 at 15:14
  • what counts up? if it wasn't clear, the 1705968054.906 in my example should be your variable.
    – ysth
    Commented Feb 2 at 15:29
  • The time counting up starting at midnight. Currently it starts at 19 and counts up every minute (the unix time). I'd like to keep the data, want to efficiently convert the leading 19 to 00 and count up each minute.
    – Joey
    Commented Feb 2 at 15:35
  • using 1970 + interval and date_format should give you a utc time, not an eastern time, so the 19 should be fixed. for the rest, I'm still not getting it, it would help if you included a full runnable example in your question (create table, inserts, full select). show what you are doing well enough that we can try it too
    – ysth
    Commented Feb 2 at 15:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.