Skip to content

Commit

Permalink
Allow multiplication/division of Periods by non-integers (#34639)
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock authored Feb 7, 2020
1 parent b0a15d0 commit 4ac9657
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions stdlib/Dates/src/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ for op in (:+, :-, :lcm, :gcd)
end

/(x::P, y::P) where {P<:Period} = /(value(x), value(y))
/(x::P, y::Real) where {P<:Period} = P(/(value(x), Int64(y)))
/(x::P, y::Real) where {P<:Period} = P(/(value(x), y))
div(x::P, y::P, r::RoundingMode) where {P<:Period} = div(value(x), value(y), r)
div(x::P, y::Real, r::RoundingMode) where {P<:Period} = P(div(value(x), Int64(y), r))

Expand All @@ -90,7 +90,7 @@ for op in (:rem, :mod)
end
end

(*)(x::P, y::Real) where {P<:Period} = P(value(x) * Int64(y))
(*)(x::P, y::Real) where {P<:Period} = P(value(x) * y)
(*)(y::Real, x::Period) = x * y

# intfuncs
Expand Down
11 changes: 10 additions & 1 deletion stdlib/Dates/test/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,16 @@ end
@test y + Dates.Year(1f0) == Dates.Year(2)
@test y * 4 == Dates.Year(4)
@test y * 4f0 == Dates.Year(4)
@test_throws InexactError y * 3//4 == Dates.Year(1)
@test Dates.Year(2) * 0.5 == y
@test Dates.Year(2) * 3//2 == Dates.Year(3)
@test_throws InexactError y * 0.5
@test_throws InexactError y * 3//4
@test Dates.Year(4) / 2 == Dates.Year(2)
@test Dates.Year(4) / 2f0 == Dates.Year(2)
@test Dates.Year(4) / 0.5 == Dates.Year(8)
@test Dates.Year(4) / 2//3 == Dates.Year(6)
@test_throws InexactError Dates.Year(4) / 3.0
@test_throws InexactError Dates.Year(4) / 3//2
@test div(y, 2) == Dates.Year(0)
@test_throws MethodError div(2, y) == Dates.Year(2)
@test div(y, y) == 1
Expand Down

0 comments on commit 4ac9657

Please sign in to comment.