Skip to content

Commit 1279de6

Browse files
authored
fix conversion of empty Dates.CompoundPeriod() to zero units (#50259)
* fix(stdlib/Dates/periods.jl): conversion of empty CompoundPeriod to zero units * add(stdlib/Dates/test/periods.jl): add test for empty CompoundPeriod
1 parent 63fefe0 commit 1279de6

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

stdlib/Dates/src/periods.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ end
325325
Base.show(io::IO,x::CompoundPeriod) = print(io, string(x))
326326

327327
Base.convert(::Type{T}, x::CompoundPeriod) where T<:Period =
328-
isconcretetype(T) ? sum(T, x.periods) : throw(MethodError(convert,(T,x)))
328+
isconcretetype(T) ? sum(T, x.periods; init = zero(T)) : throw(MethodError(convert,(T,x)))
329329

330330
# E.g. Year(1) + Day(1)
331331
(+)(x::Period,y::Period) = CompoundPeriod(Period[x, y])

stdlib/Dates/test/periods.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ end
523523
@test convert(Second, Minute(1) + Second(30)) === Second(90)
524524
@test convert(Minute, Minute(1) + Second(60)) === Minute(2)
525525
@test convert(Millisecond, Minute(1) + Second(30)) === Millisecond(90_000)
526+
@test convert(Millisecond, Dates.CompoundPeriod()) === Millisecond(0)
526527
@test_throws InexactError convert(Minute, Minute(1) + Second(30))
527528
@test_throws MethodError convert(Month, Minute(1) + Second(30))
528529
@test_throws MethodError convert(Second, Month(1) + Second(30))

0 commit comments

Comments
 (0)