You use different parameters for Time objects.
If you donβt miss the minutes, in my system the time zone will change:
t2 = Time.new(2007,01,15,11,16,0.1) => 2007-01-15 11:16:00 +0100 # what happened here? '+0100' t1 == t2 => false
Even if this is not the case for op, it is interesting that this happens in my irb. Building such Time.utc(2007,01,15,11,15,30,0.1)
objects Time.utc(2007,01,15,11,15,30,0.1)
using an explicit time zone returned exactly the behavior that you described.
However, if you pass the same number of parameters (0 minutes for t2), the comparison will return the expected result, since you probably will not even encounter the floating point problem described by joanbm.
t = Time.new(2007,01,15,11,15,30,0.1) => 2007-01-15 11:15:30 +0000 t1 = t + 30 => 2007-01-15 11:16:00 +0000 t2 = Time.new(2007,01,15,11,16,0,0.1) => 2007-01-15 11:16:00 +0000 t1 == t2 => true
If you print a rational form of time, you get:
t1.to_r => (42112611033072059383231283/36028797018963968) t2.to_r => (42112611033072059383231283/36028797018963968)
As to_r
returns the time in seconds with epoch (or Unix Time), there is no reason for t1 and t2 not to be equal if the date, time and time zone match.
Unfortunately, I cannot give a good explanation of why this is so. This seems interesting, although I will return when I find the reason for this behavior.
Edit
This does not explain the time zone change, therefore it does not explain the problem, but you do not have a comma for t
, so you actually pass 30.1 instead of 30 seconds.
t = Time.new(2007,01,15,11,15,30.1)
It should probably be
t = Time.new(2007,01,15,11,15,30,0.1)
However, this does not fail your comparison.