Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

timezone - MySQL Time Zones

Is there an exhaustive list of MySQL Time Zones?

It seems that the valid values for time_zone in MySQL settings are dependent on the host Operating System but I have been unable to find a list of possible values.

I need the time to show Calgary local time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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.


Edit:

Based on a user comment below, if you want to have the timezones automatically updated when you update the system, you first need to allow root to log in without being prompted for a password.

MySQL >= 5.6.6

Execute the following [source]:

mysql_config_editor set --login-path=client --host=localhost --user=root --password

MySQL < 5.6.6

Create a ~/.my.cnf file (if it doesn't exist yet) and add the following:

[client]
user=root
password=yourMysqlRootPW

Then execute chmod 600 ~/.my.cnf to make sure nobody else can read it.

Update script

Add the following script to crontab to be executed once per day:

#!/bin/bash
# Find if there are any timezone files that have been modified in the last 24   
# hours and do not have ".tab" in the name (since these are not timezone files) 
if [ `find /usr/share/zoneinfo -mtime -1 | grep -v '.tab' | wc -l` -gt 0 ]; then
    echo "Updating MySQL timezone info"
    # Note, suppressing STDERR here because of the .tab files above
    # that cause warnings.
    mysql_tzinfo_to_sql /usr/share/zoneinfo 2>/dev/null | mysql -u root mysql
    echo "Done!
"
fi

Remove the echo lines if you don't want any output.

Note: This is (mostly) untested. Let me know if you have any issues and I'll update this answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...