Skip to content

Commit

Permalink
Merge pull request rails#3725 from marcandre/twz_eql
Browse files Browse the repository at this point in the history
Fix inconsistencies with Time{WithZone}#{hash,eql?}
  • Loading branch information
wycats committed Nov 22, 2011
2 parents 9b7be78 + a491207 commit a93ee92
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/core_ext/time/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,14 @@ def compare_with_coercion(other)
end
alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion

# Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances
# can be eql? to an equivalent Time
def eql_with_coercion(other)
# if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do eql? comparison
other = other.comparable_time if other.respond_to?(:comparable_time)
eql_without_coercion(other)
end
alias_method :eql_without_coercion, :eql?
alias_method :eql?, :eql_with_coercion
end
7 changes: 5 additions & 2 deletions activesupport/lib/active_support/time_with_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ def future?
end

def eql?(other)
utc == other
utc.eql?(other)
end

def hash
utc.hash
end

def +(other)
Expand Down Expand Up @@ -277,7 +281,6 @@ def to_f
def to_i
utc.to_i
end
alias_method :hash, :to_i
alias_method :tv_sec, :to_i

# A TimeWithZone acts like a Time, so just return +self+.
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ def test_compare_with_time_with_zone
assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] ))
end

def test_eql?
assert_equal true, Time.utc(2000).eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC']) )
assert_equal true, Time.utc(2000).eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
assert_equal false,Time.utc(2000, 1, 1, 0, 0, 1).eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC']) )
end

def test_minus_with_time_with_zone
assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] )
end
Expand Down
11 changes: 9 additions & 2 deletions activesupport/test/core_ext/time_with_zone_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,15 @@ def future_with_time_current_as_time_with_zone
end

def test_eql?
assert @twz.eql?(Time.utc(2000))
assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
assert_equal true, @twz.eql?(Time.utc(2000))
assert_equal true, @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) )
assert_equal false, @twz.eql?( Time.utc(2000, 1, 1, 0, 0, 1) )
assert_equal false, @twz.eql?( DateTime.civil(1999, 12, 31, 23, 59, 59) )
end

def test_hash
assert_equal Time.utc(2000).hash, @twz.hash
assert_equal Time.utc(2000).hash, ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]).hash
end

def test_plus_with_integer
Expand Down

0 comments on commit a93ee92

Please sign in to comment.