Rails 3 convert time of created_at before saving to MySQL

How can I prevent this? This happens even if I do not set
config.time_zone

Basically Rails 3 subtract 7 hours from the current time before saving
it to MySQL.

I never had this issue in Rails 2.3.5.

How would I disable this functionality?

Sharkie

I understand a little better now. I am in Bangkok time. Rails converts
time into UTC before saving it to MySQL.

I do not wish for this behavior. I would rather have created_at stored
as Bangkok time as was always the case in Rails 2.3.5.

I never wish for this UTC time.

Further investigation, MySQL is running in ICT (Bangkok Time)

mysql> select distinct @@system_time_zone from user;
±-------------------+
| @@system_time_zone |
±-------------------+
| ICT |
±-------------------+
1 row in set (0.00 sec)

Sharkie

Quoting Sharkie L. [email protected]:

How can I prevent this? This happens even if I do not set
config.time_zone

config.time_zone = ‘Bangkok’

or the appropriate value among the output of:

rake time:zones:local

HTH,
Jeffrey

Did you restart your server? Did you properly migrate
config/environment.rb
to config/application.rb. I.e., change

Rails::Initializer.run do |config|
config.time_zone = ‘Bangkok’
end

to:

config/application.rb

module YourApplicationName
class Application < Rails::Application
config.time_zone = ‘Bangkok’
end
end

It sounds you haven’t completed the migration completely.

Jeffrey

Quoting Sharkie L. [email protected]:

I tried this, and it does not work and seems to have no effects.

What happen now is that created_at is stored in MySQL in UTC time, but
when retrieved and displayed in Rails it shows in Bangkok time. It is
really no problem with all new data.

However, with legacy data in MySQL which have been stored in Bangkok
time for the past 3 years, Rails now displays them incorrectly.

Jeffrey L. Taylor wrote:

Quoting Sharkie L. [email protected]:

How can I prevent this? This happens even if I do not set
config.time_zone

config.time_zone = ‘Bangkok’

or the appropriate value among the output of:

rake time:zones:local

HTH,
Jeffrey

I just had the same problem and solved it by either :

  • modify the datatype of my DB attribute from datetime to date (if tou
    want to store date)
    OR
  • convert your data from date to datetime, by using to_datetime (if you
    want to store datetime)

Hope it helps

Tomberry

Sharkie L. wrote:

I did properly migrate to Rails 3. I no longer have
Rails::Initializer.run do |config|

Inside class Application < Rails::Application is the only place I have
config.time_zone = ‘Bangkok’

I restarted a few times as well.

Sharkie

Jeffrey L. Taylor wrote:

Did you restart your server? Did you properly migrate
config/environment.rb
to config/application.rb. I.e., change

Rails::Initializer.run do |config|
config.time_zone = ‘Bangkok’
end

to:

config/application.rb

module YourApplicationName
class Application < Rails::Application
config.time_zone = ‘Bangkok’
end
end

It sounds you haven’t completed the migration completely.

Jeffrey

Quoting Sharkie L. [email protected]:

I did properly migrate to Rails 3. I no longer have
Rails::Initializer.run do |config|

Inside class Application < Rails::Application is the only place I have
config.time_zone = ‘Bangkok’

I restarted a few times as well.

Sharkie

Jeffrey L. Taylor wrote:

Did you restart your server? Did you properly migrate
config/environment.rb
to config/application.rb. I.e., change

Rails::Initializer.run do |config|
config.time_zone = ‘Bangkok’
end

to:

config/application.rb

module YourApplicationName
class Application < Rails::Application
config.time_zone = ‘Bangkok’
end
end

It sounds you haven’t completed the migration completely.

Jeffrey

Quoting Sharkie L. [email protected]: