skip to content

In a Rails app, how do config.time_zone and config.active_record.default_timezone split the work of storing and showing timestamps?

level: middleimportance: must knowfreq 58%

answer

  1. one zone stores, one zone shows
  2. default_timezone defaults to :utc
  3. config.time_zone defaults to "UTC"
  4. time_zone_aware_attributes converts on read
  5. assigned strings parsed in Time.zone

basics

~10 s

config.active_record.default_timezone (:utc by default) sets the zone Active Record writes to and reads from datetime columns; config.time_zone sets Time.zone, into which datetime attributes are converted on read and in which assigned strings are parsed.

solid answer

~40 s

Two settings, two jobs. `config.active_record.default_timezone` is the **storage** zone: with the default `:utc`, Active Record converts every time to UTC before writing and reads stored values back as UTC; `:local` uses the server process's local time instead, which ties the data to the machine. `config.time_zone` ("UTC" unless set; a Rails name like "Central Time (US & Canada)" or an IANA id) becomes `Time.zone_default`, the **presentation** zone. Because the Active Record railtie turns on `time_zone_aware_attributes`, `datetime` and `time` attributes are converted into `Time.zone` when read, returning `ActiveSupport::TimeWithZone`, and a string assigned to one is parsed as wall-clock time in `Time.zone`. So you can change the display zone without touching data, while flipping `default_timezone` on a live database makes every existing row read wrong.

code

ruby · 8 lines
ruby
# config/application.rb
module Freight
  class Application < Rails::Application
    config.load_defaults 8.1
    config.time_zone = "Central Time (US & Canada)"   # presentation and parsing zone
    # config.active_record.default_timezone = :utc   # storage zone (the default)
  end
end

go deeper

for a junior

Remember that Rails stores times in UTC by default and shows them in config.time_zone, which defaults to "UTC" until you set it.

for a middle

Walk through a write and a read: string parsed in Time.zone, value quoted as UTC, column read as UTC and converted back to a TimeWithZone. Name time_zone_aware_types and the date-column exception.

for a senior

Explain why default_timezone :local is a trap, what a change to either setting costs on a live database, and when to opt an attribute out of conversion.

for a principal

Treat UTC storage as a platform invariant across services and exports, and decide where presentation zones are chosen: per user, per tenant or per site.

## Two settings, two jobs A Rails app separates **where a time is stored** from **how it is shown**: | Setting | Default | Decides | |---|---|---| | `config.active_record.default_timezone` | `:utc` | The wall clock Active Record uses when it writes a time into a database column and when it interprets the value it reads back. The only other legal value is `:local` (the server process's zone); anything else raises `ArgumentError`. | | `config.time_zone` | `"UTC"` | `Time.zone_default`, the application zone that `Time.zone`, `Time.current` and time-zone-aware attributes use. Accepts a Rails zone name (`"Central Time (US & Canada)"`) or an IANA identifier (`"America/Chicago"`). | The generated `config/application.rb` carries `config.time_zone` only as a commented-out example, so a new app runs entirely in UTC until you set it. ## What happens on write When you assign a value to a `datetime` attribute and save the record: 1. **Casting.** A `Time`, `DateTime` or `TimeWithZone` keeps its instant. A **string** such as `"2026-07-14 09:00"` is read with `in_time_zone`, which for a string is `Time.zone.parse` — so it means 09:00 in `Time.zone`, not 09:00 UTC and not server time. 2. **Quoting.** When the value is sent to the database, Active Record converts it to UTC (with `default_timezone = :utc`) or to process-local time (with `:local`). 3. **Timestamps.** `created_at` and `updated_at` are taken from `Time.now.utc` under `:utc`, so they follow the same rule. ## What happens on read The column comes back as a plain value with no zone. The type interprets it in the storage zone (UTC by default), and the **time-zone conversion** layer then calls `in_time_zone` on it, so `record.pickup_at` is an `ActiveSupport::TimeWithZone` in whatever `Time.zone` is at that moment — the default zone, or a per-request zone set with `Time.use_zone`. ## Which attributes are converted - The Active Record railtie sets **`ActiveRecord::Base.time_zone_aware_attributes = true`**; the class default is `false`, so this is a Rails-app behaviour, not a library one. - **`time_zone_aware_types`** defaults to `[:datetime, :time]`; with the PostgreSQL adapter, `:timestamptz` is added. - **`date` columns are not converted** — a date has no time of day, so `Time.zone` never touches it. - **`time` columns** are converted too and come back pinned to 1 January 2000, which surprises people storing a "daily opening hour". - A model can opt specific attributes out with **`skip_time_zone_conversion_for_attributes = [:opens_at]`**, keeping a wall-clock value unconverted. ## Choosing a value for config.time_zone - **API-only service:** leave it `"UTC"` and let clients present times; responses then carry UTC offsets that need no explanation. - **Single-region business app:** set the home zone, so admin screens, mailers and reports read naturally without per-request work. - **Multi-user app across zones:** keep a sensible default and switch per request with `Time.use_zone` from the user's stored zone. - **Rails name or IANA id:** both work; `"Central Time (US & Canada)"` maps to `"America/Chicago"`. `bin/rails time:zones` lists Rails' names, and `bin/rails time:zones[US]` filters them by country. ## Why the defaults are the right ones - **UTC storage is unambiguous.** UTC has no daylight-saving jumps, so every stored value names exactly one instant and sorts correctly. - **Changing `config.time_zone` is cheap.** Stored rows are untouched; only presentation and string parsing move. - **Changing `default_timezone` is expensive.** Existing rows were written in the old storage zone; after the switch, Active Record reads them in the new one and every value shifts by the offset, so it needs a data migration. - **`:local` couples data to infrastructure.** Moving servers, or containers that default to UTC, silently changes what new rows mean. The usual production setup is therefore: leave `default_timezone` at `:utc`, set `config.time_zone` to the business's home zone (or leave it UTC for an API), and switch to each user's zone per request with `Time.use_zone`.

  • What happens to existing rows when you change config.time_zone from UTC to Central Time?
    Nothing in the database. Rows keep their UTC values; reads now present them in Chicago time, and new string input is parsed as Chicago wall-clock time. Anything that compared stored times with hand-built UTC strings or relied on UTC-formatted output in views, mailers or exports will show the new zone.
  • When would you use skip_time_zone_conversion_for_attributes on an Active Record model?
    For a value that is a wall-clock time rather than an instant — say a depot's daily `opens_at` stored in a `time` column. Converting it into `Time.zone` would shift 08:00 by the zone offset for viewers elsewhere; listing it in `skip_time_zone_conversion_for_attributes` keeps it exactly as stored.
  • Why does flipping config.active_record.default_timezone to :local on a live app corrupt data reads?
    Existing rows were written as UTC. After the switch, Active Record interprets stored values as process-local time, so every old timestamp shifts by the server's offset, while new rows are written in local time. Mixed rows then cannot be told apart without a data migration.

An airline's booking system records every departure in UTC, and each boarding pass prints that same departure in the local time of the airport; changing the printer's zone never moves the flight, but re-labelling the stored times would.

  • config.time_zone decides which zone Active Record writes into the columns
  • Changing config.time_zone needs a data migration to shift existing rows
  • Reading a datetime attribute returns a plain Ruby Time in UTC
  • date columns are converted into Time.zone just like datetime columns
  • A string assigned to a datetime attribute is always read as UTC