API Documentation
TimeSpans.TimeSpan
— TypeTimeSpan(start, stop)
Return TimeSpan(Nanosecond(start), Nanosecond(stop))
representing the interval [start, stop)
.
If start == stop
, a single Nanosecond
is added to stop
since stop
is an exclusive upper bound and TimeSpan operations only generally support up to nanosecond precision anyway.
The benefit of this type over e.g. Nanosecond(start):Nanosecond(1):Nanosecond(stop)
is that instances of this type are guaranteed to obey TimeSpans.start(x) < TimeSpans.stop(x)
by construction.
TimeSpans.start
— Functionstart(span)
Return the inclusive lower bound of span
as a Nanosecond
value.
TimeSpans.stop
— Functionstop(span)
Return the exclusive upper bound of span
as a Nanosecond
value.
TimeSpans.contains
— FunctionTimeSpans.contains(a, b)
Return true
if the timespan b
lies entirely within the timespan a
, return false
otherwise.
TimeSpans.overlaps
— Functionoverlaps(a, b)
Return true
if the timespan a
and the timespan b
overlap, return false
otherwise.
TimeSpans.shortest_timespan_containing
— Functionshortest_timespan_containing(spans)
Return the shortest possible TimeSpan
containing all timespans in spans
.
spans
is assumed to be an iterable of timespans.
shortest_timespan_containing(a, b)
Return the shortest possible TimeSpan
containing the timespans a
and b
.
TimeSpans.duration
— Functionduration(span)
Return stop(span) - start(span)
.
TimeSpans.translate
— Functiontranslate(span, by::Period)
Return TimeSpan(start(span) + by, stop(span) + by)
.
TimeSpans.time_from_index
— Functiontime_from_index(sample_rate, sample_index)
Given sample_rate
in Hz and assuming sample_index > 0
, return the earliest Nanosecond
containing sample_index
.
Examples:
julia> time_from_index(1, 1)
0 nanoseconds
julia> time_from_index(1, 2)
1000000000 nanoseconds
julia> time_from_index(100, 100)
990000000 nanoseconds
julia> time_from_index(100, 101)
1000000000 nanoseconds
time_from_index(sample_rate, sample_range::AbstractUnitRange)
Return the TimeSpan
corresponding to sample_range
given sample_rate
in Hz.
Note that the returned span includes the time period between the final sample and its successor, excluding the successor itself.
Examples:
julia> time_from_index(100, 1:100)
TimeSpan(0 nanoseconds, 1000000000 nanoseconds)
julia> time_from_index(100, 101:101)
TimeSpan(1000000000 nanoseconds, 1000000001 nanoseconds)
julia> time_from_index(100, 301:600)
TimeSpan(3000000000 nanoseconds, 6000000000 nanoseconds)
TimeSpans.index_from_time
— Functionindex_from_time(sample_rate, sample_time::Period)
Given sample_rate
in Hz, return the integer index of the most recent sample taken at sample_time
. Note that sample_time
must be non-negative and support convert(Nanosecond, sample_time)
.
Examples:
julia> index_from_time(1, Second(0))
1
julia> index_from_time(1, Second(1))
2
julia> index_from_time(100, Millisecond(999))
100
julia> index_from_time(100, Millisecond(1000))
101
index_from_time(sample_rate, span)
Return the UnitRange
of indices corresponding to span
given sample_rate
in Hz:
julia> index_from_time(100, TimeSpan(Second(0), Second(1)))
1:100
julia> index_from_time(100, TimeSpan(Second(1)))
101:101
julia> index_from_time(100, TimeSpan(Second(3), Second(6)))
301:600
TimeSpans.merge_spans
— FunctionTimeSpans.merge_spans!
— Functionmerge_spans!(predicate, spans)
Given a mutable, indexable iterator of timespans and a function to compare two time-sequential timespans, return the iterator in sorted order with all pairs for which predicate
returns true
merged via shortest_timespan_containing
.
julia> spans = [TimeSpan(0, 10), TimeSpan(6, 12), TimeSpan(15, 20),
TimeSpan(21, 30), TimeSpan(29, 31)]
5-element Vector{TimeSpan}:
TimeSpan(00:00:00.000000000, 00:00:00.000000010)
TimeSpan(00:00:00.000000006, 00:00:00.000000012)
TimeSpan(00:00:00.000000015, 00:00:00.000000020)
TimeSpan(00:00:00.000000021, 00:00:00.000000030)
TimeSpan(00:00:00.000000029, 00:00:00.000000031)
julia> merge_spans!(overlaps, spans)
3-element Vector{TimeSpan}:
TimeSpan(00:00:00.000000000, 00:00:00.000000012)
TimeSpan(00:00:00.000000015, 00:00:00.000000020)
TimeSpan(00:00:00.000000021, 00:00:00.000000031)
julia> merge_spans!((a, b) -> start(b) - stop(a) < Nanosecond(5), spans)
1-element Vector{TimeSpan}:
TimeSpan(00:00:00.000000000, 00:00:00.000000031)