This project is archived and is in readonly mode.
Glitch in DateHelper with timezones (#time_ago_in_words causing "undefined method 'abs'")
-
Geoff Buesing
- Assigned user set to Geoff Buesing
I can't replicate this.
With what arguments are you calling time_ago_in_words to get the error?
-
epitron
The problem seems to stem from creating an ActiveSupport::TimeWithZone from a DateTime object:
d = DateTime.parse("jan 7").in_time_zoneIf you go to the console, you'll notice that Time.now - d returns a Time object (no #abs), whereas Time.now - another_time_object gives you an integer.
-
Geoff Buesing
- State changed from new to open
Aha, ok, I see what the issue is -- the instance of ActiveSupport::TimeWithZone you're creating is storing the time internally as a DateTime (because you started with DateTime.parse), and Time#- with a DateTime instance as an argument returns another instance of Time, instead of a Float.
The fix should be easy enough -- we'll just need to add additional logic to Time#minus_with_coercion so that it returns a Float instead of a Time when a DateTime arg is supplied.
The current Time#- behavior with a DateTime arg seems very broken, so I see no issues with fixing it -- see example below, I have no idea how anyone could be relying on this behavior:
>> Time.local(2009,1,2) - DateTime.civil(2009,1,1) => Fri Jan 02 00:00:00 -0600 1970 -
epitron
Ruby's standard library has a lot of potholes. :)
-
Repository
(from [e98f9579c472e75f5a8c0678f2fc54b2d681e3ec]) Time#- with a DateTime argument behaves the same as with a Time argument, i.e. returns the difference between self and arg as a Float [#3476 status:resolved] http://github.com/rails/rails/commit/e98f9579c472e75f5a8c0678f2fc54...
-
Repository
(from [3f7729a66eeb362d6d4f7b0add6ca40151794b74]) Time#- with a DateTime argument behaves the same as with a Time argument, i.e. returns the difference between self and arg as a Float [#3476 status:resolved] http://github.com/rails/rails/commit/3f7729a66eeb362d6d4f7b0add6ca4...
-
Geoff Buesing
Turns out the standard library isn't to blame -- ActiveSupport defines DateTime#to_f, so Time#- coerces the DateTime arg #to_f and assumes we're subtracting seconds, instead of finding the difference between two times.
-
Geoff Buesing
- State changed from open to resolved