Skip to content

Commit b7977ca

Browse files
JeffBezansonKristofferC
authored andcommitted
Fix hashing of Dates.Time. Fixes #29480 (#29742)
(cherry picked from commit 1db6047)
1 parent 14fab21 commit b7977ca

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

stdlib/Dates/src/types.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ function ==(a::Time, b::Time)
350350
microsecond(a) == microsecond(b) && nanosecond(a) == nanosecond(b)
351351
end
352352
(==)(x::TimeType, y::TimeType) = (===)(promote(x, y)...)
353+
Base.hash(x::Time, h::UInt) =
354+
hash(hour(x), hash(minute(x), hash(second(x),
355+
hash(millisecond(x), hash(microsecond(x), hash(nanosecond(x), h))))))
353356

354357
import Base: sleep, Timer, timedwait
355358
sleep(time::Period) = sleep(toms(time) / 1000)

stdlib/Dates/test/types.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ c = Dates.Time(0)
194194
@test isfinite(Dates.Date)
195195
@test isfinite(Dates.DateTime)
196196
@test isfinite(Dates.Time)
197+
@test c == c
198+
@test c == (c + Dates.Hour(24))
199+
@test hash(c) == hash(c + Dates.Hour(24))
200+
@test hash(c + Dates.Nanosecond(10)) == hash(c + Dates.Hour(24) + Dates.Nanosecond(10))
197201
end
198202
@testset "Date-DateTime conversion/promotion" begin
199203
global a, b, c, d

0 commit comments

Comments
 (0)