This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-17. For the latest, interactive version please visit https://mariadb.com/kb/.

convert_unixtime outputs wrong date time

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.

Answer Answered by Ian Gilfillan in this comment.

FROM_UNIXTIME expresses the value in the current time zone. So your servers most likely have different time zone values:

SET time_zone = '+2:00';

select convert_tz(from_unixtime(1585804204),'-04:00','+00:00');
+---------------------------------------------------------+
| convert_tz(from_unixtime(1585804204),'-04:00','+00:00') |
+---------------------------------------------------------+
| 2020-04-02 11:10:04                                     |
+---------------------------------------------------------+

SET time_zone = '+4:00';
select convert_tz(from_unixtime(1585804204),'-04:00','+00:00');
+---------------------------------------------------------+
| convert_tz(from_unixtime(1585804204),'-04:00','+00:00') |
+---------------------------------------------------------+
| 2020-04-02 13:10:04                                     |
+---------------------------------------------------------+
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.