diff --git a/src/types.jl b/src/types.jl index eafb823bd..db197a7ea 100644 --- a/src/types.jl +++ b/src/types.jl @@ -283,24 +283,20 @@ end # Equality ==(a::ZonedDateTime, b::ZonedDateTime) = a.utc_datetime == b.utc_datetime isless(a::ZonedDateTime, b::ZonedDateTime) = isless(a.utc_datetime, b.utc_datetime) +isequal(a::ZonedDateTime, b::ZonedDateTime) = isequal(a.utc_datetime, b.utc_datetime) -# Note: `hash` and `isequal` assume that the "zone" of a ZonedDateTime is not being set -# incorrectly. +""" + hash(::ZonedDateTime, h) +Compute an integer hash code for a ZonedDateTime by hashing the `utc_datetime` field. +`hash(:utc_instant, h)` is used to avoid collisions with `DateTime` hashes. +""" function hash(zdt::ZonedDateTime, h::UInt) + h = hash(:utc_instant, h) h = hash(zdt.utc_datetime, h) - h = hash(zdt.timezone, h) return h end -function isequal(a::ZonedDateTime, b::ZonedDateTime) - return ( - isequal(a.utc_datetime, b.utc_datetime) && - isequal(a.timezone, b.timezone) && - isequal(a.zone, b.zone) - ) -end - function ==(a::VariableTimeZone, b::VariableTimeZone) a.name == b.name && a.transitions == b.transitions end diff --git a/test/conversions.jl b/test/conversions.jl index f8136c2bf..5d89226ee 100644 --- a/test/conversions.jl +++ b/test/conversions.jl @@ -93,7 +93,7 @@ round_trip = TimeZones.unix2zdt(TimeZones.zdt2unix(Int64, zdt)) zdt = ZonedDateTime(2010, 1, 2, 3, 4, 5, warsaw) round_trip = TimeZones.unix2zdt(TimeZones.zdt2unix(Int64, zdt)) @test round_trip == zdt -@test !isequal(round_trip, zdt) +@test TimeZones.timezone(round_trip) != TimeZones.timezone(zdt) # Julian dates jd = 2457241.855 diff --git a/test/discovery.jl b/test/discovery.jl index b2c3a628a..77b00ce8f 100644 --- a/test/discovery.jl +++ b/test/discovery.jl @@ -41,10 +41,10 @@ paris = resolve("Europe/Paris", tzdata["europe"]...) expected_instant = ZonedDateTime(DateTime(2018, 3, 11, 8), wpg, zone) expected_valid = ZonedDateTime(2018, 3, 11, 3, wpg) - @test isequal(instant, expected_instant) + @test instant === expected_instant @test instant == expected_valid - @test !isequal(instant, expected_valid) - @test isequal(instant + Millisecond(0), expected_valid) + @test instant !== expected_valid + @test instant + Millisecond(0) === expected_valid end @testset "ambiguous" begin @@ -54,10 +54,10 @@ paris = resolve("Europe/Paris", tzdata["europe"]...) expected_instant = ZonedDateTime(DateTime(2018, 11, 4, 7), wpg, zone) expected_valid = ZonedDateTime(2018, 11, 4, 1, wpg, 2) - @test isequal(instant, expected_instant) + @test instant === expected_instant @test instant == expected_valid - @test !isequal(instant, expected_valid) - @test isequal(instant + Millisecond(0), expected_valid) + @test instant !== expected_valid + @test instant + Millisecond(0) === expected_valid end @testset "upcoming" begin diff --git a/test/types.jl b/test/types.jl index e48c840ce..ff4018e54 100644 --- a/test/types.jl +++ b/test/types.jl @@ -277,8 +277,8 @@ early_utc = ZonedDateTime(DateTime(UTM(typemin(Int64))), utc) @test spring_apia.zone == FixedTimeZone("SST", -39600, 0) @test spring_utc == spring_apia @test spring_utc !== spring_apia -@test !isequal(spring_utc, spring_apia) -@test hash(spring_utc) != hash(spring_apia) +@test isequal(spring_utc, spring_apia) +@test hash(spring_utc) == hash(spring_apia) @test astimezone(spring_utc, apia) === spring_apia # Since ZonedDateTime is immutable @test astimezone(spring_apia, utc) === spring_utc @test isequal(astimezone(spring_utc, apia), spring_apia) @@ -293,8 +293,8 @@ fall_apia = ZonedDateTime(DateTime(2010, 10, 1, 2), apia) @test fall_utc !== fall_apia @test !(fall_utc < fall_apia) @test !(fall_utc > fall_apia) -@test !isequal(fall_utc, fall_apia) -@test hash(fall_utc) != hash(fall_apia) +@test isequal(fall_utc, fall_apia) +@test hash(fall_utc) == hash(fall_apia) @test astimezone(fall_utc, apia) === fall_apia # Since ZonedDateTime is immutable @test astimezone(fall_apia, utc) === fall_utc @test isequal(astimezone(fall_utc, apia), fall_apia) @@ -311,6 +311,9 @@ y = deepcopy(x) @test isequal(x, y) @test hash(x) == hash(y) +# Check that the ZonedDateTime and plain DateTimes don't hash to the same value. +@test hash(x) != hash(y.utc_datetime) + # A FixedTimeZone is effective for all of time where as a VariableTimeZone has as start. @test TimeZones.utc(early_utc) < apia.transitions[1].utc_datetime