1/10/2016 - 7:20 PM

Time zones load information

Time zones load information

Load timezones tables with information: mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql


By default, (at least on Debian-based installations) no time zone data is loaded into MySQL. If you want to test if they are loaded, try executing:

SELECT CONVERT_TZ('2012-06-07 12:00:00', 'GMT', 'America/New_York');
If it returns a DATETIME (in this case 2012-06-07 08:00:00), you have time zones loaded. If it returns NULL, they aren't. When not loaded, you are limited to converting using offsets (e.g. +10:00 or -6:00).
SELECT COUNT(*) FROM mysql.time_zone_name;
In the example above, if the function returns 0, it means the time zones are not loaded.

This should work fine in many cases, but there are times when it is better to use named time zones, like for not worrying about daylight savings time. Executing the following command loads the time zone data from the system (Unix-only. I'm not sure what the equivalent Windows command would be):

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

If you need to continually rely on MySQL time zones, the above command should be executed every time the system time zone is updated. You could also just add it to a weekly or monthly cron job to do it for you automatically.

Then, to view a list of time zones, just do the following:

USE mysql;
SELECT * FROM `time_zone_name`;
Note, the time zone info takes up about 5 MB in MySQL. If you ever want to un-load the timezone info, just execute the following and restart MySQL:

TRUNCATE `time_zone` ;
TRUNCATE `time_zone_leap_second` ;
TRUNCATE `time_zone_name` ;
TRUNCATE `time_zone_transition` ;
TRUNCATE `time_zone_transition_type` ;
Do not DROP these tables or bad things will happen.


Change timezone
Veja como é possível ajustar o fuso, referindo-se ao nome:

SELECT * FROM mysql.time_zone_name WHERE Name LIKE '%Fortaleza%';
| Name                    | Time_zone_id |
| America/Fortaleza       |          113 |
| posix/America/Fortaleza |          710 |
| right/America/Fortaleza |         1307 |
3 rows in set (0.00 sec)
SET time_zone='America/Fortaleza';
Query OK, 0 rows affected (0.00 sec)
SELECT @@time_zone;
| @@time_zone       |
| America/Fortaleza |
1 row in set (0.00 sec)