Having time and date methods on integers in Rails is nifty. Being able to say things like;
>> Date.today.to_time + 7.days...is nice.
=> Thu Feb 07 00:00:00 +0000 2008
But, be careful about relying on this when doing arithmetic with months.
>> Date.new( 2008, 1, 1 ).to_time + 1.monthWrong.
=> Thu Jan 31 00:00:00 +0000 2008
The problem is this;
>> 1.month / 86400.0i.e. a "month" is just 30 days' worth of seconds.
=> 30.0
Similarly;
>> ( Date.new( 2008, 2, 1 ).to_time + 1.month )There is a way to do month calculation correctly - use the built-in ActiveSupport::CoreExtensions::DateTime::Calculations like this;
=> Sun Mar 02 00:00:00 +0000 2008
>> Date.new( 2008, 1, 1 ).to_time.advance( :months => 1 )Much better.
=> Fri Feb 01 00:00:00 +0000 2008
>> Date.new( 2008, 2, 1 ).to_time.advance( :months => 1 )
=> Sat Mar 01 00:00:00 +0000 2008
|