0

I need to turn off the time conversions between Rails application and Mysql.

This comment at ActiveRecord source say that:

  # Timestamps are in UTC by default but you can use the local timezone by setting:
  #
  #   config.active_record.default_timezone = :local

But, my server uses the UTC timezone and it'll no make difference if use :local or :utc option. The needed timezone is 'Brasilia'.

The Rails console on production server show the conversions:

$ RAILS_ENV=production bundle exec rails c 
Loading production environment (Rails 4.1.4)
2.0.0 :001 > Time.current
 => Wed, 13 Aug 2014 17:34:15 BRT -03:00 
2.0.0 :002 > Schedule.where(startDateTimeSchedule: Time.current)
  Schedule Load (1.0ms)  SELECT `Schedule`.* FROM `Schedule`  WHERE `Schedule`.`startDateTimeSchedule` = '2014-08-13 20:34:25'

P.S.: The accepted answer of the highly voted question about this subject is wrong.

Community
  • 1
  • 1
Rodrigo
  • 5,435
  • 5
  • 42
  • 78

1 Answers1

0

You can set a new default timezone in your config/application.rb

config.time_zone = 'Brasilia'

If you have users of whom you know the timezone, you can also convert them correctly by adding this code to your ApplicationController

around_filter :user_time_zone, :if => :current_user

def user_time_zone(&block)
  Time.use_zone(current_user.timezone, &block)
end
Alain M. Lafon
  • 1,680
  • 1
  • 13
  • 13