31

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.

Gary
  • 410
  • 1
  • 6
  • 8
  • `America/Edmonton` I think is the named timezone for the area nearest Calgary. It works for PHP's `date_default_timezone_set` function. – racl101 Feb 14 '14 at 01:18
  • https://www.timetemperature.com/time-zone-maps/world-time-zone-map.shtml – JayRizzo Aug 30 '23 at 18:32

5 Answers5

58

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!\n"
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.

Community
  • 1
  • 1
Mike
  • 23,542
  • 14
  • 76
  • 87
  • 6
    This is one of the most informative posts on the subject. Thank you Mike! – dotancohen Mar 19 '14 at 08:55
  • Thanks @Mike I was getting `Unable to load` errors running the command. run the command with a force switch should do it: `/usr/share/zoneinfo | mysql -u root -p --force mysql` – snowbound Aug 17 '14 at 05:57
  • @snowbound The reason it is saying `Unable to load` is because there are some files in `/usr/share/zoneinfo` that are *not* time zone files. I got this error (on Debian) with `zone.tab` and `iso3166.tab`. If you open them up with a text editor, you will see that they are just plain text files while the actual time zone files are binary. You can safely ignore this warning (it tells you it is skipping those files anyway). Using `--force` is likely a bad idea. – Mike Feb 04 '15 at 21:30
  • @mcmillab [This question](http://stackoverflow.com/questions/713319/mysql-mysql-tzinfo-to-sql-program) might help. – Mike Aug 26 '16 at 06:06
  • This post has two very important pieces that are missing from the 20 other answers on SO that i've just perused. 1) repopulating timezone tables every time the system timezone is updated 2) warning against dropping mysqls built in timezone tables. If possible, could you bold those two lines in your answer to make them stand out? Also is there an easy way to determine if the system timezone has changed instead of running a weekly chron job? Excellent answer. – Govind Rai Sep 16 '16 at 07:20
  • 1
    @GovindRai Updated as requested. – Mike Sep 17 '16 at 00:26
22

From the MySQL 5.7 documentation (emphasis mine):

timezone values can be given in several formats, none of which are case sensitive:

The value 'SYSTEM' indicates that the time zone should be the same as the system time zone.

The value can be given as a string indicating an offset from UTC, such as '+10:00' or '-6:00'.

The value can be given as a named time zone, such as 'Europe/Helsinki', 'US/Eastern', or 'MET'. Named time zones can be used only if the time zone information tables in the mysql database have been created and populated.

It should be noted that the MySQL timezone variable's default setting is SYSTEM at MySQL startup. The SYSTEM value is obtained from an operating system setting (e.g. from the file that is referenced by the symlink /etc/localtime)

MySQL's default timezone variable can be initialised to a different value at start-up by providing the following command line option:

--default-time-zone=timezone

Alternatively, if you are supplying the value in an options file, you should use the following syntax to set the variable:

--default-time-zone='timezone'

If you are a MySQL SUPER user, you can set the SYSTEM time_zone variable at runtime from the MYSQL> prompt using the following syntax:

SET GLOBAL time_zone=timezone;

MySQL also supports individual SESSION timezone values which defaults to the GLOBAL time_zone environment variable value. To change the session timezone value during a SESSION, use the following syntax:

SET time_zone=timezone;

In order to interrogate the existing MYSQL timezone setting values, you can execute the following SQL to obtain these values:

SELECT @@global.time_zone, @@session.time_zone;

For what it's worth, I simply googled mysql time_zone configuration valid values and looked at the first result.

David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
6

You can run this query to get a complete list of the timezones MySQL supports:

SELECT Name
FROM mysql.time_zone_name
ORDER BY Name

(Found on this page: https://www.plumislandmedia.net/mysql/time-zones-mysql/)

Sean the Bean
  • 5,222
  • 5
  • 38
  • 40
3

An exhaustive list of timezones can be downloaded from MySQL's website as database tables that get imported into MySQL server.

For Calgary time you can specify UTC offsets as

set time_zone = '-6:00';
sikander
  • 2,286
  • 16
  • 23
  • 1
    Note that -6 for Calgary is due to Daylight Savings Time and the Standard Time is -7 – sikander Mar 21 '12 at 16:05
  • 17
    NEVER use numeric offsets when setting timezone. It breaks TIMESTAMP columns when used with daylight savings times. Always use "Continent/City" format. – Josef Kufner Feb 22 '14 at 14:50
0

It should be noted that the MySQL timezone variable's default setting is SYSTEM at MySQL startup. The SYSTEM value is obtained from the operating system's GLOBAL time_zone environment variable.

MySQL's default timezone variable can be initialised to a different value at start-up by providing the following command line option:

--default-time-zone=timezone

Alternatively, if you are supplying the value in an options file, you should use the following syntax to set the variable:

--default-time-zone='timezone'

If you are a MySQL SUPER user, you can set the SYSTEM time_zone variable at runtime from the MYSQL> prompt using the following syntax:

SET GLOBAL time_zone=timezone;

MySQL also supports individual SESSION timezone values which defaults to the GLOBAL time_zone environment variable value. To change the session timezone value during a SESSION, use the following syntax:

SET time_zone=timezone;

In order to interrogate the existing MYSQL timezone setting values, you can execute the following SQL to obtain these values:

SELECT @@global.time_zone, @@session.time_zone;
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Matt G
  • 1,332
  • 2
  • 13
  • 25