Skip to content

Commit

Permalink
add comments and use intervals abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson committed Feb 12, 2025
1 parent a46528f commit 23bd60e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,24 @@ end

function stop_index_from_time(sample_rate, interval::Interval,
::RoundingModeFullyContainedSampleSpans)
last_index, error = index_and_error_from_time(sample_rate, last(interval), RoundDown)
if time_from_index(sample_rate, last_index + 1) >= last(interval)
# here we are in `RoundingModeFullyContainedSampleSpans` which means we treat each sample
# as a closed-open span starting from each sample to just before the next sample,
# and we are trying to round down to the last fully-enclosed sample span
last_index, _ = index_and_error_from_time(sample_rate, last(interval), RoundDown)

# `time_from_index(sample_rate, last_index + 1)` gives us the _start_ of the next sample
# we subtract 1 ns to get the (inclusive) _end_ of the span associated to this sample
end_of_span_time = time_from_index(sample_rate, last_index + 1) - Nanosecond(1)

# if this end isn't fully included in the interval, then we need to go back one
if !(end_of_span_time in interval)
@debug "Decrementing last index to fully fit within span"
last_index -= 1
end

t = time_from_index(sample_rate, last_index + 1)
@assert t <= last(interval)
# We should never need to decrement twice, but we will assert this
end_of_span_time = time_from_index(sample_rate, last_index + 1) - Nanosecond(1)
@assert end_of_span_time in interval
return last_index
end

Expand Down
4 changes: 4 additions & 0 deletions test/time_index_conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ end
aligned = AlignedSpan(1 / 30, input, RoundFullyContainedSampleSpans)
@test aligned == AlignedSpan(1 / 30, 1, 2)

input = TimeSpan(0, Second(60))
aligned = AlignedSpan(1 / 30, input, RoundFullyContainedSampleSpans)
@test aligned == AlignedSpan(1 / 30, 1, 2)

# No spans contained
input = TimeSpan(Nanosecond(1), Second(30) + Nanosecond(1))
@test_throws ArgumentError AlignedSpan(1 / 30, input, RoundFullyContainedSampleSpans)
Expand Down

0 comments on commit 23bd60e

Please sign in to comment.