Skip to content

Commit 40c3b2e

Browse files
authored
Merge pull request #37834 from sostock/compoundperiodhash
Fix hashing of Dates.CompoundPeriod
2 parents ff20ff0 + 15dda72 commit 40c3b2e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

stdlib/Dates/src/periods.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,14 @@ Base.hash(x::Year, h::UInt) = hash(12 * value(x), h + otherperiod_seed(x))
472472
Base.hash(x::Quarter, h::UInt) = hash(3 * value(x), h + otherperiod_seed(x))
473473
Base.hash(x::Month, h::UInt) = hash(value(x), h + otherperiod_seed(x))
474474

475+
function Base.hash(x::CompoundPeriod, h::UInt)
476+
isempty(x.periods) && return hash(0, h + zero_or_fixedperiod_seed)
477+
for p in x.periods
478+
h = hash(p, h)
479+
end
480+
return h
481+
end
482+
475483
Base.isless(x::FixedPeriod, y::OtherPeriod) = throw(MethodError(isless, (x, y)))
476484
Base.isless(x::OtherPeriod, y::FixedPeriod) = throw(MethodError(isless, (x, y)))
477485

stdlib/Dates/test/periods.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,14 @@ end
477477
end
478478
end
479479
end
480+
@testset "Hashing for CompoundPeriod (#37447)" begin
481+
periods = [Dates.Year(0), Dates.Minute(0), Dates.Second(0), Dates.CompoundPeriod(),
482+
Dates.Minute(2), Dates.Second(120), Dates.CompoundPeriod(Dates.Minute(2)),
483+
Dates.CompoundPeriod(Dates.Second(120)), Dates.CompoundPeriod(Dates.Minute(1), Dates.Second(60))]
484+
for x = periods, y = periods
485+
@test isequal(x,y) == (hash(x) == hash(y))
486+
end
487+
end
480488

481489
@testset "#30832" begin
482490
@test Dates.toms(Dates.Second(1) + Dates.Nanosecond(1)) == 1e3

0 commit comments

Comments
 (0)