Skip to content

Commit 5316061

Browse files
committed
Drop compat code for Dates.Period rounding from #462
1 parent b3deb13 commit 5316061

File tree

4 files changed

+73
-194
lines changed

4 files changed

+73
-194
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ Currently, the `@compat` macro supports the following syntaxes:
151151

152152
* `printstyled` prints to a given stream optionally in color and/or bolded ([#25522]).
153153

154-
* `Dates.Period` rounding (e.g., `round(Dates.Hour(36), Dates.Day, RoundNearestTiesUp) == Dates.Day(2)` ([#24182]).
155-
156154
* `firstindex` to obtain the first index of an iterable ([#25458]).
157155

158156
* `Compat.names` supporting keyword arguments for `all` and `imported` ([#25647]).

src/Compat.jl

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -26,125 +26,6 @@ import Libdl
2626

2727
include("compatmacro.jl")
2828

29-
# https://github.com/JuliaLang/julia/pull/24182
30-
if VERSION < v"0.7.0-DEV.2402"
31-
const ConvertiblePeriod = Union{Compat.Dates.TimePeriod, Compat.Dates.Week, Compat.Dates.Day}
32-
const TimeTypeOrPeriod = Union{Compat.Dates.TimeType, Compat.ConvertiblePeriod}
33-
34-
"""
35-
floor(x::Period, precision::T) where T <: Union{TimePeriod, Week, Day} -> T
36-
37-
Rounds `x` down to the nearest multiple of `precision`. If `x` and `precision` are different
38-
subtypes of `Period`, the return value will have the same type as `precision`.
39-
40-
For convenience, `precision` may be a type instead of a value: `floor(x, Dates.Hour)` is a
41-
shortcut for `floor(x, Dates.Hour(1))`.
42-
43-
```jldoctest
44-
julia> floor(Dates.Day(16), Dates.Week)
45-
2 weeks
46-
47-
julia> floor(Dates.Minute(44), Dates.Minute(15))
48-
30 minutes
49-
50-
julia> floor(Dates.Hour(36), Dates.Day)
51-
1 day
52-
```
53-
54-
Rounding to a `precision` of `Month`s or `Year`s is not supported, as these `Period`s are of
55-
inconsistent length.
56-
"""
57-
function Base.floor(x::Compat.ConvertiblePeriod, precision::T) where T <: Compat.ConvertiblePeriod
58-
Compat.Dates.value(precision) < 1 && throw(DomainError(precision))
59-
_x, _precision = promote(x, precision)
60-
return T(_x - mod(_x, _precision))
61-
end
62-
63-
"""
64-
ceil(x::Period, precision::T) where T <: Union{TimePeriod, Week, Day} -> T
65-
66-
Rounds `x` up to the nearest multiple of `precision`. If `x` and `precision` are different
67-
subtypes of `Period`, the return value will have the same type as `precision`.
68-
69-
For convenience, `precision` may be a type instead of a value: `ceil(x, Dates.Hour)` is a
70-
shortcut for `ceil(x, Dates.Hour(1))`.
71-
72-
```jldoctest
73-
julia> ceil(Dates.Day(16), Dates.Week)
74-
3 weeks
75-
76-
julia> ceil(Dates.Minute(44), Dates.Minute(15))
77-
45 minutes
78-
79-
julia> ceil(Dates.Hour(36), Dates.Day)
80-
3 days
81-
```
82-
83-
Rounding to a `precision` of `Month`s or `Year`s is not supported, as these `Period`s are of
84-
inconsistent length.
85-
"""
86-
function Base.ceil(x::Compat.ConvertiblePeriod, precision::Compat.ConvertiblePeriod)
87-
f = Base.floor(x, precision)
88-
return (x == f) ? f : f + precision
89-
end
90-
91-
"""
92-
floorceil(x::Period, precision::T) where T <: Union{TimePeriod, Week, Day} -> (T, T)
93-
94-
Simultaneously return the `floor` and `ceil` of `Period` at resolution `p`. More efficient
95-
than calling both `floor` and `ceil` individually.
96-
"""
97-
function floorceil(x::Compat.ConvertiblePeriod, precision::Compat.ConvertiblePeriod)
98-
f = Base.floor(x, precision)
99-
return f, (x == f) ? f : f + precision
100-
end
101-
102-
"""
103-
round(x::Period, precision::T, [r::RoundingMode]) where T <: Union{TimePeriod, Week, Day} -> T
104-
105-
Rounds `x` to the nearest multiple of `precision`. If `x` and `precision` are different
106-
subtypes of `Period`, the return value will have the same type as `precision`. By default
107-
(`RoundNearestTiesUp`), ties (e.g., rounding 90 minutes to the nearest hour) will be rounded
108-
up.
109-
110-
For convenience, `precision` may be a type instead of a value: `round(x, Dates.Hour)` is a
111-
shortcut for `round(x, Dates.Hour(1))`.
112-
113-
```jldoctest
114-
julia> round(Dates.Day(16), Dates.Week)
115-
2 weeks
116-
117-
julia> round(Dates.Minute(44), Dates.Minute(15))
118-
45 minutes
119-
120-
julia> round(Dates.Hour(36), Dates.Day)
121-
3 days
122-
```
123-
124-
Valid rounding modes for `round(::Period, ::T, ::RoundingMode)` are `RoundNearestTiesUp`
125-
(default), `RoundDown` (`floor`), and `RoundUp` (`ceil`).
126-
127-
Rounding to a `precision` of `Month`s or `Year`s is not supported, as these `Period`s are of
128-
inconsistent length.
129-
"""
130-
function Base.round(x::Compat.ConvertiblePeriod, precision::Compat.ConvertiblePeriod, r::RoundingMode{:NearestTiesUp})
131-
f, c = floorceil(x, precision)
132-
_x, _f, _c = promote(x, f, c)
133-
return (_x - _f) < (_c - _x) ? f : c
134-
end
135-
136-
Base.round(x::Compat.TimeTypeOrPeriod, p::Compat.Dates.Period, r::RoundingMode{:Down}) = Base.floor(x, p)
137-
Base.round(x::Compat.TimeTypeOrPeriod, p::Compat.Dates.Period, r::RoundingMode{:Up}) = Base.ceil(x, p)
138-
139-
Base.round(::Compat.TimeTypeOrPeriod, p::Compat.Dates.Period, ::RoundingMode) = throw(DomainError(p))
140-
Base.round(x::Compat.TimeTypeOrPeriod, p::Compat.Dates.Period) = Base.round(x, p, RoundNearestTiesUp)
141-
Base.floor(x::Compat.TimeTypeOrPeriod, ::Type{P}) where P <: Compat.Dates.Period = Base.floor(x, oneunit(P))
142-
Base.ceil(x::Compat.TimeTypeOrPeriod, ::Type{P}) where P <: Compat.Dates.Period = Base.ceil(x, oneunit(P))
143-
function Base.round(x::Compat.TimeTypeOrPeriod, ::Type{P}, r::RoundingMode=RoundNearestTiesUp) where P <: Compat.Dates.Period
144-
return Base.round(x, oneunit(P), r)
145-
end
146-
end
147-
14829
if VERSION < v"0.7.0-DEV.3216"
14930
const AbstractDateTime = Compat.Dates.TimeType
15031
else

test/old.jl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,76 @@ end
321321
@test isa(Base.rtoldefault(1.0, Float64, 0), Float64)
322322
@test isa(Base.rtoldefault(Float64, Float64, 0), Float64)
323323
@test Base.rtoldefault(Float64, Float64, 1.0) === 0.0
324+
325+
# 0.7.0-DEV.2402
326+
327+
x = Compat.Dates.Second(172799)
328+
@test floor(x, Compat.Dates.Week) == Compat.Dates.Week(0)
329+
@test floor(x, Compat.Dates.Day) == Compat.Dates.Day(1)
330+
@test floor(x, Compat.Dates.Hour) == Compat.Dates.Hour(47)
331+
@test floor(x, Compat.Dates.Minute) == Compat.Dates.Minute(2879)
332+
@test floor(x, Compat.Dates.Second) == Compat.Dates.Second(172799)
333+
@test floor(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(172799000)
334+
@test ceil(x, Compat.Dates.Week) == Compat.Dates.Week(1)
335+
@test ceil(x, Compat.Dates.Day) == Compat.Dates.Day(2)
336+
@test ceil(x, Compat.Dates.Hour) == Compat.Dates.Hour(48)
337+
@test ceil(x, Compat.Dates.Minute) == Compat.Dates.Minute(2880)
338+
@test ceil(x, Compat.Dates.Second) == Compat.Dates.Second(172799)
339+
@test ceil(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(172799000)
340+
@test round(x, Compat.Dates.Week) == Compat.Dates.Week(0)
341+
@test round(x, Compat.Dates.Day) == Compat.Dates.Day(2)
342+
@test round(x, Compat.Dates.Hour) == Compat.Dates.Hour(48)
343+
@test round(x, Compat.Dates.Minute) == Compat.Dates.Minute(2880)
344+
@test round(x, Compat.Dates.Second) == Compat.Dates.Second(172799)
345+
@test round(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(172799000)
346+
347+
x = Compat.Dates.Nanosecond(2000999999)
348+
@test floor(x, Compat.Dates.Second) == Compat.Dates.Second(2)
349+
@test floor(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(2000)
350+
@test floor(x, Compat.Dates.Microsecond) == Compat.Dates.Microsecond(2000999)
351+
@test floor(x, Compat.Dates.Nanosecond) == x
352+
@test ceil(x, Compat.Dates.Second) == Compat.Dates.Second(3)
353+
@test ceil(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(2001)
354+
@test ceil(x, Compat.Dates.Microsecond) == Compat.Dates.Microsecond(2001000)
355+
@test ceil(x, Compat.Dates.Nanosecond) == x
356+
@test round(x, Compat.Dates.Second) == Compat.Dates.Second(2)
357+
@test round(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(2001)
358+
@test round(x, Compat.Dates.Microsecond) == Compat.Dates.Microsecond(2001000)
359+
@test round(x, Compat.Dates.Nanosecond) == x
360+
361+
362+
for x in [Compat.Dates.Week(3), Compat.Dates.Day(14), Compat.Dates.Second(604800)]
363+
local x
364+
for p in [Compat.Dates.Week, Compat.Dates.Day, Compat.Dates.Hour, Compat.Dates.Second, Compat.Dates.Millisecond, Compat.Dates.Microsecond, Compat.Dates.Nanosecond]
365+
local p
366+
@test floor(x, p) == p(x)
367+
@test ceil(x, p) == p(x)
368+
end
369+
end
370+
371+
x = Compat.Dates.Hour(36)
372+
@test round(x, Compat.Dates.Day, RoundNearestTiesUp) == Compat.Dates.Day(2)
373+
@test round(x, Compat.Dates.Day, RoundUp) == Compat.Dates.Day(2)
374+
@test round(x, Compat.Dates.Day, RoundDown) == Compat.Dates.Day(1)
375+
@test_throws DomainError round(x, Compat.Dates.Day, RoundNearest)
376+
@test_throws DomainError round(x, Compat.Dates.Day, RoundNearestTiesAway)
377+
@test_throws DomainError round(x, Compat.Dates.Day, RoundToZero)
378+
@test round(x, Compat.Dates.Day) == round(x, Compat.Dates.Day, RoundNearestTiesUp)
379+
380+
x = Compat.Dates.Hour(86399)
381+
for p in [Compat.Dates.Week, Compat.Dates.Day, Compat.Dates.Hour, Compat.Dates.Second, Compat.Dates.Millisecond, Compat.Dates.Microsecond, Compat.Dates.Nanosecond]
382+
local p
383+
for v in [-1, 0]
384+
@test_throws DomainError floor(x, p(v))
385+
@test_throws DomainError ceil(x, p(v))
386+
@test_throws DomainError round(x, p(v))
387+
end
388+
end
389+
for p in [Compat.Dates.Year, Compat.Dates.Month]
390+
local p
391+
for v in [-1, 0, 1]
392+
@test_throws MethodError floor(x, p(v))
393+
@test_throws MethodError ceil(x, p(v))
394+
@test_throws DomainError round(x, p(v))
395+
end
396+
end

test/runtests.jl

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -275,79 +275,6 @@ end
275275
@test Compat.AbstractDateTime <: Compat.Dates.TimeType
276276
@test Compat.Dates.DateTime <: Compat.AbstractDateTime
277277

278-
# 0.7.0-DEV.2402
279-
280-
x = Compat.Dates.Second(172799)
281-
@test floor(x, Compat.Dates.Week) == Compat.Dates.Week(0)
282-
@test floor(x, Compat.Dates.Day) == Compat.Dates.Day(1)
283-
@test floor(x, Compat.Dates.Hour) == Compat.Dates.Hour(47)
284-
@test floor(x, Compat.Dates.Minute) == Compat.Dates.Minute(2879)
285-
@test floor(x, Compat.Dates.Second) == Compat.Dates.Second(172799)
286-
@test floor(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(172799000)
287-
@test ceil(x, Compat.Dates.Week) == Compat.Dates.Week(1)
288-
@test ceil(x, Compat.Dates.Day) == Compat.Dates.Day(2)
289-
@test ceil(x, Compat.Dates.Hour) == Compat.Dates.Hour(48)
290-
@test ceil(x, Compat.Dates.Minute) == Compat.Dates.Minute(2880)
291-
@test ceil(x, Compat.Dates.Second) == Compat.Dates.Second(172799)
292-
@test ceil(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(172799000)
293-
@test round(x, Compat.Dates.Week) == Compat.Dates.Week(0)
294-
@test round(x, Compat.Dates.Day) == Compat.Dates.Day(2)
295-
@test round(x, Compat.Dates.Hour) == Compat.Dates.Hour(48)
296-
@test round(x, Compat.Dates.Minute) == Compat.Dates.Minute(2880)
297-
@test round(x, Compat.Dates.Second) == Compat.Dates.Second(172799)
298-
@test round(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(172799000)
299-
300-
x = Compat.Dates.Nanosecond(2000999999)
301-
@test floor(x, Compat.Dates.Second) == Compat.Dates.Second(2)
302-
@test floor(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(2000)
303-
@test floor(x, Compat.Dates.Microsecond) == Compat.Dates.Microsecond(2000999)
304-
@test floor(x, Compat.Dates.Nanosecond) == x
305-
@test ceil(x, Compat.Dates.Second) == Compat.Dates.Second(3)
306-
@test ceil(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(2001)
307-
@test ceil(x, Compat.Dates.Microsecond) == Compat.Dates.Microsecond(2001000)
308-
@test ceil(x, Compat.Dates.Nanosecond) == x
309-
@test round(x, Compat.Dates.Second) == Compat.Dates.Second(2)
310-
@test round(x, Compat.Dates.Millisecond) == Compat.Dates.Millisecond(2001)
311-
@test round(x, Compat.Dates.Microsecond) == Compat.Dates.Microsecond(2001000)
312-
@test round(x, Compat.Dates.Nanosecond) == x
313-
314-
315-
for x in [Compat.Dates.Week(3), Compat.Dates.Day(14), Compat.Dates.Second(604800)]
316-
local x
317-
for p in [Compat.Dates.Week, Compat.Dates.Day, Compat.Dates.Hour, Compat.Dates.Second, Compat.Dates.Millisecond, Compat.Dates.Microsecond, Compat.Dates.Nanosecond]
318-
local p
319-
@test floor(x, p) == p(x)
320-
@test ceil(x, p) == p(x)
321-
end
322-
end
323-
324-
x = Compat.Dates.Hour(36)
325-
@test round(x, Compat.Dates.Day, RoundNearestTiesUp) == Compat.Dates.Day(2)
326-
@test round(x, Compat.Dates.Day, RoundUp) == Compat.Dates.Day(2)
327-
@test round(x, Compat.Dates.Day, RoundDown) == Compat.Dates.Day(1)
328-
@test_throws DomainError round(x, Compat.Dates.Day, RoundNearest)
329-
@test_throws DomainError round(x, Compat.Dates.Day, RoundNearestTiesAway)
330-
@test_throws DomainError round(x, Compat.Dates.Day, RoundToZero)
331-
@test round(x, Compat.Dates.Day) == round(x, Compat.Dates.Day, RoundNearestTiesUp)
332-
333-
x = Compat.Dates.Hour(86399)
334-
for p in [Compat.Dates.Week, Compat.Dates.Day, Compat.Dates.Hour, Compat.Dates.Second, Compat.Dates.Millisecond, Compat.Dates.Microsecond, Compat.Dates.Nanosecond]
335-
local p
336-
for v in [-1, 0]
337-
@test_throws DomainError floor(x, p(v))
338-
@test_throws DomainError ceil(x, p(v))
339-
@test_throws DomainError round(x, p(v))
340-
end
341-
end
342-
for p in [Compat.Dates.Year, Compat.Dates.Month]
343-
local p
344-
for v in [-1, 0, 1]
345-
@test_throws MethodError floor(x, p(v))
346-
@test_throws MethodError ceil(x, p(v))
347-
@test_throws DomainError round(x, p(v))
348-
end
349-
end
350-
351278
# 0.7.0-DEV.3025
352279
let c = CartesianIndices((1:3, 1:2)), l = LinearIndices((1:3, 1:2))
353280
@test LinearIndices(c) == collect(l)

0 commit comments

Comments
 (0)