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
51 changes: 51 additions & 0 deletions src/split.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,57 @@ 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

"""
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 `TimeArray` 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),
)
timestamps = filter(dt -> Time(dt) ∈ from:interval:to, start:interval:stop)
return TimeArray(timestamps, ones(length(timestamps)))
Comment thread
anurag-mds marked this conversation as resolved.
Resolved
end

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

Return A `TimeArray` 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)
)
timestamps = filter(dt -> Time(dt) ∈ from:step(range):to, range)
return TimeArray(timestamps, ones(length(timestamps)))
end

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

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

@testset "time_slots" begin
@test length(
time_slots(
DateTime(2026, 1, 1),
DateTime(2026, 1, 3),
Minute(15);
from=Time(8, 45),
to=Time(13, 45),
),
) == 42
@test length(
time_slots(
DateTime(2026, 1, 1),
DateTime(2026, 1, 1),
Minute(15);
from=Time(14, 0),
to=Time(15, 0),
),
) == 0
@test length(
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(
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(
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(
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
end # @testset "split"
Loading