Search code examples
ruby-on-rails-3timezonelegacy

Saving Rails 3 time in server local time to support non-timezone aware systems


Some of us are unable to store and work with time in UTC because of compatibility issues with existing data or legacy/connected systems that are purely accessed locally and non-timezone aware.

1 way is to disable saving time in UTC in rails which many have tried but not found the way to do it, I have not seen any before.

How can we override the UTC time saving in rails 3?

How do you manage such an issue in your system?


Solution

  • What I found to be working on my app with MSSql backend is the following combination of configs

    I have in application.rb

    config.time_zone = '[the server's timezone]'
    config.active_record.default_timezone = :local
    ActiveRecord::Base.time_zone_aware_attributes = false
    

    time was saved as per the server time, without conversion to UTC and displayed accordingly.

    I have not tried this with any other database backends.

    Perhaps an alternative solution is to create dedicated time columns for use by rails and use triggers to sync between these UTC time data with another set of time columns(in local time) in order to support legacy and non-timezone aware systems.