This project is archived and is in readonly mode.
TimeWithZone comparison fails for distinct instances of same time
-
Ari Epstein
- Tag set to activesupport, datetime, time, timewithzone
-
Matt Duncan
I was unable to reproduce this on master.
Loading development environment (Rails 3.0.pre) >> r = Role.create(:name => 'a role') => #<Role id: 2, name: "a role", created_at: "2009-08-30 16:10:18", updated_at: "2009-08-30 16:10:18"> >> initial = r.created_at => Sun, 30 Aug 2009 16:10:18 UTC +00:00 >> r.reload => #<Role id: 2, name: "a role", created_at: "2009-08-30 16:10:18", updated_at: "2009-08-30 16:10:18"> >> initial == r.created_at => true -
Matt Jones
I've seen this happen in tests before - typically caused by the database having a different time resolution than the system. So even though the two times print identically, one is really (for instance) 15:45:32.012445362 and the DB loads back 15:45:32, which doesn't compare as equal.
-
JackC
Just ran into this in tests myself. You can determine the usec method to see the difference. I worked around this in my tests by specifying times instead of using the current time.
-
Geoff Buesing
- State changed from new to invalid
Matt Jones is probably right; this looks to be an issue with the database not storing usec.
In your tests, you can try coercing to_a before comparing; usec value isn't returned in the to_a representation:
assert_equal initial.to_a, r.created_at.to_a