Skip to content

Commit a9b71c6

Browse files
committed
Merge pull request #12370 from JuliaLang/teh/dates
Handle Dates-array arithmetic with promote_op
2 parents aab4179 + 01fd0a1 commit a9b71c6

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

base/dates/arithmetic.jl

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ for op in (:.+, :.-)
8282
($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y)
8383
($op_){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = ($op)(x,y)
8484
($op_){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = ($op)(x,y)
85-
86-
# AbstractArray{TimeType}, StridedArray{GeneralPeriod}
87-
($op_){T<:TimeType,P<:GeneralPeriod}(x::Range{T}, y::StridedArray{P}) = ($op_)(collect(x),y)
88-
($op_){T<:TimeType,P<:GeneralPeriod}(x::AbstractArray{T}, y::StridedArray{P}) =
89-
reshape(TimeType[($op_)(x[i],y[i]) for i in eachindex(x, y)], promote_shape(size(x),size(y)))
90-
($op_){T<:TimeType,P<:GeneralPeriod}(y::StridedArray{P}, x::AbstractArray{T}) = ($op_)(x,y)
9185
end
9286
end
9387

@@ -100,7 +94,29 @@ end
10094
# AbstractArray{TimeType}, AbstractArray{TimeType}
10195
(-){T<:TimeType}(x::OrdinalRange{T}, y::OrdinalRange{T}) = collect(x) - collect(y)
10296
(-){T<:TimeType}(x::Range{T}, y::Range{T}) = collect(x) - collect(y)
103-
(-){T<:TimeType}(x::AbstractArray{T}, y::Range{T}) = y - collect(x)
104-
(-){T<:TimeType}(x::Range{T}, y::AbstractArray{T}) = collect(x) - y
105-
(-){T<:TimeType}(x::AbstractArray{T}, y::AbstractArray{T}) =
106-
reshape(Period[x[i] - y[i] for i in eachindex(x, y)], promote_shape(size(x),size(y)))
97+
98+
# promotion rules
99+
100+
for (op,F) in ((:+, Base.AddFun),
101+
(:-, Base.SubFun),
102+
(:.+, Base.DotAddFun),
103+
(:.-, Base.DotSubFun))
104+
@eval begin
105+
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = P
106+
Base.promote_op{P1<:Period,P2<:Period}(::$F, ::Type{P1}, ::Type{P2}) = CompoundPeriod
107+
Base.promote_op{D<:Date}(::$F, ::Type{D}, ::Type{D}) = Day
108+
Base.promote_op{D<:DateTime}(::$F, ::Type{D}, ::Type{D}) = Millisecond
109+
end
110+
end
111+
112+
for (op,F) in ((:/, Base.RDivFun),
113+
(:%, Base.RemFun),
114+
(:div, Base.IDivFun),
115+
(:mod, Base.ModFun),
116+
(:./, Base.DotRDivFun),
117+
(:.%, Base.DotRemFun))
118+
@eval begin
119+
Base.promote_op{P<:Period}(::$F, ::Type{P}, ::Type{P}) = typeof($op(1,1))
120+
Base.promote_op{P<:Period,R<:Real}(::$F, ::Type{P}, ::Type{R}) = P
121+
end
122+
end

base/functors.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ call(::DotMulFun, x, y) = x .* y
5858
immutable RDivFun <: Func{2} end
5959
call(::RDivFun, x, y) = x / y
6060

61+
immutable DotRDivFun <: Func{2} end
62+
call(::DotRDivFun, x, y) = x ./ y
63+
6164
immutable LDivFun <: Func{2} end
6265
call(::LDivFun, x, y) = x \ y
6366

0 commit comments

Comments
 (0)