Report this

What is the reason for this report?

convert_unixtime outputs wrong date time

Posted on April 4, 2020
Hasnain Haider

By Hasnain Haider

Product Engineer

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.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

First, ensure that the timezone settings of your new server match those of the previous server. It’s possible that a difference in configuration or missing timezone data is affecting the conversion.

To update timezone information, you might need to install the tzdata package for your system. Here are some common distribution-specific commands:

For Ubuntu/Debian:

  1. sudo apt update
  2. sudo apt install tzdata

For CentOS/RHEL:

  1. sudo yum update
  2. sudo yum install tzdata

Also, if you’re using MySQL or a compatible database, ensure that your server’s time_zone and system_time_zone variables are configured correctly.

You can check the current values with these SQL commands:

  1. SHOW VARIABLES LIKE 'time_zone';
  2. SHOW VARIABLES LIKE 'system_time_zone';

If needed, update the time_zone variable or configure it in your MySQL configuration file (e.g., /etc/my.cnf), then restart the server.

If the issue persists after checking and adjusting these settings, please provide more information about your server environment, including the database engine and version, operating system, and any related configurations.

For more details on timezone configuration, you can visit the DigitalOcean Time Zone Configuration guide.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.