beacon-biosignals / TimeSpans.jl

A Julia package that provides a `TimeSpan` type for representing a continuous span between two points in time.
Other
6 stars 2 forks source link

`middle(::TimeSpan)` #24

Closed palday closed 2 years ago

palday commented 2 years ago

I think it might make sense to define

Statistics.middle(t::TimeSpan) = (start(t) + stop(t)) / 2

I have a few uses for it. If it sounds generally useful, I'll open a PR.

ararslan commented 2 years ago

FWIW I'm not sure we'd be able to use that definition verbatim, since start(t) + stop(t) will be a Nanosecond and may be odd, so dividing by 2 will throw an InexactError.

Relatedly, what should middle(TimeSpan(0, 1)) return? It can't actually be represented in the precision provided by TimeSpans, nor by the Dates stdlib.

palday commented 2 years ago

Yeah, the edge cases occurred to me later. In both cases, I would say round down to the nearest whole Nanosecond because the spans are half-open intervals, so rounding down is line with "include the lower bound but not the upper".

ararslan commented 2 years ago

So I guess the definition would be fld(start(t) + stop(t), 2) then.

omus commented 2 years ago

May as well just let the user have the option of choosing the rounding mode and do:

Statistics.middle(t::TimeSpan, r::RoundingMode=RoundToZero) = div(start(t) + stop(t), 2, r)