skip to content

In Rails 8.1, what does to_time return for a TimeWithZone, a DateTime and a Date, and what became of to_time_preserves_timezone?

level: seniorimportance: nice to knowfreq 14%

answer

  1. always the receiver's zone now
  2. TimeWithZone#to_time keeps its zone
  3. Date#to_time is process-local midnight
  4. the config key only warns
  5. in_time_zone for the app zone

basics

~20 s

In Rails 8.1, TimeWithZone#to_time returns a Ruby Time that keeps the receiver's zone and DateTime#to_time keeps its offset; the system-local option is gone and to_time_preserves_timezone is deprecated. Date#to_time and String#to_time still use the process zone.

solid answer

~40 s

Rails 8.1 removed the deprecated mode in which `to_time` converted a time to the system's local zone: it now always preserves the receiver's zone. `TimeWithZone#to_time` returns a Ruby `Time` carrying the same zone and offset, `DateTime#to_time` returns a `Time` with the same UTC offset, and `Time#to_time` returns `self`. `config.active_support.to_time_preserves_timezone`, which used to choose between those behaviours, is deprecated: setting or reading it only emits a warning that it will be removed in Rails 8.2, so upgraded apps should delete the line from their config. The trap that remains is `Date#to_time` and `String#to_time`: both default to the `:local` form, the process zone (`ENV['TZ']`), not `Time.zone`. Use `in_time_zone` when you want the application's zone.

code

ruby · 10 lines
ruby
# Rails 8.1, config.time_zone = "Tokyo", server process TZ=UTC
t = Time.zone.local(2026, 3, 8, 9)   # => 2026-03-08 09:00:00.000000000 JST +09:00
t.to_time            # => a Ruby Time, 09:00 with the Tokyo zone (+09:00)
t.to_time.class      # => Time

Date.new(2026, 3, 8).to_time        # => 2026-03-08 00:00:00 +0000  (process zone)
Date.new(2026, 3, 8).in_time_zone   # => 2026-03-08 00:00:00.000000000 JST +09:00

# config/application.rb -- remove on upgrade; in 8.1 it only warns
# config.active_support.to_time_preserves_timezone = :zone

go deeper

for a junior

Recall that to_time and in_time_zone are different: in_time_zone gives a TimeWithZone in the app's zone, while Date#to_time uses the server's zone.

for a middle

Explain what to_time returns for Time, TimeWithZone, DateTime and Date in 8.1, and why Date and String fall back to the process zone.

for a senior

Run the 8.1 upgrade: remove the deprecated config key, find code that expected system-local conversion or added a TimeWithZone to a Time, and replace Date#to_time where zones matter.

for a principal

Use changes like this to justify a regular upgrade cadence that reads release notes and retires framework-default overrides instead of carrying them forward.

## What changed in Rails 8.1 For years Active Support's `to_time` had two possible behaviours for zoned values, chosen by **`ActiveSupport.to_time_preserves_timezone`** (set through `config.active_support.to_time_preserves_timezone`): - **convert to system local time** — return a `Time` in the server process's zone; - **preserve the receiver** — return a `Time` that keeps the receiver's offset or zone. The 8.1 release notes list two Active Support removals and one deprecation here: 1. **Removed:** support for `to_time` preserving the system local time. `to_time` now **always preserves the receiver's zone**. 2. **Removed:** the deprecated addition of an `ActiveSupport::TimeWithZone` to a Ruby `Time` instance. 3. **Deprecated:** `config.active_support.to_time_preserves_timezone`. Both its setter and getter emit a deprecation warning saying it will be removed in Rails 8.2; the value no longer changes anything. ## What each to_time returns in 8.1 | Receiver | `to_time` returns | |---|---| | `Time` | `self` | | `ActiveSupport::TimeWithZone` | a Ruby `Time` for the same instant, carrying the receiver's zone (and so its offset) | | `DateTime` | a Ruby `Time` with the same UTC offset | | `Date` | midnight of that date in the **process** zone (`:local`, the default) or UTC (`to_time(:utc)`); any other argument raises `ArgumentError` | | `String` | parsed in the **process** zone by default, or UTC with `to_time(:utc)` | The first three are the "receiver" cases the 8.1 change is about. The last two never had a receiver zone to preserve, so they still fall back to Ruby's process zone — which is **not** `Time.zone`. The source comment on `Date#to_time` says so directly: use `in_time_zone` if the application's zone is needed. ## Upgrading an app to 8.1 - **Delete the config line.** Search `config/application.rb`, `config/initializers/` and any `new_framework_defaults_*.rb` for `to_time_preserves_timezone` and remove it; leaving it only produces deprecation warnings at boot. - **Find code that relied on system-local conversion.** If something called `to_time` on a `TimeWithZone` and expected server-local wall-clock digits (for a log line, a file name, a legacy API), it now gets the receiver's zone. Ask for the zone you mean explicitly: `.utc`, `.getlocal`, or `in_time_zone(zone)`. - **Look for `Time + TimeWithZone`.** Adding the two was deprecated and is gone in 8.1; such code was almost always meant to add a `Duration` or compute a difference. - **Re-check `Date#to_time` calls.** They were never zone-aware; replace them with `date.in_time_zone` (the app zone) or `date.in_time_zone(zone)` where the result feeds a query or a comparison. ## Checking an app before the upgrade 1. Search the config tree for the deprecated key and delete it. 2. Search application code for `.to_time` and decide, call by call, whether the receiver is zoned (now preserved) or a `Date`/`String` (process zone). 3. Run the test suite with deprecation warnings raised as errors (`config.active_support.deprecation = :raise` in the test environment) so any remaining use of the key fails loudly. 4. Watch boot logs on the first deploy for the deprecation message. ## Why it matters in an interview - It tests whether a candidate knows that **Ruby's process zone and `Time.zone` are different things** and which methods use which. - It tests whether they read **release notes during an upgrade** instead of copying old configuration forward. - It separates "conversion" (`to_time`, which turns a value into a Ruby `Time`) from "presentation in a zone" (`in_time_zone`, which returns a `TimeWithZone`). A good short answer: "In 8.1 `to_time` always keeps the receiver's zone, the old config key is deprecated and does nothing, and `Date#to_time` is still process-local, so I use `in_time_zone` for anything that should follow `Time.zone`."

  • How do you get midnight of a Date in the application's zone in Rails 8.1?
    Call `date.in_time_zone` (or `date.beginning_of_day`, which is the same), which returns a `TimeWithZone` at midnight in `Time.zone`. Pass a zone to target another one: `date.in_time_zone("Tokyo")`. `Date#to_time` gives midnight in the process zone instead.
  • What does an 8.1 app see at boot if config still sets config.active_support.to_time_preserves_timezone?
    The Active Support railtie forwards `config.active_support` keys to `ActiveSupport`, so the deprecated setter runs and emits a deprecation warning that the setting will be removed in Rails 8.2. Behaviour is unchanged: `to_time` preserves the receiver's zone either way.

  • In Rails 8.1 to_time on a TimeWithZone returns the server's local time
  • Date#to_time returns midnight in config.time_zone
  • Setting to_time_preserves_timezone is still needed to get zone-preserving to_time
  • TimeWithZone#to_time returns the same TimeWithZone object
  • Removing the config line changes how to_time behaves in 8.1

Assumes Rails 8.1, which removed to_time's system-local mode and deprecated config.active_support.to_time_preserves_timezone.