This project is archived and is in readonly mode.

#3117 ✓invalid
Ari Epstein

TimeWithZone comparison fails for distinct instances of same time

Reported by Ari Epstein | August 30th, 2009 @ 03:05 PM

Let's say I have a model "Role" with timestamps and a string name attribute. I want to do some time comparisons. Here's the expected versus actual that I see:


r = Role.create(:name => 'a role')
=> true


r=Role.create(:name => 'a role') => # initial = r.created_at => Sun, 30 Aug 2009 09:46:14 EDT -04:00 r.reload => # r.created_at == initial => false

I've tried <=>, converting to other types (Time, DateTime), and the comparison always fails. What's going on?

Comments and changes to this ticket

  • Ari Epstein

    Ari Epstein August 30th, 2009 @ 03:05 PM

    • Tag set to activesupport, datetime, time, timewithzone
  • Matt Duncan

    Matt Duncan August 30th, 2009 @ 05:12 PM

    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

    Matt Jones September 5th, 2009 @ 05:42 AM

    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

    JackC October 5th, 2009 @ 07:47 PM

    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

    Geoff Buesing October 27th, 2009 @ 02:34 AM

    • 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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>