Skip to content

Commit

Permalink
Revert "Fix #28 (#29)"
Browse files Browse the repository at this point in the history
This reverts commit b938543.
  • Loading branch information
ericphanson committed Jan 31, 2022
1 parent b938543 commit 91f9a6a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TimeSpans"
uuid = "bb34ddd2-327f-4c4a-bfb0-c98fc494ece1"
authors = ["Beacon Biosignals, Inc."]
version = "0.2.7"
version = "0.2.6"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
16 changes: 4 additions & 12 deletions src/TimeSpans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,11 @@ julia> index_from_time(100, Millisecond(1000))
101
```
"""
index_from_time(sample_rate, sample_time::Period) = first(index_and_is_rounded_from_time(sample_rate, sample_time))

# Helper to get the index and whether or not it has been rounded
function index_and_is_rounded_from_time(sample_rate, sample_time::Period)
function index_from_time(sample_rate, sample_time::Period)
time_in_nanoseconds = convert(Nanosecond, sample_time).value
time_in_nanoseconds >= 0 || throw(ArgumentError("`sample_time` must be >= 0 nanoseconds"))
time_in_seconds = time_in_nanoseconds / NS_IN_SEC
index = time_in_seconds * sample_rate + 1
return floor(Int, index), !isinteger(index)
return floor(Int, time_in_seconds * sample_rate) + 1
end

"""
Expand All @@ -237,12 +233,8 @@ julia> index_from_time(100, TimeSpan(Second(3), Second(6)))
"""
function index_from_time(sample_rate, span)
i = index_from_time(sample_rate, start(span))
j, is_rounded = index_and_is_rounded_from_time(sample_rate, stop(span))
# if `j` has been rounded down, then we are already excluding the right endpoint
# by means of that rounding. Hence, we don't need to decrement here.
if i != j && !is_rounded
j -= 1
end
j = index_from_time(sample_rate, stop(span))
j = i == j ? j : (j - 1)
return i:j
end

Expand Down
6 changes: 0 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ end
@test index_from_time(100, Nanosecond(0)) == 1
@test index_from_time(100, TimeSpan(Second(3), Second(6))) == 301:600
@test index_from_time(100, TimeSpan(Second(1))) == 101:101

# https://github.com/beacon-biosignals/TimeSpans.jl/issues/28
@test index_from_time(1, Millisecond(1500)) == 2
@test index_from_time(1, Millisecond(2500)) == 3
@test index_from_time(1, TimeSpan(Millisecond(1500), Millisecond(2500))) == 2:3

# test non-integer sample rates
rate = 100.66
ns_per_sample = nanoseconds_per_sample(rate)
Expand Down

0 comments on commit 91f9a6a

Please sign in to comment.