I need to use a Time object as a int (TimeObject.to_i)
Then i need to convert a int back to a Time, to compare against the original Time.
Short Example
t1 = Time.now
t2 = Time.at(t1.to_i)
puts t1 == t2 # Says False
puts t1.eql?(t2) # Says False
Why this says its false ?
When i print both Time objetcs shows the same thing D:
puts t1 #shows : 2012-01-06 16:01:53 -0300
puts t2 #shows : 2012-01-06 16:01:53 -0300
puts t1.to_a.to_s #shows : [ 53, 1, 16, 6, 1, 2012, 5, 6, true, "CLST"]
puts t2.to_a.to_s #shows : [ 53, 1, 16, 6, 1, 2012, 5, 6, true, "CLST"]
they are the same thing D: but when trying to compare with == or eql? says they are diferent
(sorry for my bad english)
Answer
Explanation:
Link: Time#eql?
So, apparently, fractions of seconds get dropped when performing
#to_iand then restored time is not exactly the same as original one. But if we restore two copies, they will be equal.One may think, “Hey, let’s use
#to_fthen!”. But, surprisingly enough, results are the same! Maybe it’s because of rounding errors or floating point comparison, not sure.Alternate answer
Don’t convert integer back to time for comparison. Convert original time to int instead!