Skip to content
2 changes: 2 additions & 0 deletions src/TimeSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ using PrettyTables: PrettyTables, pretty_table
export TimeArray,
AbstractTimeSeries,
when,
time_slots,
TimeWindow,
Comment thread
iblislin marked this conversation as resolved.
Resolved
from,
to,
findwhen,
Expand Down
87 changes: 87 additions & 0 deletions src/split.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,93 @@ function when(ta::TimeArray, period::Function, t::Integer)
end
when(ta::TimeArray, period::Function, t::String) = ta[findall(period.(timestamp(ta)) .== t)]

struct TimeWindow
from::Time
to::Time
end

time_slots(; from::Time, to::Time) = TimeWindow(from, to)

"""
when(ta::TimeArray, window::TimeWindow)

Return a subset of `TimeArray` where the time of each timestamp falls
within the daily window `[window.from, window.to]`.
"""
function when(ta::TimeArray, window::TimeWindow)
return ta[findall(t -> window.from <= Time(t) <= window.to, timestamp(ta))]
end

struct TimeSlot{T<:TimeType,P<:Period}
start::T
stop::T
interval::P
from::Time
to::Time
end

Base.IteratorSize(::Type{<:TimeSlot}) = Base.SizeUnknown()
Base.eltype(::Type{TimeSlot{T,P}}) where {T,P} = T

function Base.iterate(ts::TimeSlot{T}) where {T}
current = ts.start
while current <= ts.stop
if ts.from <= Time(current) <= ts.to
return (current, current + ts.interval)
end
current += ts.interval
Comment thread
anurag-mds marked this conversation as resolved.
Resolved
end
return nothing
end

function Base.iterate(ts::TimeSlot{T}, state::T) where {T}
current = state
while current <= ts.stop
if ts.from <= Time(current) <= ts.to
return (current, current + ts.interval)
end
current += ts.interval
end
return nothing
end

"""
time_slots(start::T, stop::T, interval::Period;
from::Time=Time(0,0), to::Time=Time(23,59))

Return A `TimeSlot` iterator where each timestamp falls within the daily time
window `[from, to]`,
filtered from the full `start` to `stop` range at the given `interval`
"""
function time_slots(
start::T, stop::T, interval::Period; from::Time=Time(0, 0), to::Time=Time(23, 59)
) where {T<:TimeType}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function time_slots(
start::T, stop::T, interval::Period; from::Time=Time(0, 0), to::Time=Time(23, 59)
) where {T<:TimeType}
function time_slots(
start::T, stop::T, interval::P; from::Time=Time(0, 0), to::Time=Time(23, 59)
) where {T<:TimeType,P<:Period}

then, also use those type variables in return?

# Check for negative or zero period
if interval <= zero(interval)
throw(ArgumentError("interval must be positive, got $interval"))
end

return TimeSlot{typeof(start),typeof(interval)}(start, stop, interval, from, to)
end

"""
time_slots(range::StepRange{T};
from::Time=Time(0,0), to::Time=Time(23,59))

Return A `TimeSlot` iterator where each timestamp falls within the daily time
window `[from, to]`,
filtered from the given `range`
"""
function time_slots(
range::StepRange{T}; from::Time=Time(0, 0), to::Time=Time(23, 59)
) where {T}
if step(range) <= zero(step(range))
throw(ArgumentError("interval must be positive, got $(step(range))"))
end
return TimeSlot{eltype(range),typeof(step(range))}(
range.start, range.stop, step(range), from, to
)

@iblislin iblislin Mar 18, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the current behaviour of compiler, is this consider type stable or not, if we use eltype(..) and typeof(..) in return statement. 🤔

type stable is preferred, still fine if not.

end
# from, to ######################

"""
Expand Down
100 changes: 100 additions & 0 deletions test/split.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,104 @@ using TimeSeries
# test empty timearray
@test length(split(to(cl, Date(2000)), week)) == 0
end

@testset "time_slots" begin
@test length(
collect(
time_slots(
DateTime(2026, 1, 1),
DateTime(2026, 1, 3),
Minute(15);
from=Time(8, 45),
to=Time(13, 45),
),
),
) == 42
@test length(
collect(
time_slots(
DateTime(2026, 1, 1),
DateTime(2026, 1, 1),
Minute(15);
from=Time(14, 0),
to=Time(15, 0),
),
),
) == 0
@test length(
collect(
time_slots(
DateTime(2026, 1, 1, 8, 45),
DateTime(2026, 1, 1, 8, 45),
Minute(15);
from=Time(8, 45),
to=Time(8, 45),
),
),
) == 1
@test length(
collect(
time_slots(
DateTime(2026, 1, 1),
DateTime(2026, 1, 1, 23, 59),
Hour(1);
from=Time(9, 0),
to=Time(17, 0),
),
),
) == 9
@test length(
collect(
time_slots(
DateTime(2026, 1, 1),
DateTime(2026, 1, 1, 23, 59),
Hour(1);
from=Time(13, 0),
to=Time(8, 0),
),
),
) == 0
@test length(
collect(
time_slots(
DateTime(2026, 1, 1):Minute(15):DateTime(2026, 1, 3);
from=Time(8, 45),
to=Time(13, 45),
),
),
) == 42
@test length(
when(
TimeArray(DateTime(2026, 1, 1):Minute(15):DateTime(2026, 1, 3), rand(193)),
time_slots(; from=Time(8, 45), to=Time(13, 45)),
),
) == 42
end

# Test for negative Day interval
@test_throws ArgumentError time_slots(
DateTime(2023, 1, 1), DateTime(2023, 1, 2), Day(-1), from=Time(9), to=Time(17)
)
# Test for zero Minute interval
@test_throws ArgumentError time_slots(
DateTime(2023, 1, 1), DateTime(2023, 1, 2), Minute(0)
)

# Test for StepRange overload with negative step
rng = DateTime(2023, 1, 2):Day(-1):DateTime(2023, 1, 1)
@test_throws ArgumentError time_slots(rng)
@testset "TimeSlot performance" begin
big_slots = time_slots(
DateTime(2000, 1, 1),
DateTime(2100, 1, 1),
Minute(1);
from=Time(9, 0),
to=Time(9, 1),
)

@test first(big_slots) == DateTime(2000, 1, 1, 9, 0)

five_slots = collect(Iterators.take(big_slots, 5))
@test length(five_slots) == 5
end
end # @testset "split"
Loading