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.

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

#5083

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

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • 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.

    July 11th, 2010 @ 12:19 PM

  2. Jose Luis Duran
    Jose Luis Duran

    Ok. Will do.

    It's just:

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

    Thank you.

    July 11th, 2010 @ 11:03 PM

  3. Rohit Arondekar
    Rohit Arondekar
      # 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:
    

    July 12th, 2010 @ 01:48 AM

  4. Jose Luis Duran
    Jose Luis Duran

    OK got it. Sorry!

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

    July 12th, 2010 @ 02:36 AM