This is tricky. It seems like we could use a nice utility for this. The best I could do with builtins was
julia> Iterators.takewhile(!=(Dates.Time(00,01,00) + Dates.Minute(1)),
Iterators.countfrom(Dates.Time(23,59,00), Dates.Minute(1))) |> collect
3-element Vector{Time}:
23:59:00
00:00:00
00:01:00
But this is obviously very brittle. It relies on the endpoint being hit exactly and has to add 1 to it so that it doesn’t stop one too early.
If you knew how many steps you wanted, rather than the stop point, this would be easier
julia> Iterators.take(Iterators.countfrom(Dates.Time(23,59,00), Dates.Minute(1)), 3) |> collect
3-element Vector{Time}:
23:59:00
00:00:00
00:01:00
One can always implement their own iterator for this. Here’s what I threw together. It covers this basic case but perhaps not everything you need. It probably has some strange behavior in some corner cases that you may need to fix (for example, increments larger than 1 Day will probably cause trouble).
struct TimeIterator{I<:Dates.TimePeriod}
start::Dates.Time
stop::Dates.Time
step::I
end
Base.IteratorSize(::Type{<:TimeIterator}) = Base.SizeUnknown()
Base.eltype(::Type{<:TimeIterator}) = Dates.Time
function Base.iterate(t::TimeIterator, state=(t.start, t.start <= t.stop, false))
t.step > zero(t.step) || throw(DomainError(t.step, "needs extra work to support nonpositive steps")) # TODO: should check in constructor instead
tnow, homestretch, stopnow = state
if stopnow || (homestretch && !(tnow <= t.stop)) # time to stop
return nothing
end
tnext = tnow + t.step
stopnext = false
if tnext < tnow # detect when the time wraps
if homestretch # we were already on the home stretch -- don't let us wrap again!
stopnext = true
end
homestretch = true # use inequality-based stopping condition now
end
return (tnow, (tnext, homestretch, stopnext))
end
julia> TimeIterator(Dates.Time(23,57,00), Dates.Time(23,59,00), Dates.Minute(1)) |> collect
3-element Vector{Time}:
23:57:00
23:58:00
23:59:00
julia> TimeIterator(Dates.Time(23,57,00), Dates.Time(00,01,00), Dates.Minute(1)) |> collect
5-element Vector{Time}:
23:57:00
23:58:00
23:59:00
00:00:00
00:01:00