Mysql2 gem casting datetime objects from remote db to local (server) time

I have been above and over the documentation of the excellent Mysql2 gem (otherwise).

I have a Rails application for products hosted on a server running in UTC. The config.time_zone application is set to Eastern Time (US and Canada).

We have an internal application with support for MS Access (which, fortunately, uses the MySQL database), to which the aforementioned Rails application is connected to synchronize product data, production schedules, etc. This MySQL server cannot be migrated to the cloud due to application restrictions and, of course, we cannot change our timezone processing to UTC - all in EST.

When I connect from our Rails application to our local database to retrieve data, Mysql2 (by default) transfers all the native data to digestible Ruby objects. However, the only way I can get the Rails application to interpret the dates correctly is to send: cast => false during the Mysql2 query command, and then run Time.zone.parse (string) in the response processing, This It requires the use of your own Ruby object, which is much less efficient than the casting functions built into the gem.

I tried to pass the database_timezone and application_timezone parameters mentioned in the documentation , but: local characters do not have the expected effect of using the application. Config.time_zone property.

thehost.query("SELECT NOW() AS n;", :symbolize_keys => true).each { |result| p result[:n] }

2015-05-19 16:28:41 +0000

thehost.query("SELECT NOW() AS n;", :symbolize_keys => true, :database_timezone => :local, :application_timezone => :local).each { |result| p result[:n] }

2015-05-19 16:30:11 +0000

thehost.query("SELECT NOW() AS n;", :symbolize_keys => true, :database_timezone => :utc, :application_timezone => :local).each { |result| p result[:n] }

2015-05-19 16:30:28 +0000

thehost.query("SELECT NOW() AS n;", :symbolize_keys => true, :database_timezone => :local, :application_timezone => :utc).each { |result| p result[:n] }

2015-05-19 16:30:57 UTC

thehost.query("SELECT NOW() AS n;", :symbolize_keys => true, :database_timezone => :utc, :application_timezone => :utc).each { |result| p result[:n] }

2015-05-19 16:31:19 UTC
+4

All Articles