This project is archived and is in readonly mode.

#5083 ✓invalid
Jose Luis Duran

If 1.year = 365.25.days, then 1.month should be equal to 30.4375.days

Reported by Jose Luis Duran | July 11th, 2010 @ 11:34 AM

in /activesupport/lib/active_support/core_ext/integer/time.rb:

#   # equivalent to 30.4375.days.to_i.from_now
#   1.month.to_i.from_now

def months
  ActiveSupport::Duration.new(self * 30.4375.days, [[:months, self]])
end

Comments and changes to this ticket

  • Rohit Arondekar

    Rohit Arondekar July 11th, 2010 @ 12:19 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

    These are convenience methods. The API docs explain how the should be used:

      # These methods use Time#advance for precise date calculations when using from_now, ago, etc. 
      # as well as adding or subtracting their results from a Time object. For example:
      #
      #   # equivalent to Time.now.advance(:months => 1)
      #   1.month.from_now
    

    And also how not to use them:

      # While these methods provide precise calculation when used as in the examples above, care
      # should be taken to note that this is not true if the result of `months', `years', etc is
      # converted before use:
      #
      #   # equivalent to 30.days.to_i.from_now
      #   1.month.to_i.from_now
    

    And also suggest what to use for precision arithmetic:

      # In such cases, Ruby's core
      # Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
      # Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
      # date and time arithmetic
    

    Most importantly they behave as expected. :)

    If you can provide a good reason as to why this change needs to be done I'll reopen the ticket.

  • Jose Luis Duran

    Jose Luis Duran July 11th, 2010 @ 11:03 PM

    Ok. Will do.

    It's just:

    if 1.year.to_i != 12.months.to_i
      puts "something must be wrong!"
    end
    

    Thank you.

  • Rohit Arondekar

    Rohit Arondekar July 12th, 2010 @ 01:48 AM

      # While these methods provide precise calculation when used as in the examples above, care
      # should be taken to note that this is not true if the result of `months', `years', etc is
      # converted before use:
    
  • Jose Luis Duran

    Jose Luis Duran July 12th, 2010 @ 02:36 AM

    OK got it. Sorry!

    require 'timecop'
    
    Timecop.freeze(Time.now)
    if 1.year.ago == 12.months.ago
      puts "Everything is OK!"
    end
    

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>

Pages