Skip to content

Commit

Permalink
Fix #28 (#29)
Browse files Browse the repository at this point in the history
* fix #28

* rm extra space

* bump batch
  • Loading branch information
ericphanson authored Jan 28, 2022
1 parent eb8b794 commit b938543
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 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.6"
version = "0.2.7"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
16 changes: 12 additions & 4 deletions src/TimeSpans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,15 @@ julia> index_from_time(100, Millisecond(1000))
101
```
"""
function index_from_time(sample_rate, sample_time::Period)
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)
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
return floor(Int, time_in_seconds * sample_rate) + 1
index = time_in_seconds * sample_rate + 1
return floor(Int, index), !isinteger(index)
end

"""
Expand All @@ -233,8 +237,12 @@ 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 = index_from_time(sample_rate, stop(span))
j = i == j ? j : (j - 1)
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
return i:j
end

Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ 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

2 comments on commit b938543

@ericphanson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53396

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.7 -m "<description of version>" b93854383ab5b3e715db7de957f369bb38989461
git push origin v0.2.7

Please sign in to comment.