Skip to content

Avoid Time/Date::DATE_FORMATS deprecation on Rails 8.2#180

Open
kylekeesling wants to merge 1 commit into
basecamp:mainfrom
kylekeesling:fix-date-formats-deprecation
Open

Avoid Time/Date::DATE_FORMATS deprecation on Rails 8.2#180
kylekeesling wants to merge 1 commit into
basecamp:mainfrom
kylekeesling:fix-date-formats-deprecation

Conversation

@kylekeesling
Copy link
Copy Markdown

Problem

Rails 8.2 (currently main) deprecates the Time::DATE_FORMATS and Date::DATE_FORMATS constants in favor of the new ActiveSupport::TimeFormats / ActiveSupport::DateFormats modules. See rails/rails@b527067 for the deprecation.

LocalTimeHelper#ruby_time_or_date_format reads those constants on every render that uses a symbol format, which produces a noisy warning on apps tracking Rails edge:

DEPRECATION WARNING: Time::DATE_FORMATS is deprecated, to register custom time
formats use ActiveSupport::TimeFormats.register (called from
LocalTimeHelper#ruby_time_or_date_format at
.../local_time-3.0.3/app/helpers/local_time_helper.rb:106)

Fix

Prefer ActiveSupport::TimeFormats.lookup / ActiveSupport::DateFormats.lookup when the modules are defined, falling back to the existing DATE_FORMATS read on older Rails so this stays compatible with the gem's current ~> 7.0 dev dependency.

ActiveSupport::TimeFormats.lookup internally checks both the new frozen @list and the legacy DEPRECATED_LIST (where writes via Time::DATE_FORMATS[:foo] = ... still land), so existing call sites that register formats the old way continue to work transparently.

Verification

  • bundle exec rake test:helpers → 27 runs, 28 assertions, 0 failures (Rails 7.1.5.1, backward-compat path).
  • Manually verified the new path against a Rails 8.2-alpha app: the deprecation no longer fires, and :stamp-style formats resolve correctly.

Rails 8.2 deprecates the Time::DATE_FORMATS and Date::DATE_FORMATS
constants in favor of ActiveSupport::TimeFormats / DateFormats.
Reading those constants from LocalTimeHelper#ruby_time_or_date_format
now emits a DEPRECATION WARNING on every render that uses a symbol
format.

Prefer the new lookup API when available, fall back to the existing
DATE_FORMATS read on older Rails. Tests pass on Rails 7.1.
Copilot AI review requested due to automatic review settings May 27, 2026 17:50
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Refactors time/date format lookup to prefer new ActiveSupport lookup APIs when available, with fallbacks to Time::DATE_FORMATS / Date::DATE_FORMATS.

Changes:

  • Replaces direct Time::DATE_FORMATS/Date::DATE_FORMATS access with helper methods.
  • Adds lookup_time_format/lookup_date_format to support ActiveSupport::*Formats.lookup.
  • Keeps fallback behavior for older Rails versions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +113 to 125
if defined?(ActiveSupport::TimeFormats)
ActiveSupport::TimeFormats.lookup(name.to_sym)
else
Time::DATE_FORMATS.with_indifferent_access[name]
end
end

def lookup_date_format(name)
if defined?(ActiveSupport::DateFormats)
ActiveSupport::DateFormats.lookup(name.to_sym)
else
Date::DATE_FORMATS.with_indifferent_access[name]
end
if defined?(ActiveSupport::TimeFormats)
ActiveSupport::TimeFormats.lookup(name.to_sym)
else
Time::DATE_FORMATS.with_indifferent_access[name]

def lookup_time_format(name)
if defined?(ActiveSupport::TimeFormats)
ActiveSupport::TimeFormats.lookup(name.to_sym)

def lookup_date_format(name)
if defined?(ActiveSupport::DateFormats)
ActiveSupport::DateFormats.lookup(name.to_sym)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants