Contact Us

Got questions, inquiries, or opportunities for collaboration? We are just a message away!

Configure Time Zone in a Ruby on Rails Application

Finding Available Time Zones

To see all available time zones in Rails, run:

$ rake time:zones:all

Or to search for specific time zones:

# US time zones
$ rake time:zones:us

# Search for specific zones
$ rake time:zones:all | grep -i "pacific"

Common time zone examples:

config.time_zone = 'Pacific Time (US & Canada)'
config.time_zone = 'Eastern Time (US & Canada)'
config.time_zone = 'Central Time (US & Canada)'
config.time_zone = 'Mountain Time (US & Canada)'
config.time_zone = 'UTC'
config.time_zone = 'London'
config.time_zone = 'Paris'
config.time_zone = 'Tokyo'
config.time_zone = 'Sydney'

Setting the Application Time Zone

The most common way to configure a Rails application's time zone is to set it in the config/application.rb file:

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.time_zone = 'Eastern Time (US & Canada)'
  end
end

Explanation:

  • config.time_zone sets the default time zone for your Rails application
  • All time operations in Rails will use this time zone unless explicitly overridden
  • You can use any time zone name from rake time:zones:all (see below for list)

Setting Database Time Zone

You can also configure how Rails stores times in the database:

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.time_zone = 'Eastern Time (US & Canada)'
    config.active_record.default_timezone = :local # or :utc (default)
  end
end

Explanation:

  • config.active_record.default_timezone = :utc stores times in UTC in the database (recommended)
  • config.active_record.default_timezone = :local stores times in the local time zone
  • Best practice is to keep this as :utc and let Rails handle the conversion to your application time zone

Configuring Time Zone on Heroku

If deploying to Heroku, you can set the time zone using an environment variable:

$ heroku config:set TZ="America/New_York"

Or:

$ heroku config:add TZ="Europe/Athens"

To check your current Heroku time zone setting:

$ heroku config:get TZ

Explanation:

  • This sets the system time zone for your Heroku dyno
  • The time zone affects things like log timestamps and system-level time operations
  • This is separate from your Rails config.time_zone setting

Using Environment Variables for Time Zone

You can make your time zone configurable via environment variables:

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.time_zone = ENV['APP_TIMEZONE'] || 'UTC'
  end
end

Then start your Rails server with:

$ APP_TIMEZONE="Pacific Time (US & Canada)" rails s

Or set it in your .env file (if using the dotenv-rails gem or similar):

APP_TIMEZONE=Pacific Time (US & Canada)

Explanation:

  • ENV['APP_TIMEZONE'] reads the time zone from an environment variable
  • || 'UTC' falls back to UTC if the environment variable is not set
  • This allows different time zones for development, staging, and production without changing code

Setting a Custom Startup Time

You can read custom time values from environment variables and use them throughout your application. Create an initializer:

# config/initializers/set_startup_time.rb
require 'date'

$startup_time = DateTime.parse(ENV['STARTUP_TIME']) rescue DateTime.now
puts "Server started at: #{$startup_time}"

Then start your Rails server with a custom time:

$ STARTUP_TIME="2014-02-26 22:06:11 -0500" rails s

Explanation:

  • ENV['STARTUP_TIME'] reads a custom time from an environment variable
  • DateTime.parse() parses the time string into a DateTime object
  • rescue DateTime.now falls back to the current time if parsing fails or the variable is not set
  • $startup_time a global variable accessible anywhere in your Rails application
  • Placing this in an initializer ensures it runs when the Rails application starts

Understanding Time Zone vs System Time

Rails server uses the time of the hosting machine at the system level, but Rails itself can use a different time zone:

System Time (affects logs, file timestamps)

  • On your local machine - changing your operating system's time affects Rails logs and file operations
  • On a server - the server's system time is used
  • On Heroku - set with heroku config:add TZ="timezone"

Application Time (affects Time.zone.now, created_at, etc.)

  • Set in config/application.rb with config.time_zone
  • Affects all ActiveRecord time operations and Time.zone.now
  • Does not affect system-level operations

Working with Times in Rails

Once you've configured your time zone, use these methods to work with times correctly:

# CORRECT: Uses Rails' configured time zone
Time.zone.now
Time.zone.today
Time.zone.parse("2014-02-26 22:06:11")
Time.zone.local(2014, 2, 26, 22, 6, 11)

# INCORRECT: Uses system time zone
Time.now
Time.today
Time.parse("2014-02-26 22:06:11")
Time.local(2014, 2, 26, 22, 6, 11)

Explanation:

  • Always use Time.zone methods in Rails to respect your configured time zone
  • Avoid using Time.now, Time.parse, etc., as they use the system time zone
  • ActiveRecord automatically handles time zone conversion for datetime columns

Converting Between Time Zones

To convert times between different time zones:

# Get current time in application time zone
time = Time.zone.now

# Convert to a specific time zone
time_in_tokyo = time.in_time_zone('Tokyo')
time_in_london = time.in_time_zone('London')

# Get time in UTC
time_in_utc = time.utc

Explanation:

  • in_time_zone('timezone_name') converts a time to a different time zone
  • .utc converts to UTC (Coordinated Universal Time)

Per-User Time Zones

To support different time zones for different users, store the time zone preference in the user model:

# Migration
class AddTimeZoneToUsers < ActiveRecord::Migration[7.0]
  def change
    add_column :users, :time_zone, :string, default: 'UTC'
  end
end

# User model
class User < ApplicationRecord
  validates :time_zone, inclusion: { in: ActiveSupport::TimeZone.all.map(&:name) }
end

# Application controller
class ApplicationController < ActionController::Base
  around_action :set_time_zone, if: :current_user

  private

  def set_time_zone(&block)
    Time.use_zone(current_user.time_zone, &block)
  end
end

Explanation:

  • Stores each user's preferred time zone in the database
  • around_action :set_time_zone automatically sets the time zone for each request
  • Time.use_zone() temporarily sets the time zone for the duration of the block
  • All times in views and controllers will use the user's time zone

Testing Time Zone Configuration

To verify your time zone configuration is working correctly:

# In Rails console
Time.zone.name
# => "Eastern Time (US & Canada)"

Time.zone.now
# => Wed, 26 Feb 2014 22:06:11 EST -05:00

Time.now
# => 2014-02-26 22:06:11 -0500 (system time)

# Check if ActiveRecord uses UTC
ActiveRecord::Base.default_timezone
# => :utc

Environment-Specific Configuration

You can set different time zones for different environments:

# config/environments/development.rb
Rails.application.configure do
  config.time_zone = 'Pacific Time (US & Canada)'
end

# config/environments/production.rb
Rails.application.configure do
  config.time_zone = 'UTC'
end

Explanation:

  • This allows you to use your local time zone in development while keeping production in UTC
  • Useful when your team is distributed across different time zones

Summary

ConfigurationLocationPurpose
config.time_zoneconfig/application.rbSets Rails application time zone
config.active_record.default_timezoneconfig/application.rbSets database storage time zone (:utc or :local)
heroku config:add TZHeroku CLISets system time zone on Heroku
ENV['APP_TIMEZONE']Environment variableMakes time zone configurable
Time.zone.nowThroughout appAlways use this instead of Time.now
Per-user time zonesUser model + around_actionSupport multiple time zones for different users

Best Practices

  • Store times in UTC in the database (config.active_record.default_timezone = :utc)
  • Set your application time zone in config/application.rb
  • Always use Time.zone.now instead of Time.now
  • Consider supporting per-user time zones for user-facing applications