On Tue, Sep 11, 2007 at 01:35:08AM +0900, Phlip wrote:
> joep wrote:
> 
> > I am writing a small piece that will include a date that needs to be
> > compared to the current time.
> > 
> > EX. a = Time.now
> >       b = db_cert_date + (72 months)
> 
> Rails's ActiveSupport package lets you say some_date + 72.months. It's that
> simple; the secret unit of exchange is probably seconds.
> 
> If this were Brand X, using another platform's low-level library would be
> eternal torment, but this is Ruby, so just try require 'active_support'!

Depending on how accurate you actually want, active_support's 72.months
assumes 30 days in a month.  Here's a quick comparison of adding 72
months in different libraries:

    % cat time-test.rb 
    #!/usr/bin/env ruby
    require 'rubygems'
    require 'active_support'
    require 'chronic'

    today_time = Time.now
    today_dt   = DateTime.now
    format     = "%Y-%m-%d %H:%M:%S"
    puts "Calculate the date of 72 months from today  : #{today_time.strftime(format)}"
    puts "ActiveSupport (today (Time) + 72.months)    : #{(today_time + 72.months).strftime(format)}"
    puts "ActiveSupport (today (Time) + 6.years)      : #{(today_time + 6.years).strftime(format)}"
    puts "Chronic.parse '72 months from now'          : #{Chronic.parse('72 months from now').strftime(format)}"
    puts "DateTime.now >> 72                          : #{(today_dt  >> 72).strftime(format)}"

    % ruby time-test.rb
    Calculate the date of 72 months from today  : 2007-09-10 11:32:14
    ActiveSupport (today (Time) + 72.months)    : 2013-08-09 11:32:14
    ActiveSupport (today (Time) + 6.years)      : 2013-09-09 23:32:14
    Chronic.parse '72 months from now'          : 2013-09-10 11:32:14
    DateTime.now >> 72                          : 2013-09-10 11:32:14

As you can see, there is some disparity.  From my perspective, the only ones of
these that are actually correct are the Chronic and the DateTime ones.

Is there a really good date calculation library in ruby that I'm missing?

enjoy,

-jeremy

-- 
========================================================================
 Jeremy Hinegardner                              jeremy / hinegardner.org