This project is archived and is in readonly mode.

#2067 new
Amiel Martin

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

Reported by Amiel Martin | February 25th, 2009 @ 05:34 AM | in 3.x

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 * 3600, [[:seconds, self * 3600]])
# could be
def hours * 1.hour, [[:hours, self]])

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

# for example
(1.month + + 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: 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")?

Comments and changes to this ticket

  • Amiel Martin

    Amiel Martin February 25th, 2009 @ 06:04 AM

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

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

    any thoughts?

  • Amiel Martin

    Amiel Martin February 25th, 2009 @ 06:05 AM

    • 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”
  • Amiel Martin

    Amiel Martin February 25th, 2009 @ 06:09 AM

    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.

  • Levin Alexander

    Levin Alexander February 25th, 2009 @ 06:46 AM

    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.

  • Amiel Martin

    Amiel Martin February 26th, 2009 @ 06:52 AM

    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.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • bingbing

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=""></a>

People watching this ticket