Set Session Time Zone While Connecting to Mysql #13494

azitabh opened this Issue Dec 26, 2013 · 7 comments

8 participants


Mysql sets the time zone for all the connections as SYSTEM which is the time zone of the system where mysql-server is running. In my case it is +05:30. It stores all the values in UTC while it accepts and returns all values in the timezone set as session time_zone.

Default timezone for rails4 is :local whereas AR default for rails4 is UTC . So when I set a timestamp field by doing, AR converts the time into UTC and sends it to Mysql. Mysql assumes the incoming value to be in +05:30 and hence converts it to UTC before storing. So, the conversion to UTC is happening twice.

This problem can be solved by letting AR know what Mysql session time_zone is.
config.active_record.default_timezone = :local

But the problem is if mysql server is to be moved to a separate machine with different time zone setting, our rails application needs to aware of this change and config.active_record.default_timezone must be changed accordingly for a consistent behavior.

This dependency can be removed if AR sets the session time_zone to whatever is set as config.active_record.default_timezone while connecting to the mysql server.
It can be done at the same time when AR sets variables like wait_timeout and etc.

Please point out if I am missing something.


Aaron and I are taking this one!


@eric-chahin you can have a look at #13663


This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@azitabh azitabh added the stale label May 27, 2014

I thought this was being worked upon.


@azitabh My apologies. I should have posted here that we did not end up working on this issue, and I can no longer work on any OSS project due to a company contract I recently signed.


I think this feature is needed, rails timezone is not always same with database timezone.

@matthewd matthewd removed the openacademy label Jun 26, 2015

This issue is very important.
If I use STR_TO_DATE in MySQL for example, I will end with the MySQL timezone in the datetime field, while in Rails I will end with the Rails app timezone when generating a datetime. There is no strict reason why this two timezone should match, so this feature is important.

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