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
84 changes: 84 additions & 0 deletions src/split.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,90 @@ function when(ta::TimeArray, period::Function, t::Integer)
end
when(ta::TimeArray, period::Function, t::String) = ta[findall(period.(timestamp(ta)) .== t)]

"""
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]`.
"""
Comment thread
anurag-mds marked this conversation as resolved.
Resolved
struct TimeWindow
from::Time
to::Time
end

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

function when(ta::TimeArray, window::TimeWindow)
return ta[findall(t -> window.from <= Time(t) <= window.to, timestamp(ta))]
end

struct TimeSlot{T<:DateTime,P<:Period}
start::DateTime
stop::DateTime
interval::P
from::Time
to::Time
end
Comment thread
anurag-mds marked this conversation as resolved.
Resolved

Base.IteratorSize(::Type{<:TimeSlot}) = Base.SizeUnknown()
Base.eltype(::Type{<:TimeSlot}) = DateTime

function Base.iterate(ts::TimeSlot)
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, state::DateTime)
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::DateTime, stop::DateTime, interval::Period;
from::Time=Time(0,0), to::Time=Time(23,59))
Comment thread
anurag-mds marked this conversation as resolved.
Resolved

Return A `Vector{DateTime}` 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::DateTime,
stop::DateTime,
interval::Period;
from::Time=Time(0, 0),
to::Time=Time(23, 59),
)
return TimeSlot{DateTime,typeof(interval)}(start, stop, interval, from, to)
end

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

Return A `Vector{DateTime}` where each timestamp falls within the daily time
window `[from, to]`,
filtered from the given `range`
"""
function time_slots(
range::StepRange{DateTime}; from::Time=Time(0, 0), to::Time=Time(23, 59)
)
return TimeSlot{DateTime,typeof(step(range))}(
range.start, range.stop, step(range), from, to
)
end

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

"""
Expand Down
88 changes: 88 additions & 0 deletions test/split.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,92 @@ 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

@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