Skip to content

Commit ef073e1

Browse files
authored
add promoting ops for Dates.Period (#782)
1 parent dce2f96 commit ef073e1

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "4.2.0"
3+
version = "4.3.0"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ changes in `julia`.
7070

7171
## Supported features
7272

73+
* `div`, `lcm`, `gcd`, `/`, `rem`, and `mod` will `promote` heterogenous `Dates.Period`s ([`@bdf9ead9`]). (since Compat 4.3.0)
74+
7375
* `stack` combines a collection of slices into one array ([#43334]). (since Compat 4.2.0)
7476

7577
* `keepat!` removes the items at all the indices which are not given and returns
@@ -153,3 +155,4 @@ Note that you should specify the correct minimum version for `Compat` in the
153155
[#42351]: https://github.com/JuliaLang/julia/issues/42351
154156
[#43354]: https://github.com/JuliaLang/julia/issues/43354
155157
[#43334]: https://github.com/JuliaLang/julia/issues/43334
158+
[`@bdf9ead9`]: https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329

src/Compat.jl

+11
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ if VERSION < v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
320320
allequal(r::AbstractRange) = iszero(step(r)) || length(r) <= 1
321321
end
322322

323+
# https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329
324+
if VERSION < v"1.8.0-DEV.1109"
325+
# we do not add the methods for == and isless that are included in the above
326+
# commit, since they are already present in earlier versions.
327+
import Base: /, rem, mod, lcm, gcd, div
328+
for op in (:/, :rem, :mod, :lcm, :gcd)
329+
@eval ($op)(x::Period, y::Period) = ($op)(promote(x, y)...)
330+
end
331+
div(x::Period, y::Period, r::RoundingMode) = div(promote(x, y)..., r)
332+
end
333+
323334
# This function is available as of Julia 1.7.
324335
@static if !isdefined(Base, :keepat!)
325336
export keepat!

test/runtests.jl

+12
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,15 @@ end
556556
@test_throws ArgumentError stack([])
557557
@test_throws ArgumentError stack(x for x in 1:3 if false)
558558
end
559+
560+
@testset "promoting ops for TimePeriod" begin
561+
x = 10
562+
y = 3
563+
xs = Second(x)
564+
ys = Second(y)
565+
xms = Millisecond(xs)
566+
yms = Millisecond(ys)
567+
for op in (/, div, rem, mod, lcm, gcd)
568+
@test op(xms, yms) == op(xms, ys) == op(xs, yms)
569+
end
570+
end

0 commit comments

Comments
 (0)