Skip to content

Time slots iterators and helper functions #481

Closed

Description

@iblislin
  • I need a iterator to create continuous time slots in a given interval of a day.
    • e.g. start from 2021/1/1 to 2021/1/10, in each day, I need to create 15m time slots from am 8:45 to pm 1:45.
  • Helper functions:
    • Given a timestamp, find the time slots which the timestamp belongs to.

Activity

  1. anurag-mds commented on Mar 1, 2026

    @anurag-mds
    Contributor

    Hi @iblislin! I've been contributing to
    TimeSeries.jl and I'm interested in working
    on this issue.

    Could you clarify a few things before I start:

    1. Is this issue still relevant?
    2. What would the expected API look like?
    3. Are there any existing packages or
      functions I should build on top of?

    Happy to take this on if it's still needed!

  2. iblislin commented on Mar 3, 2026

    @iblislin
    CollaboratorAuthor

    well, I think it still relevant, but not sure about there is some re-usable codebase about the resampling function recently added or not.

    About the API, maybe a factory function to create iterator object that obey the iterator protocol in internal implementation, then return the TimeArray for the public API.

    maybe you could put your idea about the API design first here.

  3. anurag-mds commented on Mar 3, 2026

    @anurag-mds
    Contributor

    Thanks! Here's a rough API idea:
    time_slots(start, stop, interval; from=Time(8,45), to=Time(13,45))
    returning a TimeArray. Does that direction make sense?

  4. iblislin commented on Mar 4, 2026

    @iblislin
    CollaboratorAuthor

    Thanks! Here's a rough API idea:
    time_slots(start, stop, interval; from=Time(8,45), to=Time(13,45))
    returning a TimeArray. Does that direction make sense?

    oh, I just got an idea:
    filter(∈(Time(8,45):Time(13,45)), DateTime(2026, 1, 1):Minute(15):DateTime(2026, 12, 31))

    Do this style make sense to you?

    Also, it' fine for me to provide several styles of API.

  5. anurag-mds commented on Mar 4, 2026

    @anurag-mds
    Contributor

    That's a clean approach! So the idea would be to make Time ranges work with ∈ and step through DateTime ranges essentially making the filtering composable with Julia's existing iteration protocol rather than a custom API. I'll prototype this and share what I come up with.

  6. anurag-mds commented on Mar 9, 2026

    @anurag-mds
    Contributor
    julia> using Dates
    
    julia> result = filter(∈(Time(8,45):Minute(15):Time(13,45)),
                           DateTime(2026,1,1):Minute(15):DateTime(2026,1,10))
    ERROR: promotion of types Time and DateTime failed to change any arguments
    Stacktrace:
     [1] error(::String, ::String, ::String)
       @ Base .\error.jl:54
     [2] sametype_error(input::Tuple{Time, DateTime})
       @ Base .\promotion.jl:428
     [3] not_sametype(x::Tuple{Time, DateTime}, y::Tuple{Time, DateTime})
       @ Base .\promotion.jl:422
     [4] promote
       @ .\promotion.jl:405 [inlined]
     [5] ==(x::Time, y::DateTime)
       @ Dates C:\Users\RadhaKunjBalram\.julia\juliaup\julia-1.12.5+0.x64.w64.mingw32\share\julia\stdlib\v1.12\Dates\src\types.jl:475
     [6] in
       @ .\operators.jl:1362 [inlined]
     [7] Fix
       @ .\operators.jl:1194 [inlined]
     [8] filter(f::Base.Fix2{typeof(in), StepRange{Time, Minute}}, a::StepRange{DateTime, Minute})
       @ Base .\array.jl:2952
     [9] top-level scope
       @ REPL[5]:1
    
    julia> 
    

    Prototyped the core logic. The key insight is that ∈ fails when comparing Time and DateTime directly due to type promotion, but extracting Time(dt) from each DateTime solves it cleanly:

    juliatimestamps = filter(dt -> Time(dt) ∈ Time(8,45):Minute(15):Time(13,45),
                        DateTime(2026,1,1):Minute(15):DateTime(2026,1,10))
    ta = TimeArray(timestamps, ones(length(timestamps)))
    # 189×1 TimeArray{Float64,1,DateTime,...}
    

    Planning to wrap this as:

    juliatime_slots(start, stop, interval; from=Time(8,45), to=Time(13,45))

    returning a TimeArray. Does this direction look good to proceed with a PR?

                   _
       _       _ _(_)_     |  Documentation: https://docs.julialang.org
      (_)     | (_) (_)    |
       _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
      | | | | | | |/ _` |  |
      | | |_| | | | (_| |  |  Version 1.12.5 (2026-02-09)
     _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org release
    |__/                   |
    
    julia> using TimeSeries
    
    julia> 
    
    julia> timestamps = filter(dt -> Time(dt) ∈ Time(8,45):Minute(15):Time(13,45),
                               DateTime(2026,1,1):Minute(15):DateTime(2026,1,10))
    189-element Vector{DateTime}:
     2026-01-01T08:45:00
     2026-01-01T09:00:00
     2026-01-01T09:15:00
     2026-01-01T09:30:00
     2026-01-01T09:45:00
     2026-01-01T10:00:00
     2026-01-01T10:15:00
     2026-01-01T10:30:00
     2026-01-01T10:45:00
     2026-01-01T11:00:00
     2026-01-01T11:15:00
     2026-01-01T11:30:00
     2026-01-01T11:45:00
     2026-01-01T12:00:00
     2026-01-01T12:15:00
     2026-01-01T12:30:00
     2026-01-01T12:45:00
     2026-01-01T13:00:00
     2026-01-01T13:15:00
     2026-01-01T13:30:00
     2026-01-01T13:45:00
     ⋮
     2026-01-09T09:00:00
     2026-01-09T09:15:00
     2026-01-09T09:30:00
     2026-01-09T09:45:00
     2026-01-09T10:00:00
     2026-01-09T10:15:00
     2026-01-09T10:30:00
     2026-01-09T10:45:00
     2026-01-09T11:00:00
     2026-01-09T11:15:00
     2026-01-09T11:30:00
     2026-01-09T11:45:00
     2026-01-09T12:00:00
     2026-01-09T12:15:00
     2026-01-09T12:30:00
     2026-01-09T12:45:00
     2026-01-09T13:00:00
     2026-01-09T13:15:00
     2026-01-09T13:30:00
     2026-01-09T13:45:00
    
    julia> 
    
    julia> ta = TimeArray(timestamps, ones(length(timestamps)))
    189×1 TimeArray{Float64, 1, DateTime, Vector{Float64}} 2026-01-01T08:45:00 to 2026-01-09T13:45:00
    ┌─────────────────────┬─────┐
    │                     │ A   │
    ├─────────────────────┼─────┤
    │ 2026-01-01T08:45:00 │ 1.0 │
    │ 2026-01-01T09:00:00 │ 1.0 │
    │ 2026-01-01T09:15:00 │ 1.0 │
    │ 2026-01-01T09:30:00 │ 1.0 │
    │ 2026-01-01T09:45:00 │ 1.0 │
    │ 2026-01-01T10:00:00 │ 1.0 │
    │ 2026-01-01T10:15:00 │ 1.0 │
    │ 2026-01-01T10:30:00 │ 1.0 │
    │ 2026-01-01T10:45:00 │ 1.0 │
    │ 2026-01-01T11:00:00 │ 1.0 │
    │ 2026-01-01T11:15:00 │ 1.0 │
    │ 2026-01-01T11:30:00 │ 1.0 │
    │ 2026-01-01T11:45:00 │ 1.0 │
    │ 2026-01-01T12:00:00 │ 1.0 │
    │ 2026-01-01T12:15:00 │ 1.0 │
    │ 2026-01-01T12:30:00 │ 1.0 │
    │ 2026-01-01T12:45:00 │ 1.0 │
    │ 2026-01-01T13:00:00 │ 1.0 │
    │ 2026-01-01T13:15:00 │ 1.0 │
    │          ⋮          │  ⋮  │
    │ 2026-01-09T09:30:00 │ 1.0 │
    │ 2026-01-09T09:45:00 │ 1.0 │
    │ 2026-01-09T10:00:00 │ 1.0 │
    │ 2026-01-09T10:15:00 │ 1.0 │
    │ 2026-01-09T10:30:00 │ 1.0 │
    │ 2026-01-09T10:45:00 │ 1.0 │
    │ 2026-01-09T11:00:00 │ 1.0 │
    │ 2026-01-09T11:15:00 │ 1.0 │
    │ 2026-01-09T11:30:00 │ 1.0 │
    │ 2026-01-09T11:45:00 │ 1.0 │
    │ 2026-01-09T12:00:00 │ 1.0 │
    │ 2026-01-09T12:15:00 │ 1.0 │
    │ 2026-01-09T12:30:00 │ 1.0 │
    │ 2026-01-09T12:45:00 │ 1.0 │
    │ 2026-01-09T13:00:00 │ 1.0 │
    │ 2026-01-09T13:15:00 │ 1.0 │
    │ 2026-01-09T13:30:00 │ 1.0 │
    │ 2026-01-09T13:45:00 │ 1.0 │
    └─────────────────────┴─────┘
                 152 rows omitted
    
    julia> 
    
  7. iblislin commented on Mar 10, 2026

    @iblislin
    CollaboratorAuthor

    ah, fine

    Maybe we could have following public APIs:

    time_slots(start, stop, interval; from=Time(8,45), to=Time(13,45)) # could returns a lazy iterator that do not materialized the vector at first?
    
    time_slots(range_object; from=..., to=...)
    
    when(ta, time_slots(; from=..., to=...)) # this returns a new TimeArray

    If you have more ideas, fine to put them here.

  8. anurag-mds commented on Mar 10, 2026

    @anurag-mds
    Contributor

    Thanks!,
    I'll implement all three. The when variant is especially useful for filtering existing TimeArrays by time-of-day very composable with real data. Will start with basic time_slots and share a draft PR soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions