Skip to content
Samir
Articles

How to handle time zones in Rails ?

2 min read

Time zone is a tricky subject in any application. You could rapidly find yourself in a position where you tell your users we are Saturday but really it is Sunday where they live. Fortunately, Rails has some great built-in features to help us deal with it. Still, there is some gotchas you need to be aware of.

First of all, you need to know that there is 3 different time zone configurations in any Rails web app :

  1. Your system (or server) time zone configuration
  2. ActiveRecord time zone configuration
  3. Rails time zone configuration

What is the best configuration ?

The best configuration for an app that does not have to change its time zone based on its user, it is to change Rails timezone, but continue to have Active Record save in the database in UTC.

Just put this configuration in application.rb:

Ruby
# config/application.rb
config.time_zone = 'Paris'

If you use this configuration…

  • … ActiveRecord will write UTC to the database.
  • … ActiveRecord will convert time attributes back to the configured default time zone.
  • … Time.current will be in the configured default time zone (here Paris)
  • … Time.now will be your local time (or the local time of your server)

To see the list of all available time zones, run this command:

Shell
$ rails time:zones:all

An alternative approach

This is not recommended, but if you want to change Rails timezone AND have Active Record store times in this timezone, you can do it as follow:

Ruby
# config/application.rb
config.time_zone = 'Paris'
config.active_record.default_timezone = :local

/!\ Warning: remember that saving times in the database in a non-UTC format is not recommended and you should really think this through before implementing this approach.

Also important to note that config.active_record.default_timezone can only take two values:

  • :local (converts to the timezone defined in config.time_zone)
  • :utc (converts to UTC)

What about Heroku ?

You also need to take into account your server time zone.

Heroku, for instance, uses UTC by default.

To change this, set your preferred timezone using the TZ Database Timezone format :

Shell
$ heroku config:add TZ="Europe/Paris"

Do’s

Ruby
1.hours.ago # => Fri, 12 Oct 2018 16:40:36 AFT +02:00
2.day.from_now # => Sun, 14 Oct 2018 18:40:36 AFT +02:00
Time.zone.parse("2015-08-27 14:15:00 +0300") # => Thu, 27 Aug 2015 14:15:00 AFT +03:00
Time.current # => Thu, 27 Aug 2015 16:39:36 AFT +02:00
Time.current.utc.iso8601 # When you have to supply an API ("2018-10-12T14:40:36Z")
Date.current # If you really can’t have a Time or DateTime (Thu, 27 Aug 2015)
Date.current.in_time_zone # If you have a date and want to make the best out of it (Thu, 27 Aug 2015 00:00:00 AFT +04:30)

Don’ts

Ruby
Date.today # This could be yesterday or tomorrow depending on the machine’s time zone (Sat, 13 Oct 2018)
Time.now # Returns system time and ignores your configured time zone. (2018-10-12 18:12:34 +0200)
Time.parse("2018-10-12T14:09:36Z") # Will assume time string given is in the system’s time zone. (2018-10-12 14:09:36 UTC)
Time.strptime("2018-10-12T14:09:36Z", "%Y-%m-%dT%H:%M:%S%z") # Same problem as with Time.parse. (2018-10-12 14:09:36 UTC)

Stay up to date

Get notified when I publish something new, and unsubscribe at any time.

No spam, unsubscribe any time.

Search the site

esc

Pages

Home
Articles
Contact

Writing

The O in S.O.L.I.D : improving without even touching Oct 29, 2021
The S in S.O.L.I.D : just because you can doesn't mean you should Oct 21, 2021
How to handle time zones in Rails ? Oct 12, 2018
Build your own FaceID with Amazon ReKognition in Ruby Dec 17, 2017
Code Smell Example #3: Long Class Feb 8, 2017
↑ ↓ to move ↵ to open