From: Johan Veenstra Date: 2007-02-09T06:17:38+09:00 Subject: Re: Calculating a future date ------=_Part_16728_22980617.1170969441299 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 2/6/07, Alexandru E. Ungur wrote: > > >>> sender: "Toine" date: "Tue, Feb 06, 2007 at 08:00:06PM +0900" << > Date.today + 31 and Time.now + 31*24*60*60 yield the same results. > Just beware that although they yield the same result they do it at > *very* different speeds: > > ~>> cat date_vs_time.rb > require 'benchmark' > require 'date' > > n = 100_000 > Benchmark.bm do |x| > x.report { n.times do a = Date.today+31; end } > x.report { n.times do a = Time.now+2678400; end } > end > > > ~>> ruby date_vs_time.rb > user system total real > 24.300000 0.760000 25.060000 ( 31.808122) > 0.410000 0.440000 0.850000 ( 0.971718) > > > So, whenever you can get away with Time, use it. Only use Date when > you have to. > > > Cheers, > Alex > > require 'date' puts Date.today puts Date.today + 100 puts Time.now puts Time.now + 100*24*60*60 2007-02-08 2007-05-19 Thu Feb 08 22:05:49 W. Europe Standard Time 2007 Sat May 19 23:05:49 W. Europe Daylight Time 2007 Looks innocent enough, but when I run the same program again in 60 minutes, Time.now + 100*60*60 would output: Sun May 20 00:05:49 W. Europe Daylight Time 2007 (which is my birthday by the way). So from a correctness point of view, use Date when you are dealing with dates, and use Time when you are dealing with time. ------=_Part_16728_22980617.1170969441299--