Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert_tz returns null

This will happen if you haven't loaded the time zone table into mysql.

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

mysql is the name of the built-in database that holds MySQL-specific configuration data.


I found this thread after spending some time trying to figure out why after running the command in the accepted answer (which is the same on MySQL's dev site) the command was unable to convert between timezones such as

SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','MET') AS time

It turns out that on OS X there are two files that cause problems: /usr/share/zoneinfo/Factory and /usr/share/zoneinfo/+VERSION.

The fix... temporarily moving these files to a different location such as /usr/share/zoneinfo/.bak/ allows for the command

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

to fully populate all of the expected timezone information.

This may or may not be a bug in my installed version of MySQL:

$ mysql --version
mysql  Ver 14.14 Distrib 5.6.11, for osx10.6 (x86_64) using  EditLine wrapper

I am also operating in STRICT_MODE.

In any case, I hope this saves a few headaches for anyone searching for the fix.


Apart from Windows environment, You can set Time Zone by

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

In Windows environment,

1. download Time zone description tables from http://dev.mysql.com/downloads/timezones.html

2. Stop MySQL server

3. Put then inside Mysql installation package (ie. C:\Program Files\MySQL\data\mysql)`

4. Start MySQL server

..Your work is finished..

If still you are getting NULL for CONVERT_TZ Download these database tables and insert it into mysql database http://www.4shared.com/folder/Toba2qu-/Mysql_timezone.html

Now you problem will be solved.. :)


MAMP PRO

  1. Open Terminal
  2. cd /usr/share/zoneinfo/
  3. sudo mv +VERSION ~/Desktop
  4. cd /applications/MAMP/Library/bin
  5. sudo ./mysql_tzinfo_to_sql /usr/share/zoneinfo | ./mysql -p -u root mysql
  6. sudo mv ~/Desktop/+VERSION /usr/share/zoneinfo/

1) In Windows, there isn't any data folder now in C:\Program Files\MySQL\ as in other answers.

2) In that case, look for C:\ProgramData\MySQL\MySQL Server 5.x\Data\mysql. Generally this folder hidden and you will not see C:\ProgramData\ some times.

3) Change the Settings in View tab to see Hidden files and Folders as explained here https://irch.info/index.php?pg=kb.page&id=133

4) Stop the MySQL service by searching for "services" in Windows Start button.

5) Then unzip the timezone_2017c_posix.zip and then copy the files in it (copy the files directly, don't copy the whole folder itself), and paste in C:\ProgramData\MySQL\MySQLServer5.x\Data\mysql\

6) For MySQL 5.7, timezone_2017c_posix.zip will just give a .sql file after unzipping and it may not solve the issue. So go ahead and download the zip file for 5.6 even if you are running MySQL 5.7 and copy those files to C:\ProgramData\MySQL\MySQL Server 5.x\Data\mysql\

7) Restart the MySQL server. To check if the CONVERT_TZ () is working, run this sql query.

SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','Asia/Jakarta'); and check for non-null output.


These are the steps to make it work if you're in windows and using MySQL 5.7.

  1. Right click on My Computer/Computer/This PC or whatever the name in your OS and choose Properties.
  2. Choose "Advanced system settings" from the left panel.
  3. Choose "Environmental Variables", enter the complete path name of your MySQL bin directory (generally it will be in, C:\Program Files\MySQL\MySQL Server 5.7\bin).
  4. Open cmd prompt, enter into mysql using mysql -u root -p password.
  5. Enter use mysql to select the MySQL DB.
  6. Download the file "timezone_YYYYc_posix_sql.zip" (In the place of YYYY, substitute the maximum year available in that page like 2017 or 2018) from https://dev.mysql.com/downloads/timezones.html.
  7. Extract it and open the file in text editor.
  8. Copy the contents and execute in the cmd prompt.

On successful completion, you should be able to use CONVERT_TZ and other timezone functions.