This project is archived and is in readonly mode.

#3476 ✓resolved
epitron

Glitch in DateHelper with timezones (#time_ago_in_words causing "undefined method 'abs'")

Reported by epitron | November 10th, 2009 @ 09:51 AM

After I enabled Rails' new TimeZone features, I got this error in an obscure set of circumstances:

ActionView::TemplateError (undefined method `abs' for Thu Feb 19 13:59:51 -0500 1970:Time)

(The exception is is thrown by DateHelper#distance_of_time_in_words, called fromDateHelper#time_ago_in_words... it's not in the backtrace, but I confirmed it in a debugger.)

It appears that Time objects and ActiveSupport::TimeWithZone objects are getting mixed together, and when you try to take their difference, the resulting object doesn't always have an #abs method.

To fix it, I just patched #time_ago_in_words to use "Time.zone.now" instead of "Time.now":

module ActionView::Helpers::DateHelper
  def time_ago_in_words(from_time, include_seconds = false)
    distance_of_time_in_words(from_time, Time.zone.now, include_seconds)
  end
end

But this is a nasty hack. :)

Perhaps the default Time objects (DateTime, Time, etc.) should be patched to always return ActiveSupport::TimeWithZone objects when timezones are enabled?

(Rails version: 2.3.4)

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages