Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdrago999/ca09953cf7fb6769565dcfd1beaea449 to your computer and use it in GitHub Desktop.
Save jdrago999/ca09953cf7fb6769565dcfd1beaea449 to your computer and use it in GitHub Desktop.
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.

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