Skip to content

Commit c317f95

Browse files
committed
Deprecate negate keyword argument in Dates adjuster API
1 parent 7119419 commit c317f95

File tree

3 files changed

+119
-60
lines changed

3 files changed

+119
-60
lines changed

base/dates/adjusters.jl

Lines changed: 101 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ lastdayofquarter(dt::DateTime) = DateTime(lastdayofquarter(Date(dt)))
127127
immutable DateFunction
128128
f::Function
129129
# validate boolean, single-arg inner constructor
130-
function DateFunction(f::ANY, negate::Bool, dt::TimeType)
130+
function DateFunction(f::ANY, dt::TimeType)
131131
isa(f(dt), Bool) || throw(ArgumentError("Provided function must take a single TimeType argument and return true or false"))
132-
return new(negate ? x -> !f(x)::Bool : f)
132+
return new(f)
133133
end
134134
end
135135
Base.show(io::IO, df::DateFunction) = println(io, df.f)
@@ -143,89 +143,130 @@ function adjust(df::DateFunction, start, step, limit)
143143
throw(ArgumentError("Adjustment limit reached: $limit iterations"))
144144
end
145145

146-
function adjust(func::Function, start; step::Period=Day(1), negate::Bool=false, limit::Int=10000)
147-
return adjust(DateFunction(func, negate, start), start, step, limit)
146+
function adjust(func::Function, start; step::Period=Day(1), negate=nothing, limit::Int=10000)
147+
if negate !== nothing
148+
deprecate_negate(:adjust, "func,start", negate)
149+
negate && (func = !func)
150+
end
151+
return adjust(DateFunction(func, start), start, step, limit)
148152
end
149153

150154
# Constructors using DateFunctions
151155

152156
"""
153-
Date(f::Function, y[, m, d]; step=Day(1), negate=false, limit=10000) -> Date
157+
Date(f::Function, y[, m, d]; step=Day(1), limit=10000) -> Date
154158
155159
Create a `Date` through the adjuster API. The starting point will be constructed from the
156-
provided `y, m, d` arguments, and will be adjusted until `f::Function` returns `true`. The
157-
step size in adjusting can be provided manually through the `step` keyword. If
158-
`negate=true`, then the adjusting will stop when `f::Function` returns `false` instead of
159-
`true`. `limit` provides a limit to the max number of iterations the adjustment API will
160+
provided `y, m, d` arguments, and will be adjusted until `f::Function` returns `true`.
161+
The step size in adjusting can be provided manually through the `step` keyword.
162+
`limit` provides a limit to the max number of iterations the adjustment API will
160163
pursue before throwing an error (given that `f::Function` is never satisfied).
161164
"""
162-
function Date(func::Function, y, m=1, d=1;step::Period=Day(1), negate::Bool=false, limit::Int=10000)
163-
return adjust(DateFunction(func, negate, Date(y, m, d)), Date(y, m, d), step, limit)
165+
function Date(func::Function, y, m=1, d=1;step::Period=Day(1), negate=nothing, limit::Int=10000)
166+
if negate !== nothing
167+
deprecate_negate(:Date, "func,y,m,d", negate)
168+
negate && (func = !func)
169+
end
170+
return adjust(DateFunction(func, Date(y, m, d)), Date(y, m, d), step, limit)
164171
end
165172

166173
"""
167-
DateTime(f::Function, y[, m, d, h, mi, s]; step=Day(1), negate=false, limit=10000) -> DateTime
174+
DateTime(f::Function, y[, m, d, h, mi, s]; step=Day(1), limit=10000) -> DateTime
168175
169176
Create a `DateTime` through the adjuster API. The starting point will be constructed from
170177
the provided `y, m, d...` arguments, and will be adjusted until `f::Function` returns
171-
`true`. The step size in adjusting can be provided manually through the `step` keyword. If
172-
`negate=true`, then the adjusting will stop when `f::Function` returns `false` instead of
173-
`true`. `limit` provides a limit to the max number of iterations the adjustment API will
178+
`true`. The step size in adjusting can be provided manually through the `step` keyword.
179+
`limit` provides a limit to the max number of iterations the adjustment API will
174180
pursue before throwing an error (in the case that `f::Function` is never satisfied).
175181
"""
176182
DateTime(::Function, args...)
177183

178-
function DateTime(func::Function, y, m=1; step::Period=Day(1), negate::Bool=false, limit::Int=10000)
179-
return adjust(DateFunction(func, negate, DateTime(y, m)), DateTime(y, m), step, limit)
184+
function DateTime(func::Function, y, m=1; step::Period=Day(1), negate=nothing, limit::Int=10000)
185+
if negate !== nothing
186+
deprecate_negate(:DateTime, "func,y,m", negate)
187+
negate && (func = !func)
188+
end
189+
return adjust(DateFunction(func, DateTime(y, m)), DateTime(y, m), step, limit)
180190
end
181-
function DateTime(func::Function, y, m, d; step::Period=Hour(1), negate::Bool=false, limit::Int=10000)
182-
return adjust(DateFunction(func, negate, DateTime(y)), DateTime(y, m, d), step, limit)
191+
function DateTime(func::Function, y, m, d; step::Period=Hour(1), negate=nothing, limit::Int=10000)
192+
if negate !== nothing
193+
deprecate_negate(:DateTime, "func,y,m,d", negate)
194+
negate && (func = !func)
195+
end
196+
return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d), step, limit)
183197
end
184-
function DateTime(func::Function, y, m, d, h; step::Period=Minute(1), negate::Bool=false, limit::Int=10000)
185-
return adjust(DateFunction(func, negate, DateTime(y)), DateTime(y, m, d, h), step, limit)
198+
function DateTime(func::Function, y, m, d, h; step::Period=Minute(1), negate=nothing, limit::Int=10000)
199+
if negate !== nothing
200+
deprecate_negate(:DateTime, "func,y,m,d,h", negate)
201+
negate && (func = !func)
202+
end
203+
return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d, h), step, limit)
186204
end
187-
function DateTime(func::Function, y, m, d, h, mi; step::Period=Second(1), negate::Bool=false, limit::Int=10000)
188-
return adjust(DateFunction(func, negate, DateTime(y)), DateTime(y, m, d, h, mi), step, limit)
205+
function DateTime(func::Function, y, m, d, h, mi; step::Period=Second(1), negate=nothing, limit::Int=10000)
206+
if negate !== nothing
207+
deprecate_negate(:DateTime, "func,y,m,d,h,mi", negate)
208+
negate && (func = !func)
209+
end
210+
return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d, h, mi), step, limit)
189211
end
190-
function DateTime(func::Function, y, m, d, h, mi, s; step::Period=Millisecond(1), negate::Bool=false, limit::Int=10000)
191-
return adjust(DateFunction(func, negate, DateTime(y)), DateTime(y, m, d, h, mi, s), step, limit)
212+
function DateTime(func::Function, y, m, d, h, mi, s; step::Period=Millisecond(1), negate=nothing, limit::Int=10000)
213+
if negate !== nothing
214+
deprecate_negate(:DateTime, "func,y,m,d,h,mi,s", negate)
215+
negate && (func = !func)
216+
end
217+
return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d, h, mi, s), step, limit)
192218
end
193219

194220
"""
195-
Time(f::Function, h[, mi, s, ms, us]; step=Second(1), negate=false, limit=10000) -> Time
221+
Time(f::Function, h[, mi, s, ms, us]; step=Second(1), limit=10000) -> Time
196222
197223
Create a `Time` through the adjuster API. The starting point will be constructed from the
198-
provided `h, mi, s, ms, us` arguments, and will be adjusted until `f::Function` returns `true`. The step
199-
size in adjusting can be provided manually through the `step` keyword. If `negate=true`,
200-
then the adjusting will stop when `f::Function` returns `false` instead of `true`. `limit`
224+
provided `h, mi, s, ms, us` arguments, and will be adjusted until `f::Function` returns `true`.
225+
The step size in adjusting can be provided manually through the `step` keyword. `limit`
201226
provides a limit to the max number of iterations the adjustment API will pursue before
202227
throwing an error (in the case that `f::Function` is never satisfied). Note that the default step
203228
will adjust to allow for greater precision for the given arguments; i.e. if hour, minute, and second
204229
arguments are provided, the default step will be `Millisecond(1)` instead of `Second(1)`.
205230
"""
206231
Time(::Function, args...)
207232

208-
function Time(func::Function, h, mi=0; step::Period=Second(1), negate::Bool=false, limit::Int=10000)
209-
return adjust(DateFunction(func, negate, Time(h, mi)), Time(h, mi), step, limit)
233+
function Time(func::Function, h, mi=0; step::Period=Second(1), negate=nothing, limit::Int=10000)
234+
if negate !== nothing
235+
deprecate_negate(:Time, "func,h,mi", negate)
236+
negate && (func = !func)
237+
end
238+
return adjust(DateFunction(func, Time(h, mi)), Time(h, mi), step, limit)
210239
end
211-
function Time(func::Function, h, mi, s; step::Period=Millisecond(1), negate::Bool=false, limit::Int=10000)
212-
return adjust(DateFunction(func, negate, Time(h, mi, s)), Time(h, mi, s), step, limit)
240+
function Time(func::Function, h, mi, s; step::Period=Millisecond(1), negate=nothing, limit::Int=10000)
241+
if negate !== nothing
242+
deprecate_negate(:Time, "func,h,mi,s", negate)
243+
negate && (func = !func)
244+
end
245+
return adjust(DateFunction(func, Time(h, mi, s)), Time(h, mi, s), step, limit)
213246
end
214-
function Time(func::Function, h, mi, s, ms; step::Period=Microsecond(1), negate::Bool=false, limit::Int=10000)
215-
return adjust(DateFunction(func, negate,Time(h, mi, s, ms)),Time(h, mi, s, ms), step, limit)
247+
function Time(func::Function, h, mi, s, ms; step::Period=Microsecond(1), negate=nothing, limit::Int=10000)
248+
if negate !== nothing
249+
deprecate_negate(:Time, "func,h,mi,s,ms", negate)
250+
negate && (func = !func)
251+
end
252+
return adjust(DateFunction(func, Time(h, mi, s, ms)), Time(h, mi, s, ms), step, limit)
216253
end
217-
function Time(func::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), negate::Bool=false, limit::Int=10000)
218-
return adjust(DateFunction(func, negate, Time(h, mi, s, ms, us)), Time(h, mi, s, ms, us), step, limit)
254+
function Time(func::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), negate=nothing, limit::Int=10000)
255+
if negate !== nothing
256+
deprecate_negate(:Time, "func,h,mi,s,ms,us", negate)
257+
negate && (func = !func)
258+
end
259+
return adjust(DateFunction(func, Time(h, mi, s, ms, us)), Time(h, mi, s, ms, us), step, limit)
219260
end
220261

221262
# Return the next TimeType that falls on dow
222-
ISDAYOFWEEK = Dict(Mon => DateFunction(ismonday, false, Date(0)),
223-
Tue => DateFunction(istuesday, false, Date(0)),
224-
Wed => DateFunction(iswednesday, false, Date(0)),
225-
Thu => DateFunction(isthursday, false, Date(0)),
226-
Fri => DateFunction(isfriday, false, Date(0)),
227-
Sat => DateFunction(issaturday, false, Date(0)),
228-
Sun => DateFunction(issunday, false, Date(0)))
263+
ISDAYOFWEEK = Dict(Mon => DateFunction(ismonday, Date(0)),
264+
Tue => DateFunction(istuesday, Date(0)),
265+
Wed => DateFunction(iswednesday, Date(0)),
266+
Thu => DateFunction(isthursday, Date(0)),
267+
Fri => DateFunction(isfriday, Date(0)),
268+
Sat => DateFunction(issaturday, Date(0)),
269+
Sun => DateFunction(issunday, Date(0)))
229270

230271
# "same" indicates whether the current date can be considered or not
231272
"""
@@ -239,15 +280,18 @@ tonext(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same
239280

240281
# Return the next TimeType where func evals true using step in incrementing
241282
"""
242-
tonext(func::Function,dt::TimeType;step=Day(1),negate=false,limit=10000,same=false) -> TimeType
283+
tonext(func::Function,dt::TimeType;step=Day(1),limit=10000,same=false) -> TimeType
243284
244285
Adjusts `dt` by iterating at most `limit` iterations by `step` increments until `func`
245286
returns `true`. `func` must take a single `TimeType` argument and return a `Bool`. `same`
246-
allows `dt` to be considered in satisfying `func`. `negate` will make the adjustment process
247-
terminate when `func` returns `false` instead of `true`.
287+
allows `dt` to be considered in satisfying `func`.
248288
"""
249-
function tonext(func::Function, dt::TimeType;step::Period=Day(1), negate::Bool=false, limit::Int=10000, same::Bool=false)
250-
return adjust(DateFunction(func, negate, dt), same ? dt : dt+step, step, limit)
289+
function tonext(func::Function, dt::TimeType;step::Period=Day(1), negate=nothing, limit::Int=10000, same::Bool=false)
290+
if negate !== nothing
291+
deprecate_negate(:tonext, "func,dt", negate)
292+
negate && (func = !func)
293+
end
294+
return adjust(DateFunction(func, dt), same ? dt : dt+step, step, limit)
251295
end
252296

253297
"""
@@ -260,15 +304,18 @@ Tuesday, etc`. Setting `same=true` allows the current `dt` to be considered as t
260304
toprev(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same ? dt : dt+Day(-1), Day(-1), 7)
261305

262306
"""
263-
toprev(func::Function,dt::TimeType;step=Day(-1),negate=false,limit=10000,same=false) -> TimeType
307+
toprev(func::Function,dt::TimeType;step=Day(-1),limit=10000,same=false) -> TimeType
264308
265309
Adjusts `dt` by iterating at most `limit` iterations by `step` increments until `func`
266310
returns `true`. `func` must take a single `TimeType` argument and return a `Bool`. `same`
267-
allows `dt` to be considered in satisfying `func`. `negate` will make the adjustment process
268-
terminate when `func` returns `false` instead of `true`.
311+
allows `dt` to be considered in satisfying `func`.
269312
"""
270-
function toprev(func::Function, dt::TimeType; step::Period=Day(-1), negate::Bool=false, limit::Int=10000, same::Bool=false)
271-
return adjust(DateFunction(func, negate, dt), same ? dt : dt+step, step, limit)
313+
function toprev(func::Function, dt::TimeType; step::Period=Day(-1), negate=nothing, limit::Int=10000, same::Bool=false)
314+
if negate !== nothing
315+
deprecate_negate(:toprev, "func,dt", negate)
316+
negate && (func = !func)
317+
end
318+
return adjust(DateFunction(func, dt), same ? dt : dt+step, step, limit)
272319
end
273320

274321
# Return the first TimeType that falls on dow in the Month or Year

base/deprecated.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,11 +1766,22 @@ end
17661766
# Not exported
17671767
eval(LibGit2, quote
17681768
function owner(x)
1769-
depwarn("owner(x) is deprecated, use repository(x) instead.", :owner)
1769+
Base.depwarn("owner(x) is deprecated, use repository(x) instead.", :owner)
17701770
repository(x)
17711771
end
17721772
end)
17731773

17741774
@deprecate EachLine(stream, ondone) EachLine(stream, ondone=ondone)
17751775

1776+
# when this deprecation is deleted, remove all calls to it, and all
1777+
# negate=nothing keyword arguments, from base/dates/adjusters.jl
1778+
eval(Dates, quote
1779+
function deprecate_negate(f, sig, negate)
1780+
msg = "$f($sig; negate=$negate) is deprecated, use $f("
1781+
negate && (msg *= "!")
1782+
msg *= "$sig) instead."
1783+
Base.depwarn(msg, f)
1784+
end
1785+
end)
1786+
17761787
# End deprecations scheduled for 0.6

test/dates/adjusters.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ dt = Dates.Date(2014,5,21)
229229

230230
@test Dates.tonext(Dates.Date(0),Dates.Mon) == Dates.Date(0,1,3)
231231

232-
#test func, diff steps, negate, same
232+
#test func, diff steps, same
233233
@test Dates.tonext(Dates.iswednesday,dt) == Dates.Date(2014,5,28)
234234
@test Dates.tonext(Dates.iswednesday,dt;same=true) == dt
235235
@test Dates.tonext(Dates.isthursday,dt) == Dates.Date(2014,5,22)
@@ -240,7 +240,8 @@ dt = Dates.Date(2014,5,21)
240240
@test Dates.tonext(Dates.istuesday,dt) == Dates.Date(2014,5,27)
241241
@test Dates.tonext(Dates.ismonday,Dates.Date(0)) == Dates.Date(0,1,3)
242242

243-
@test Dates.tonext(x->!Dates.iswednesday(x),dt;negate=true) == Dates.Date(2014,5,28)
243+
@test Dates.tonext(!Dates.iswednesday,dt) == Dates.Date(2014,5,22)
244+
@test Dates.tonext(!Dates.isthursday,dt) == Dates.Date(2014,5,23)
244245
# Reach adjust limit
245246
@test_throws ArgumentError Dates.tonext(Dates.iswednesday,dt;limit=6)
246247

@@ -322,8 +323,8 @@ Januarymondays2014 = [Dates.Date(2014,1,6),Dates.Date(2014,1,13),Dates.Date(2014
322323
@test filter(Dates.ismonday,startdate:stopdate) == Januarymondays2014
323324

324325
@test_throws MethodError filter((x,y)->x+y,Dates.Date(2013):Dates.Date(2014))
325-
@test_throws MethodError Dates.DateFunction((x,y)->x+y, false, Date(0))
326-
@test_throws ArgumentError Dates.DateFunction((dt)->2, false, Date(0))
326+
@test_throws MethodError Dates.DateFunction((x,y)->x+y, Date(0))
327+
@test_throws ArgumentError Dates.DateFunction((dt)->2, Date(0))
327328
@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,2))) == 32
328329
@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,1))) == 1
329330
@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,2))) == 2
@@ -469,4 +470,4 @@ r = Dates.Time(x->Dates.second(x) == 5, 1)
469470
r = filter(x->Dates.second(x) == 5, Dates.Time(0):Dates.Time(10))
470471
@test length(r) == 600
471472
@test first(r) == Dates.Time(0,0,5)
472-
@test last(r) == Dates.Time(9,59,5)
473+
@test last(r) == Dates.Time(9,59,5)

0 commit comments

Comments
 (0)