Beware of comparing times in Rails
I posed this rails question to my team today:
date1 # => Sat, 12 Sep 2009 09:08:33 +0000
date2 # => Sat, 12 Sep 2009 09:08:33 +0000
date1 == date2 # => false
So why can this comparison be false?
The answer is that the two dates are in fact not identical times. See:
date1.to_f # => 1252746513.25
date2.to_f # => 1252746513.75
They both have a decimal component which is not shown when you simply print (to_s) their values. This can come up when you are importing data with timestamps that include a decimal component and then later make comparisons on that data where you aren’t expecting (or wish to ignore) the fractions of a second. If you don’t want the decimal component of a time, strip it off before saving it to your database.
(Source: api.rubyonrails.org)