Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions stdlib/Dates/src/adjusters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,41 @@ function adjust(df::DateFunction, start, step, limit)
throw(ArgumentError("Adjustment limit reached: $limit iterations"))
end

"""
adjust(df, start[, step, limit]) -> TimeType
adjust(df, start) -> TimeType

Adjusts the date in `start` until the `f::Function` passed using `df` returns `true`.
The optional `step` parameter dictates the change in `start` on every iteration.
If `limit` iterations occur, then an [`ArgumentError`](@ref) is thrown.

The default values for parameters `start` and `limit` are 1 Day and 10,000 respectively.

# Examples
```jldoctest
julia> adjust(date -> month(date) == 10, Date(2022, 1, 1), step=Month(3), limit=10)
2022-10-01

julia> adjust(date -> year(date) == 2025, Date(2022, 1, 1), step=Year(1), limit=4)
2025-01-01

julia> adjust(date -> day(date) == 15, Date(2022, 1, 1), step=Year(1), limit=3)
ERROR: ArgumentError: Adjustment limit reached: 3 iterations
Stacktrace:
[...]

julia> adjust(date -> month(date) == 10, Date(2022, 1, 1))
2022-10-01

julia> adjust(date -> year(date) == 2025, Date(2022, 1, 1))
2025-01-01

julia> adjust(date -> year(date) == 2224, Date(2022, 1, 1))
ERROR: ArgumentError: Adjustment limit reached: 10000 iterations
Stacktrace:
[...]
```
"""
function adjust(func::Function, start; step::Period=Day(1), limit::Int=10000)
return adjust(DateFunction(func, start), start, step, limit)
end
Expand Down
4 changes: 1 addition & 3 deletions stdlib/Dates/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ for file in readlines(joinpath(@__DIR__, "testgroups"))
end

@testset "Docstrings" begin
undoc = Docs.undocumented_names(Dates)
@test_broken isempty(undoc)
@test undoc == [:adjust]
@test isempty(Docs.undocumented_names(Dates))
end

end