Skip to content

Commit

Permalink
Switched isequal and hash to just use utc_datetime (#147)
Browse files Browse the repository at this point in the history
* Switched `isequal` and `hash` to just use `utc_datetime`.

NOTE: This is a breaking change that'll make most of our lives easier
(e.g., joins with `DataFrame`s) :)

* PR review fixes.
  • Loading branch information
rofinn authored and omus committed Aug 15, 2018
1 parent 2affcd4 commit 47a0c86
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
18 changes: 7 additions & 11 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/discovery.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 47a0c86

Please sign in to comment.