DEV Community

Cover image for Rails 8 – New ActiveSupport Timezone Defaults
Georgy Yuriev
Georgy Yuriev

Posted on

Rails 8 – New ActiveSupport Timezone Defaults

Rails 8 introduces a new default value for

ActiveSupport.to_time_preserves_timezone
Enter fullscreen mode Exit fullscreen mode
# config/initializers/new_framework_defaults_8_0.rb
#
# Specifies whether `to_time` methods preserve the UTC offset of their receivers or preserve the timezone.
# If set to `:zone`, `to_time` methods will use the timezone of their receivers.
# If set to `:offset`, `to_time` methods will use the UTC offset.
# If `false`, `to_time` methods will convert to the local system UTC offset instead.
#
# Rails.application.config.active_support.to_time_preserves_timezone = :zone
Enter fullscreen mode Exit fullscreen mode

It was not clear to me what this changes for my application or where problems might hide. So I figured it out.

require 'active_support/all'
Time.zone = 'America/New_York'
Enter fullscreen mode Exit fullscreen mode

In the United States, daylight saving time begins on the second Sunday in March and ends on the first Sunday in November, with the time change occurring at 2:00 a.m. local time. I chose the borderline case - November 1st.

ActiveSupport.to_time_preserves_timezone = :offset

t = Time.zone.parse('1/Nov/2025 12:30:00').to_time
=> 2025-11-01 12:30:00 -0400

t.zone
=> nil

t + 1.day
=> 2025-11-02 12:30:00 -0400
Enter fullscreen mode Exit fullscreen mode

Notice the lack of time zone information in the result object. The transition from daylight saving time was not completed.

ActiveSupport.to_time_preserves_timezone = :zone

t = Time.zone.parse('1/Nov/2025 12:30:00').to_time
=> 2025-11-01 12:30:00 -0400

t.zone
=> #<ActiveSupport::TimeZone:0x000000011c9aff20 @name="America/New_York", @tzinfo=#<TZInfo::DataTimezone: America/New_York>, @utc_offset=nil>

t + 1.day
=> 2025-11-02 12:30:00 -0500
Enter fullscreen mode Exit fullscreen mode

But with the new Rails default (:zone), the to_time method returns an object with timezone information, which helps to correctly make calculations taking the daylight saving time transition into account.

Top comments (0)