Skip to content

Commit

Permalink
Merge pull request #12370 from JuliaLang/teh/dates
Browse files Browse the repository at this point in the history
Handle Dates-array arithmetic with promote_op
  • Loading branch information
timholy committed Jul 30, 2015
2 parents aab4179 + 01fd0a1 commit a9b71c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
36 changes: 26 additions & 10 deletions base/dates/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ for op in (:.+, :.-)
($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y)
($op_){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = ($op)(x,y)
($op_){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y)

# AbstractArray{TimeType}, StridedArray{GeneralPeriod}
($op_){T<:TimeType,P<:GeneralPeriod}(x::Range{T}, y::StridedArray{P}) = ($op_)(collect(x),y)
($op_){T<:TimeType,P<:GeneralPeriod}(x::AbstractArray{T}, y::StridedArray{P}) =
reshape(TimeType[($op_)(x[i],y[i]) for i in eachindex(x, y)], promote_shape(size(x),size(y)))
($op_){T<:TimeType,P<:GeneralPeriod}(y::StridedArray{P}, x::AbstractArray{T}) = ($op_)(x,y)
end
end

Expand All @@ -100,7 +94,29 @@ end
# AbstractArray{TimeType}, AbstractArray{TimeType}
(-){T<:TimeType}(x::OrdinalRange{T}, y::OrdinalRange{T}) = collect(x) - collect(y)
(-){T<:TimeType}(x::Range{T}, y::Range{T}) = collect(x) - collect(y)
(-){T<:TimeType}(x::AbstractArray{T}, y::Range{T}) = y - collect(x)
(-){T<:TimeType}(x::Range{T}, y::AbstractArray{T}) = collect(x) - y
(-){T<:TimeType}(x::AbstractArray{T}, y::AbstractArray{T}) =
reshape(Period[x[i] - y[i] for i in eachindex(x, y)], promote_shape(size(x),size(y)))

# promotion rules

for (op,F) in ((:+, Base.AddFun),
(:-, Base.SubFun),
(:.+, Base.DotAddFun),
(:.-, Base.DotSubFun))
@eval begin
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = P
Base.promote_op{P1<:Period,P2<:Period}(::$F, ::Type{P1}, ::Type{P2}) = CompoundPeriod
Base.promote_op{D<:Date}(::$F, ::Type{D}, ::Type{D}) = Day
Base.promote_op{D<:DateTime}(::$F, ::Type{D}, ::Type{D}) = Millisecond
end
end

for (op,F) in ((:/, Base.RDivFun),
(:%, Base.RemFun),
(:div, Base.IDivFun),
(:mod, Base.ModFun),
(:./, Base.DotRDivFun),
(:.%, Base.DotRemFun))
@eval begin
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = typeof($op(1,1))
Base.promote_op{P<:Period,R<:Real}(::$F, ::Type{P}, ::Type{R}) = P
end
end
3 changes: 3 additions & 0 deletions base/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ call(::DotMulFun, x, y) = x .* y
immutable RDivFun <: Func{2} end
call(::RDivFun, x, y) = x / y

immutable DotRDivFun <: Func{2} end
call(::DotRDivFun, x, y) = x ./ y

immutable LDivFun <: Func{2} end
call(::LDivFun, x, y) = x \ y

Expand Down

0 comments on commit a9b71c6

Please sign in to comment.