My current timezone : GMT+2 ('Europe/Paris')
rails new sandbox
(rails 4.1.1)
rails g model User name:string
Database : Postgresql 9.3.4
In seed.rb
:
User.create(name: 'Kévin')
rake db:reset
Default values in application.rb
sandbox_development=# SELECT * FROM USERS;
id | name | created_at | updated_at
----+--------+----------------------------+----------------------------
1 | Kévin | 2014-06-25 16:23:03.964801 | 2014-06-25 16:23:03.964801
Current time : 18:23
Conclusion : UTC in db
<%= user.created_at.to_s %>
returns 2014-06-25 16:23:03 UTC
<%= user.created_at.class %>
returns ActiveSupport::TimeWithZone
In application.rb
:
config.time_zone = 'Europe/Paris'
config.active_record.default_timezone = :utc
rake db:reset
sandbox_development=# SELECT * FROM USERS;
id | name | created_at | updated_at
----+--------+----------------------------+----------------------------
1 | Kévin | 2014-06-25 16:28:03.481602 | 2014-06-25 16:28:03.481602
Current time : 18:28
Conclusion : UTC in db
<%= user.created_at.to_s %>
returns 2014-06-25 18:28:03 +0200
<%= user.created_at.class %>
returns ActiveSupport::TimeWithZone
In application.rb
:
config.time_zone = 'Europe/Paris'
config.active_record.default_timezone = :local
rake db:reset
sandbox_development=# SELECT * FROM USERS;
id | name | created_at | updated_at
----+--------+----------------------------+----------------------------
1 | Kévin | 2014-06-25 18:29:50.749949 | 2014-06-25 18:29:50.749949
Current time : 18:29
Conclusion : 'Europe/Paris' in db => Be aware of that => Better to set
config.active_record.default_timezone = :utc
<%= user.created_at.to_s %>
returns 2014-06-25 18:29:50 +0200
<%= user.created_at.class %>
returns ActiveSupport::TimeWithZone