Don't use from_now, for now

Boop Boop wrote:

A little app I wrote has a calendar view for three months. Everything went fine until today, when it didn't display August - September - October, but August - August - September. Next month had become this month.

Watch this:

Time.now

=> Wed Aug 01 21:13:55 +0200 2007

1.month.from_now

=> Fri Aug 31 21:14:01 +0200 2007

Time.now.advance :months=>1

=> Sat Sep 01 21:14:13 +0200 2007

Especially annoying since the rdoc for ActiveSupport::CoreExtensions::Numeric::Time pretends that:

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

It's NOT equivalent. It's broken.

Where did you get this?

I just checked the API docs at:

http://api.rubyonrails.com/classes/ActiveSupport/CoreExtensions/Numeric/Time.html

And they explicitly state that:

  If you need precise date calculations that doesn‘t just treat   months as 30 days, then have a look at Time#advance.

  Some of these methods are approximations, Ruby‘s core Date and   Time should be used for precision date and time arithmetic

Boop Boop wrote:

A little app I wrote has a calendar view for three months. Everything went fine until today, when it didn't display August - September - October, but August - August - September. Next month had become this month.

Watch this:

Time.now

=> Wed Aug 01 21:13:55 +0200 2007

1.month.from_now

=> Fri Aug 31 21:14:01 +0200 2007

Time.now.advance :months=>1

=> Sat Sep 01 21:14:13 +0200 2007

Especially annoying since the rdoc for ActiveSupport::CoreExtensions::Numeric::Time pretends that:

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

It's NOT equivalent. It's broken.

Where did you get this?

I just checked the API docs at:

Peak Obsession

And they explicitly state that:

  If you need precise date calculations that doesn't just treat   months as 30 days, then have a look at Time#advance.

  Some of these methods are approximations, Ruby's core Date and   Time should be used for precision date and time arithmetic

-- Cheers, - Jacob Atzen

Just read the right docs:

Time#months_ago and Time#months_since

http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Time/Calculations.html#M000330

>> Time.now.months_since(1) => Sat Sep 01 18:57:06 -0400 2007

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com