Repository navigation
Add time_slots and TimeWindow for issue #481 #553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
8462231
1563ef8
97784e8
02a793e
e7bd27b
225422a
4f9a300
09dc1ab
bac168f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||
|
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} | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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 | ||||||||||||||
| ) | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ###################### | ||||||||||||||
|
|
||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.