Jay
Jay

Reputation: 87

Timezone setting in Rails

I am trying to set the ActiveRecord's time zone from UTC to current time zone. This is what I ended up

Application.rb

module application
  class Application < Rails::Application
    config.load_defaults 5.2
    config.time_zone = 'Tokyo'.freeze
    config.active_record.default_timezone = :local
  end
end

I have turned off the server and restarted and made an object then when I checked the object's created_at it seems the ActiveRecords still records the data based on UTC time not Tokyo time.

Any solutions guys?

Upvotes: 0

Views: 126

Answers (1)

Nuttapon
Nuttapon

Reputation: 101

It always records the data of DateTime as UTC in Database and it will be converted according to config.time_zone if you call object.created_at

Let's say, It stored 2018-09-17 04:41:00 or Fri, 17 Sep 2018 04:41:00 UTC +00:00 in the database.

If your config the timezone as Tokyo then the result is Fri, 17 Sep 2018 13:41:00 JST +09:00

Additionally, you can use .in_time_zone method to convert the DateTime based on that zone. Here's .in_time_zone

Upvotes: 1

Related Questions