• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37552

pending completion
#37552

push

local

web-flow
Abbreviate varinfo signature and re-order for consistency (#48860)

1 of 1 new or added line in 1 file covered. (100.0%)

72746 of 83846 relevant lines covered (86.76%)

34617131.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.95
/stdlib/Dates/src/adjusters.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
### truncation
4
Base.trunc(dt::Date, p::Type{Year}) = Date(UTD(totaldays(year(dt), 1, 1)))
×
5
Base.trunc(dt::Date, p::Type{Quarter}) = firstdayofquarter(dt)
×
6
Base.trunc(dt::Date, p::Type{Month}) = firstdayofmonth(dt)
×
7
Base.trunc(dt::Date, p::Type{Day}) = dt
×
8

9
Base.trunc(dt::DateTime, p::Type{Year}) = DateTime(trunc(Date(dt), Year))
×
10
Base.trunc(dt::DateTime, p::Type{Quarter}) = DateTime(trunc(Date(dt), Quarter))
×
11
Base.trunc(dt::DateTime, p::Type{Month}) = DateTime(trunc(Date(dt), Month))
×
12
Base.trunc(dt::DateTime, p::Type{Day}) = DateTime(Date(dt))
×
13
Base.trunc(dt::DateTime, p::Type{Hour}) = dt - Minute(dt) - Second(dt) - Millisecond(dt)
×
14
Base.trunc(dt::DateTime, p::Type{Minute}) = dt - Second(dt) - Millisecond(dt)
×
15
Base.trunc(dt::DateTime, p::Type{Second}) = dt - Millisecond(dt)
×
16
Base.trunc(dt::DateTime, p::Type{Millisecond}) = dt
×
17

18
Base.trunc(t::Time, p::Type{Hour}) = Time(Hour(t))
×
19
Base.trunc(t::Time, p::Type{Minute}) = Time(Hour(t), Minute(t))
×
20
Base.trunc(t::Time, p::Type{Second}) = Time(Hour(t), Minute(t), Second(t))
×
21
Base.trunc(t::Time, p::Type{Millisecond}) = t - Microsecond(t) - Nanosecond(t)
×
22
Base.trunc(t::Time, p::Type{Microsecond}) = t - Nanosecond(t)
×
23
Base.trunc(t::Time, p::Type{Nanosecond})  = t
×
24

25
"""
26
    trunc(dt::TimeType, ::Type{Period}) -> TimeType
27

28
Truncates the value of `dt` according to the provided `Period` type.
29

30
# Examples
31
```jldoctest
32
julia> trunc(DateTime("1996-01-01T12:30:00"), Day)
33
1996-01-01T00:00:00
34
```
35
"""
36
Dates.trunc(::Dates.TimeType, ::Type{Dates.Period})
37

38
# Adjusters
39
"""
40
    firstdayofweek(dt::TimeType) -> TimeType
41

42
Adjusts `dt` to the Monday of its week.
43

44
# Examples
45
```jldoctest
46
julia> firstdayofweek(DateTime("1996-01-05T12:30:00"))
47
1996-01-01T00:00:00
48
```
49
"""
50
function firstdayofweek end
51

52
firstdayofweek(dt::Date) = Date(UTD(value(dt) - dayofweek(dt) + 1))
×
53
firstdayofweek(dt::DateTime) = DateTime(firstdayofweek(Date(dt)))
×
54

55
"""
56
    lastdayofweek(dt::TimeType) -> TimeType
57

58
Adjusts `dt` to the Sunday of its week.
59

60
# Examples
61
```jldoctest
62
julia> lastdayofweek(DateTime("1996-01-05T12:30:00"))
63
1996-01-07T00:00:00
64
```
65
"""
66
function lastdayofweek end
67

68
lastdayofweek(dt::Date) = Date(UTD(value(dt) + (7 - dayofweek(dt))))
×
69
lastdayofweek(dt::DateTime) = DateTime(lastdayofweek(Date(dt)))
×
70

71
"""
72
    firstdayofmonth(dt::TimeType) -> TimeType
73

74
Adjusts `dt` to the first day of its month.
75

76
# Examples
77
```jldoctest
78
julia> firstdayofmonth(DateTime("1996-05-20"))
79
1996-05-01T00:00:00
80
```
81
"""
82
function firstdayofmonth end
83

84
firstdayofmonth(dt::Date) = Date(UTD(value(dt) - day(dt) + 1))
×
85
firstdayofmonth(dt::DateTime) = DateTime(firstdayofmonth(Date(dt)))
×
86

87
"""
88
    lastdayofmonth(dt::TimeType) -> TimeType
89

90
Adjusts `dt` to the last day of its month.
91

92
# Examples
93
```jldoctest
94
julia> lastdayofmonth(DateTime("1996-05-20"))
95
1996-05-31T00:00:00
96
```
97
"""
98
function lastdayofmonth end
99

100
function lastdayofmonth(dt::Date)
×
101
    y, m, d = yearmonthday(dt)
×
102
    return Date(UTD(value(dt) + daysinmonth(y, m) - d))
×
103
end
104
lastdayofmonth(dt::DateTime) = DateTime(lastdayofmonth(Date(dt)))
×
105

106
"""
107
    firstdayofyear(dt::TimeType) -> TimeType
108

109
Adjusts `dt` to the first day of its year.
110

111
# Examples
112
```jldoctest
113
julia> firstdayofyear(DateTime("1996-05-20"))
114
1996-01-01T00:00:00
115
```
116
"""
117
function firstdayofyear end
118

119
firstdayofyear(dt::Date) = Date(UTD(value(dt) - dayofyear(dt) + 1))
×
120
firstdayofyear(dt::DateTime) = DateTime(firstdayofyear(Date(dt)))
×
121

122
"""
123
    lastdayofyear(dt::TimeType) -> TimeType
124

125
Adjusts `dt` to the last day of its year.
126

127
# Examples
128
```jldoctest
129
julia> lastdayofyear(DateTime("1996-05-20"))
130
1996-12-31T00:00:00
131
```
132
"""
133
function lastdayofyear end
134

135
function lastdayofyear(dt::Date)
×
136
    y, m, d = yearmonthday(dt)
×
137
    return Date(UTD(value(dt) + daysinyear(y) - dayofyear(y, m, d)))
×
138
end
139
lastdayofyear(dt::DateTime) = DateTime(lastdayofyear(Date(dt)))
×
140

141
"""
142
    firstdayofquarter(dt::TimeType) -> TimeType
143

144
Adjusts `dt` to the first day of its quarter.
145

146
# Examples
147
```jldoctest
148
julia> firstdayofquarter(DateTime("1996-05-20"))
149
1996-04-01T00:00:00
150

151
julia> firstdayofquarter(DateTime("1996-08-20"))
152
1996-07-01T00:00:00
153
```
154
"""
155
function firstdayofquarter end
156

157
function firstdayofquarter(dt::Date)
×
158
    y,m = yearmonth(dt)
×
159
    mm = m < 4 ? 1 : m < 7 ? 4 : m < 10 ? 7 : 10
×
160
    return Date(y, mm, 1)
×
161
end
162
firstdayofquarter(dt::DateTime) = DateTime(firstdayofquarter(Date(dt)))
×
163

164
"""
165
    lastdayofquarter(dt::TimeType) -> TimeType
166

167
Adjusts `dt` to the last day of its quarter.
168

169
# Examples
170
```jldoctest
171
julia> lastdayofquarter(DateTime("1996-05-20"))
172
1996-06-30T00:00:00
173

174
julia> lastdayofquarter(DateTime("1996-08-20"))
175
1996-09-30T00:00:00
176
```
177
"""
178
function lastdayofquarter end
179

180
function lastdayofquarter(dt::Date)
×
181
    y,m = yearmonth(dt)
×
182
    mm, d = m < 4 ? (3, 31) : m < 7 ? (6, 30) : m < 10 ? (9, 30) : (12, 31)
×
183
    return Date(y, mm, d)
×
184
end
185
lastdayofquarter(dt::DateTime) = DateTime(lastdayofquarter(Date(dt)))
×
186

187
# Temporal Adjusters
188
struct DateFunction
189
    f::Function
190
    # validate boolean, single-arg inner constructor
191
    function DateFunction(@nospecialize(f), dt::TimeType)
×
192
        isa(f(dt), Bool) || throw(ArgumentError("Provided function must take a single TimeType argument and return true or false"))
×
193
        return new(f)
×
194
    end
195
end
196
Base.show(io::IO, df::DateFunction) = println(io, df.f)
×
197

198
# Core adjuster
199
function adjust(df::DateFunction, start, step, limit)
2✔
200
    for i = 1:limit
4✔
201
        df.f(start) && return start
7✔
202
        start += step
5✔
203
    end
10✔
204
    throw(ArgumentError("Adjustment limit reached: $limit iterations"))
×
205
end
206

207
function adjust(func::Function, start; step::Period=Day(1), limit::Int=10000)
×
208
    return adjust(DateFunction(func, start), start, step, limit)
×
209
end
210

211
# Constructors using DateFunctions
212

213
"""
214
    Date(f::Function, y[, m, d]; step=Day(1), limit=10000) -> Date
215

216
Create a `Date` through the adjuster API. The starting point will be constructed from the
217
provided `y, m, d` arguments, and will be adjusted until `f::Function` returns `true`.
218
The step size in adjusting can be provided manually through the `step` keyword.
219
`limit` provides a limit to the max number of iterations the adjustment API will
220
pursue before throwing an error (given that `f::Function` is never satisfied).
221

222
# Examples
223
```jldoctest
224
julia> Date(date -> week(date) == 20, 2010, 01, 01)
225
2010-05-17
226

227
julia> Date(date -> year(date) == 2010, 2000, 01, 01)
228
2010-01-01
229

230
julia> Date(date -> month(date) == 10, 2000, 01, 01; limit = 5)
231
ERROR: ArgumentError: Adjustment limit reached: 5 iterations
232
Stacktrace:
233
[...]
234
```
235
"""
236
function Date(func::Function, y, m=1, d=1; step::Period=Day(1), limit::Int=10000)
×
237
    return adjust(DateFunction(func, Date(y, m, d)), Date(y, m, d), step, limit)
×
238
end
239

240
"""
241
    DateTime(f::Function, y[, m, d, h, mi, s]; step=Day(1), limit=10000) -> DateTime
242

243
Create a `DateTime` through the adjuster API. The starting point will be constructed from
244
the provided `y, m, d...` arguments, and will be adjusted until `f::Function` returns
245
`true`. The step size in adjusting can be provided manually through the `step` keyword.
246
`limit` provides a limit to the max number of iterations the adjustment API will
247
pursue before throwing an error (in the case that `f::Function` is never satisfied).
248

249
# Examples
250
```jldoctest
251
julia> DateTime(dt -> second(dt) == 40, 2010, 10, 20, 10; step = Second(1))
252
2010-10-20T10:00:40
253

254
julia> DateTime(dt -> hour(dt) == 20, 2010, 10, 20, 10; step = Hour(1), limit = 5)
255
ERROR: ArgumentError: Adjustment limit reached: 5 iterations
256
Stacktrace:
257
[...]
258
```
259
"""
260
DateTime(::Function, args...)
261

262
function DateTime(func::Function, y, m=1; step::Period=Day(1), limit::Int=10000)
×
263
    return adjust(DateFunction(func, DateTime(y, m)), DateTime(y, m), step, limit)
×
264
end
265
function DateTime(func::Function, y, m, d; step::Period=Hour(1), limit::Int=10000)
×
266
    return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d), step, limit)
×
267
end
268
function DateTime(func::Function, y, m, d, h; step::Period=Minute(1), limit::Int=10000)
×
269
    return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d, h), step, limit)
×
270
end
271
function DateTime(func::Function, y, m, d, h, mi; step::Period=Second(1), limit::Int=10000)
×
272
    return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d, h, mi), step, limit)
×
273
end
274
function DateTime(func::Function, y, m, d, h, mi, s; step::Period=Millisecond(1), limit::Int=10000)
×
275
    return adjust(DateFunction(func, DateTime(y)), DateTime(y, m, d, h, mi, s), step, limit)
×
276
end
277

278
"""
279
    Time(f::Function, h, mi=0; step::Period=Second(1), limit::Int=10000)
280
    Time(f::Function, h, mi, s; step::Period=Millisecond(1), limit::Int=10000)
281
    Time(f::Function, h, mi, s, ms; step::Period=Microsecond(1), limit::Int=10000)
282
    Time(f::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), limit::Int=10000)
283

284
Create a `Time` through the adjuster API. The starting point will be constructed from the
285
provided `h, mi, s, ms, us` arguments, and will be adjusted until `f::Function` returns `true`.
286
The step size in adjusting can be provided manually through the `step` keyword. `limit`
287
provides a limit to the max number of iterations the adjustment API will pursue before
288
throwing an error (in the case that `f::Function` is never satisfied). Note that the default step
289
will adjust to allow for greater precision for the given arguments; i.e. if hour, minute, and second
290
arguments are provided, the default step will be `Millisecond(1)` instead of `Second(1)`.
291

292
# Examples
293
```jldoctest
294
julia> Time(t -> minute(t) == 30, 20)
295
20:30:00
296

297
julia> Time(t -> minute(t) == 0, 20)
298
20:00:00
299

300
julia> Time(t -> hour(t) == 10, 3; limit = 5)
301
ERROR: ArgumentError: Adjustment limit reached: 5 iterations
302
Stacktrace:
303
[...]
304
```
305
"""
306
Time(::Function, args...)
307

308
function Time(func::Function, h, mi=0; step::Period=Second(1), limit::Int=10000)
×
309
    return adjust(DateFunction(func, Time(h, mi)), Time(h, mi), step, limit)
×
310
end
311
function Time(func::Function, h, mi, s; step::Period=Millisecond(1), limit::Int=10000)
×
312
    return adjust(DateFunction(func, Time(h, mi, s)), Time(h, mi, s), step, limit)
×
313
end
314
function Time(func::Function, h, mi, s, ms; step::Period=Microsecond(1), limit::Int=10000)
×
315
    return adjust(DateFunction(func, Time(h, mi, s, ms)), Time(h, mi, s, ms), step, limit)
×
316
end
317
function Time(func::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), limit::Int=10000)
×
318
    return adjust(DateFunction(func, Time(h, mi, s, ms, us)), Time(h, mi, s, ms, us), step, limit)
×
319
end
320

321
# Return the next TimeType that falls on dow
322
ISDAYOFWEEK = Dict(Mon => DateFunction(ismonday, Date(0)),
323
                   Tue => DateFunction(istuesday, Date(0)),
324
                   Wed => DateFunction(iswednesday, Date(0)),
325
                   Thu => DateFunction(isthursday, Date(0)),
326
                   Fri => DateFunction(isfriday, Date(0)),
327
                   Sat => DateFunction(issaturday, Date(0)),
328
                   Sun => DateFunction(issunday, Date(0)))
329

330
# "same" indicates whether the current date can be considered or not
331
"""
332
    tonext(dt::TimeType, dow::Int; same::Bool=false) -> TimeType
333

334
Adjusts `dt` to the next day of week corresponding to `dow` with `1 = Monday, 2 = Tuesday,
335
etc`. Setting `same=true` allows the current `dt` to be considered as the next `dow`,
336
allowing for no adjustment to occur.
337
"""
338
tonext(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same ? dt : dt + Day(1), Day(1), 7)
2✔
339

340
# Return the next TimeType where func evals true using step in incrementing
341
"""
342
    tonext(func::Function, dt::TimeType; step=Day(1), limit=10000, same=false) -> TimeType
343

344
Adjusts `dt` by iterating at most `limit` iterations by `step` increments until `func`
345
returns `true`. `func` must take a single `TimeType` argument and return a [`Bool`](@ref).
346
`same` allows `dt` to be considered in satisfying `func`.
347
"""
348
function tonext(func::Function, dt::TimeType; step::Period=Day(1), limit::Int=10000, same::Bool=false)
×
349
    return adjust(DateFunction(func, dt), same ? dt : dt + step, step, limit)
×
350
end
351

352
"""
353
    toprev(dt::TimeType, dow::Int; same::Bool=false) -> TimeType
354

355
Adjusts `dt` to the previous day of week corresponding to `dow` with `1 = Monday, 2 =
356
Tuesday, etc`. Setting `same=true` allows the current `dt` to be considered as the previous
357
`dow`, allowing for no adjustment to occur.
358
"""
359
toprev(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same ? dt : dt + Day(-1), Day(-1), 7)
2✔
360

361
"""
362
    toprev(func::Function, dt::TimeType; step=Day(-1), limit=10000, same=false) -> TimeType
363

364
Adjusts `dt` by iterating at most `limit` iterations by `step` increments until `func`
365
returns `true`. `func` must take a single `TimeType` argument and return a [`Bool`](@ref).
366
`same` allows `dt` to be considered in satisfying `func`.
367
"""
368
function toprev(func::Function, dt::TimeType; step::Period=Day(-1), limit::Int=10000, same::Bool=false)
×
369
    return adjust(DateFunction(func, dt), same ? dt : dt + step, step, limit)
×
370
end
371

372
# Return the first TimeType that falls on dow in the Month or Year
373
"""
374
    tofirst(dt::TimeType, dow::Int; of=Month) -> TimeType
375

376
Adjusts `dt` to the first `dow` of its month. Alternatively, `of=Year` will adjust to the
377
first `dow` of the year.
378
"""
379
function tofirst(dt::TimeType, dow::Int; of::Union{Type{Year}, Type{Month}}=Month)
×
380
    dt = of <: Month ? firstdayofmonth(dt) : firstdayofyear(dt)
×
381
    return adjust(ISDAYOFWEEK[dow], dt, Day(1), 366)
×
382
end
383

384
# Return the last TimeType that falls on dow in the Month or Year
385
"""
386
    tolast(dt::TimeType, dow::Int; of=Month) -> TimeType
387

388
Adjusts `dt` to the last `dow` of its month. Alternatively, `of=Year` will adjust to the
389
last `dow` of the year.
390
"""
391
function tolast(dt::TimeType, dow::Int; of::Union{Type{Year}, Type{Month}}=Month)
×
392
    dt = of <: Month ? lastdayofmonth(dt) : lastdayofyear(dt)
×
393
    return adjust(ISDAYOFWEEK[dow], dt, Day(-1), 366)
×
394
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc