Skip to content

Instantly share code, notes, and snippets.

@kevcha
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevcha/1f34cf6923495f5ab6fe to your computer and use it in GitHub Desktop.
Save kevcha/1f34cf6923495f5ab6fe to your computer and use it in GitHub Desktop.
Timezones

Initial configuration

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')

Step 1

Initial configuration

rake db:reset
Default values in application.rb

In Database

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

In views

<%= user.created_at.to_s %> returns 2014-06-25 16:23:03 UTC
<%= user.created_at.class %> returns ActiveSupport::TimeWithZone

Step 2

Initial configuration

In application.rb :

config.time_zone = 'Europe/Paris'
config.active_record.default_timezone = :utc

rake db:reset

In Database

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

In views

<%= user.created_at.to_s %> returns 2014-06-25 18:28:03 +0200
<%= user.created_at.class %> returns ActiveSupport::TimeWithZone

Step 3

Initial configuration

In application.rb:

config.time_zone = 'Europe/Paris'
config.active_record.default_timezone = :local

rake db:reset

In Database

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

In views

<%= user.created_at.to_s %> returns 2014-06-25 18:29:50 +0200
<%= user.created_at.class %> returns ActiveSupport::TimeWithZone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment