Create a gist now

Instantly share code, notes, and snippets.

How to load MySQL time zone tables from Mac OS time zone files

The easiest way to load the Mysql Time Zone tables from your Mac OS time zone fields is via this command:

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

However, on many systems like mine that will fail because some of the time zone information is incompatible with the database schema for the time zone tables.

Therefore, you'll want to load the time zone information into a text file and edit it to only include the time zones you need. (Or, attempt to find the data breaking the import.)

mysql_tzinfo_to_sql /usr/share/zoneinfo > zone_import.sql

I edited the file to only include American time zones and UTC because they were all I need to test an app with.

After updating the zone_import.sql file, run this command to dump them to MySQL:

cat zone_import.sql | mysql -u root mysql

Once the file is imported, you'll need to restart MySQL. If you used HomeBrew to install it, these are your commands:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Now that you have restarted MySQL, there is a simple test to check that the time zones are loaded. Launch the MySQL command line:

mysql -u root

Note: add -p if you need to enter a password to access your mysql db as root.

Once at the MySQL prompt, run this select:

select CONVERT_TZ(now(), 'UTC', 'America/New_York');

If either time zone does not exist in your time zone table, you will get back a 'NULL' value in the converted column. If both exist, you'll get back a timestamp and you're ready to go.

@flagoworld

Or you can do what the dude at the bottom of the mysql docs page says... Worked perfectly for me.

mysql_tzinfo_to_sql /usr/share/zoneinfo | sed -e "s/Local time zone must be set--see zic manual page/local/" | mysql -u root mysql

Note by default you will probably have to /usr/local/mysql/bin/mysql_tzinfo_to_sql...etc

@ryantuck

+1 @flagoworld that command worked for me on ubuntu 14.04.

@gorkapit

+1 @flagoworld worked like a charm on OSX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment