Reputation: 3838
Hi I am unable to add two time components. Any idea why?
using Dates
t1 = Time(00,30,18)
t2 = Time(00,40,42)
t1 + t2
# Out >
ERROR: MethodError: no method matching +(::Time, ::Time)
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
+(::StridedArray{var"#s832", N} where {var"#s832"<:Union{Dates.CompoundPeriod, Period}, N}, ::TimeType) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/deprecated.jl:10
+(::Period, ::TimeType) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/arithmetic.jl:85
...
Stacktrace:
[1] top-level scope
@ REPL[5]:1
Upvotes: 1
Views: 82
Reputation: 216
The appropriate Julia type for your use case is Period
/CompoundPeriod
. To replicate your use case:
julia> p1 = Minute(30) + Second(18)
30 minutes, 18 seconds
julia> p2 = Minute(40) + Second(42)
40 minutes, 42 seconds
julia> typeof.((p1, p2))
(Dates.CompoundPeriod, Dates.CompoundPeriod)
julia> p1 + p2
70 minutes, 60 seconds
julia> typeof(ans)
Dates.CompoundPeriod
Upvotes: 1
Reputation: 3838
As stated in the comments below here is hack that I used to solve the above answer.
using Dates
t1 = Time(00,30,18)
t2 = Time(00,40,42)
t1.instant + t2.instant |> Time #t1.instant returns time in nanoseconds
# Out >
01:11:00
Upvotes: -3