Behind the scene: Basically I operate an app that heavily relies on time calculation. We store unixtime in the database according to the user's timezone. Now to perform specific operations we need to convert that time from the native timezone to UTC. We recently transferred the application from another server It was working perfect but when we moved the application to a new server. The calculation that was written in Server Side Langauge was accurate but those calculations in which we wrote the logic in stored procedure output wrong date while converting it from unix_time to timestamp. Now you might question how do you know that output is wrong? Because I ran the same query over 2 different servers and the one that I am talking about returned wrong output.
The result from server having bug.
select convert_tz(from_unixtime(1585804204),'-04:00','+00:00')
----Output---
convert_tz(from_unixtime(1585804204),'-04:00','+00:00')
2020年04月02日 11:10:04
The accurate result:
select convert_tz(from_unixtime(1585804204),'-04:00','+00:00')
----Output---
convert_tz(from_unixtime(1585804204),'-04:00','+00:00')
2020年04月02日 05:10:04
If you put the same value of unixtime in the epoch converter (https://www.epochconverter.com/) you will get the value above mentioned select timezone America/New York.
I have also checked that clock of the host machine is fine. Please tell me how to resolve this error. I am using MariaDB version 10.3
-
According to dbfiddle.uk/… the answer to your query is: 2020年04月02日 10:10:04. FWIW, I think a better approach is to store timestamps in UTCLennart - Slava Ukraini– Lennart - Slava Ukraini2020年04月04日 16:51:27 +00:00Commented Apr 4, 2020 at 16:51
-
Yes, Thank you very much the issue has been resolved.Hasnain– Hasnain2020年04月05日 09:14:39 +00:00Commented Apr 5, 2020 at 9:14
-
1Post an answer that describe what the solution was and accept it. It will help people with similar issues in the futureLennart - Slava Ukraini– Lennart - Slava Ukraini2020年04月05日 09:18:11 +00:00Commented Apr 5, 2020 at 9:18
2 Answers 2
Use TIMESTAMP
, not DATETIME
. Then set each client to the timezone where it resides, or to UTC for the main server.
That way, the conversions will be done automatically. That is, no conversion code is needed.
Linux timestamp was being stored in DateTime format and its conversion works according to the timezone of the connection of Database. So in order to resolve this issue, I have set the timezone of the connection to the timezone in which unix_time were stored in database.