Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Numeric::Time is not consistent, giving confusing results to Duration#inspect

#2067

Is there a reason that ActiveSupport::CoreExtensions::Numeric::Time#minutes (and hours) don't their own unit as a type when creating an ActiveSupport::Duration ?


# for example
def hours
  ActiveSupport::Duration.new(self * 3600, [[:seconds, self * 3600]])
end
# could be
def hours
  ActiveSupport::Duration.new(self * 1.hour, [[:hours, self]])
end

The problem is that this creates confusing output for ActiveSupport::Duration#inspect


# for example
(1.month + 1.day + 5.minutes).inspect
=> "1 month, 1 day, and 300 seconds"

I have included a patch with tests to show this example

I can write the fixes, I just want to know if there is a reason for what is there.

as a side note, I found this bug because I wanted to add functionality for outputting a number in seconds to a sentence.

I wrote this: http://gist.github.com/69934 which I was planning to add to ActiveSupport::Duration until I noticed #inspect. Another question is? does it make sense to use ActiveSupport::Duration#inspect for user output? should I add something like the method I wrote that can pull apart a number in seconds and still output a nice sentence (like: 65.seconds.inspect # => "1 minute and 5 seconds")?

Reported by Amiel Martin · February 25th, 2009 @ 05:34 AM

State: new
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Amiel Martin
    Amiel Martin

    another idea is to write a method in ActiveSupport::Duration that "normalizes" internal data.

    
    # example
    >> 65.seconds
    => 65 seconds
    >> 65.seconds.parts
    => [[:seconds, 65]]
    >> 65.seconds.normalize
    => 1 minute and 5 seconds
    >> 65.seconds.normalize.parts
    => [[:minutes, 1], [:seconds, 5]]
    

    any thoughts?

    February 25th, 2009 @ 06:04 AM

  2. Amiel Martin
    Amiel Martin
    • Title changed from Numeric::Time is not consistent giving confusing results to Duration#inspect to Numeric::Time is not consistent, giving confusing results to Duration#inspect

    February 25th, 2009 @ 06:05 AM

  3. Amiel Martin
    Amiel Martin

    or, maybe this would be better

    
    >> 5.seconds + 1.minute
    => 65 seconds
    >> _.humanize
    => "1 minute and 5 seconds"
    

    I'm happy to implement any of the above, the only question is which.

    February 25th, 2009 @ 06:09 AM

  4. Levin Alexander
    Levin Alexander

    I think the rationale is that days and months are not constant, their length depends on the context (1.month could stand for 30 or 31 days, a day might get an extra hour on DST)

    Hours and minutes on the other hand have a constant length (1.hour is always exactly equal to 3600.seconds) so it makes sense to lump them together to make calculations faster and simpler.

    February 25th, 2009 @ 06:46 AM

  5. Amiel Martin
    Amiel Martin

    So, what would be the best action here? I'd love to contribute, and I think having a way to output a duration in human readable form would be a good feature.

    February 26th, 2009 @ 06:52 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  7. bingbing