Skip to content

Commit

Permalink
add promoting ops for Dates.Period (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinschmidt authored Oct 6, 2022
1 parent dce2f96 commit ef073e1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.2.0"
version = "4.3.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ changes in `julia`.

## Supported features

* `div`, `lcm`, `gcd`, `/`, `rem`, and `mod` will `promote` heterogenous `Dates.Period`s ([`@bdf9ead9`]). (since Compat 4.3.0)

* `stack` combines a collection of slices into one array ([#43334]). (since Compat 4.2.0)

* `keepat!` removes the items at all the indices which are not given and returns
Expand Down Expand Up @@ -153,3 +155,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#42351]: https://github.com/JuliaLang/julia/issues/42351
[#43354]: https://github.com/JuliaLang/julia/issues/43354
[#43334]: https://github.com/JuliaLang/julia/issues/43334
[`@bdf9ead9`]: https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329
11 changes: 11 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ if VERSION < v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
allequal(r::AbstractRange) = iszero(step(r)) || length(r) <= 1
end

# https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329
if VERSION < v"1.8.0-DEV.1109"
# we do not add the methods for == and isless that are included in the above
# commit, since they are already present in earlier versions.
import Base: /, rem, mod, lcm, gcd, div
for op in (:/, :rem, :mod, :lcm, :gcd)
@eval ($op)(x::Period, y::Period) = ($op)(promote(x, y)...)
end
div(x::Period, y::Period, r::RoundingMode) = div(promote(x, y)..., r)
end

# This function is available as of Julia 1.7.
@static if !isdefined(Base, :keepat!)
export keepat!
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,15 @@ end
@test_throws ArgumentError stack([])
@test_throws ArgumentError stack(x for x in 1:3 if false)
end

@testset "promoting ops for TimePeriod" begin
x = 10
y = 3
xs = Second(x)
ys = Second(y)
xms = Millisecond(xs)
yms = Millisecond(ys)
for op in (/, div, rem, mod, lcm, gcd)
@test op(xms, yms) == op(xms, ys) == op(xs, yms)
end
end

2 comments on commit ef073e1

@martinholters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/69617

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v4.3.0 -m "<description of version>" ef073e1ce7426eba2b3feb74ae17b61ec7f8bb3f
git push origin v4.3.0

Please sign in to comment.