diff --git a/stdlib/Dates/src/periods.jl b/stdlib/Dates/src/periods.jl index 9b7e29496e642..d8995cc9066d1 100644 --- a/stdlib/Dates/src/periods.jl +++ b/stdlib/Dates/src/periods.jl @@ -325,7 +325,7 @@ end Base.show(io::IO,x::CompoundPeriod) = print(io, string(x)) Base.convert(::Type{T}, x::CompoundPeriod) where T<:Period = - isconcretetype(T) ? sum(T, x.periods) : throw(MethodError(convert,(T,x))) + isconcretetype(T) ? sum(T, x.periods; init = zero(T)) : throw(MethodError(convert,(T,x))) # E.g. Year(1) + Day(1) (+)(x::Period,y::Period) = CompoundPeriod(Period[x, y]) diff --git a/stdlib/Dates/test/periods.jl b/stdlib/Dates/test/periods.jl index 7b23ffcb5d4e1..0f00694e21f74 100644 --- a/stdlib/Dates/test/periods.jl +++ b/stdlib/Dates/test/periods.jl @@ -523,6 +523,7 @@ end @test convert(Second, Minute(1) + Second(30)) === Second(90) @test convert(Minute, Minute(1) + Second(60)) === Minute(2) @test convert(Millisecond, Minute(1) + Second(30)) === Millisecond(90_000) + @test convert(Millisecond, Dates.CompoundPeriod()) === Millisecond(0) @test_throws InexactError convert(Minute, Minute(1) + Second(30)) @test_throws MethodError convert(Month, Minute(1) + Second(30)) @test_throws MethodError convert(Second, Month(1) + Second(30))