jeremyevans / home_run

Fast Date/DateTime classes for ruby :: Unmaintained, unnecessary on ruby 1.9.3+
Other
465 stars 10 forks source link

DateTime#<=> different from stdlib #36

Closed cgriego closed 13 years ago

cgriego commented 13 years ago

I ran into another incompatibility with ActiveSupport 3.1, but I had to fight through tzinfo to get there. This time, it's an undocumented difference between home_run and stdlib. The difference is that DateTime has its own implementation of <=> rather than inheriting it from Date. Here is a minimal test case:

require 'date'

class DateTime
  def <=>(other)
    super
  end
end

a = DateTime.parse("1967-10-29T08:00:00+00:00")
b = DateTime.parse("2010-12-17T17:00:00+00:00")

puts "#{a} #{b}" #=> 1967-10-29T08:00:00+00:00 2010-12-17T17:00:00+00:00
a <=> b
puts "#{a} #{b}" #=> 67503-10-29T08:00:00+00:00 2010-12-17T17:00:00+00:00

Isn't that neat how the DateTime changed from year 1967 to year 67503?

ActiveSupport is affected by this difference because it has this code:

class DateTime
  def <=>(other)
    super other.to_datetime
  end
end

Older versions of ActiveSupport used alias_method chains to achieve the same effect, which doesn't trigger the difference from stdlib. I'm not sure what action you'll want to take, but at the very least this should be documented.

jeremyevans commented 13 years ago

This is a bug in ActiveSupport and should be reported on the rails bugtracker. The fact that Date#<=> works with DateTime instances is an implementation detail that shouldn't be relied on. See https://github.com/ruby/ruby/blob/trunk/ext/date/date_core.c#L6321 (the RDoc for Date#<=>), it doesn't mention DateTime.

cgriego commented 13 years ago

Thanks for the input. I opened a pull request on ActiveSupport. https://github.com/rails/rails/pull/2797