Skip to content

Commit

Permalink
remove range ambiguity and define istimespan
Browse files Browse the repository at this point in the history
  • Loading branch information
jrevels committed Dec 30, 2020
1 parent d8860e5 commit e46c5e5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 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.1.0"
version = "0.2.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
14 changes: 12 additions & 2 deletions src/TimeSpans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,25 @@ TimeSpan(x) = TimeSpan(start(x), stop(x))
##### generic TimeSpans.jl interface
#####

"""
istimespan(x)
Return `true` if `x` has been declared to support `TimeSpans.start(x)` and `TimeSpans.stop(x)`,
return `false` otherwise.
Types that overload `TimeSpans.start`/`TimeSpans.stop` should also overload `istimespan`.
"""
istimespan(::Any) = false
istimespan(::TimeSpan) = true
istimespan(::Period) = true

"""
start(span)
Return the inclusive lower bound of `span` as a `Nanosecond` value.
"""
start(span::TimeSpan) = span.start
start(t::Period) = convert(Nanosecond, t)
start(r::AbstractRange) = convert(Nanosecond, first(r))

"""
stop(span)
Expand All @@ -58,7 +69,6 @@ Return the exclusive upper bound of `span` as a `Nanosecond` value.
"""
stop(span::TimeSpan) = span.stop
stop(t::Period) = convert(Nanosecond, t) + Nanosecond(1)
stop(r::AbstractRange) = convert(Nanosecond, last(r))

#####
##### generic utilities
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ using Test, TimeSpans, Dates
@test TimeSpans.duration(TimeSpan(start(t), stop(t) + Nanosecond(100))) == Nanosecond(101)
@test TimeSpans.duration(start(t)) == Nanosecond(1)
@test_throws ArgumentError TimeSpan(4, 2)
@test TimeSpans.istimespan(t)
@test TimeSpans.istimespan(start(t))
@test !TimeSpans.istimespan(1)
@test !TimeSpans.istimespan(1:10)
end

@testset "contains(::TimeSpan...)" begin
Expand Down

2 comments on commit e46c5e5

@jrevels
Copy link
Member Author

@jrevels jrevels commented on e46c5e5 Jan 3, 2021

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/27244

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.0 -m "<description of version>" e46c5e5a4876e512cca3683a6714b7369f1fbffb
git push origin v0.2.0

Please sign in to comment.