2
2
# #### Intervals -> AlignedSpan
3
3
# ####
4
4
5
- is_start_exclusive (:: Interval{T,L,R} ) where {T,L,R} = L == Open
6
- is_stop_exclusive (:: Interval{T,L,R} ) where {T,L,R} = R == Open
7
-
8
5
# Interface methods:
9
6
duration (interval:: Interval{<:TimePeriod} ) = last (interval) - first (interval)
10
7
11
- function start_index_from_time (sample_rate, interval:: Interval ,
8
+ function start_index_from_time (sample_rate, interval:: Interval{Nanosecond,Closed,Closed} ,
12
9
mode:: Union{RoundingMode{:Up},RoundingMode{:Down}} )
13
- first_index, err = index_and_error_from_time (sample_rate, first (interval), mode)
14
- if is_start_exclusive (interval) && mode == RoundUp && iszero (err)
15
- first_index += 1
16
- end
10
+ first_index, _ = index_and_error_from_time (sample_rate, first (interval), mode)
17
11
18
12
if mode == RoundUp
19
13
t = time_from_index (sample_rate, first_index)
@@ -23,6 +17,9 @@ function start_index_from_time(sample_rate, interval::Interval,
23
17
msg = """
24
18
[AlignedSpans] Unexpected error in `start_index_from_time`:
25
19
20
+ - `sample_rate = $(sample_rate) `
21
+ - `interval = $(interval) `
22
+ - `mode = $(mode) `
26
23
- `time_from_index(sample_rate, first_index) = $(t) `
27
24
- `first(interval) = $(first (interval)) `
28
25
- Expected `time_from_index(sample_rate, first_index) >= first(interval)`
@@ -33,28 +30,28 @@ function start_index_from_time(sample_rate, interval::Interval,
33
30
if ASSERTS_ON[]
34
31
error (msg)
35
32
else
36
- @warn msg
33
+ @warn msg maxlog = 100
37
34
end
38
35
end
39
36
end
40
37
return first_index
41
38
end
42
39
43
- function stop_index_from_time (sample_rate, interval:: Interval ,
40
+ function stop_index_from_time (sample_rate, interval:: Interval{Nanosecond,Closed,Closed} ,
44
41
mode:: Union{RoundingMode{:Up},RoundingMode{:Down}} )
45
- last_index, err = index_and_error_from_time (sample_rate, last (interval), mode)
46
- if is_stop_exclusive (interval) && mode == RoundDown && iszero (err)
47
- last_index -= 1
48
- end
42
+ last_index, _ = index_and_error_from_time (sample_rate, last (interval), mode)
49
43
50
44
if mode == RoundDown
51
45
t = time_from_index (sample_rate, last_index)
52
46
# this should *always* be true by construction, and we promise it in the docstring.
53
- # let's add an check to verify.
54
- if ! (t <= last (interval))
47
+ # let's add an check to verify. Note here we add 1ns to make it an open span again, since we've converted to closed
48
+ if ! (t <= last (interval) + Nanosecond ( 1 ) )
55
49
msg = """
56
50
[AlignedSpans] Unexpected error in `stop_index_from_time`:
57
51
52
+ - `sample_rate = $(sample_rate) `
53
+ - `interval = $(interval) `
54
+ - `mode = $(mode) `
58
55
- `time_from_index(sample_rate, last_index) = $(t) `
59
56
- `last(interval) = $(last (interval)) `
60
57
- Expected `time_from_index(sample_rate, last_index) <= last(interval)`
@@ -65,15 +62,15 @@ function stop_index_from_time(sample_rate, interval::Interval,
65
62
if ASSERTS_ON[]
66
63
error (msg)
67
64
else
68
- @warn msg
65
+ @warn msg maxlog = 100
69
66
end
70
67
end
71
68
end
72
69
return last_index
73
70
end
74
71
75
- function stop_index_from_time (sample_rate, interval:: Interval ,
76
- :: RoundingModeFullyContainedSampleSpans )
72
+ function stop_index_from_time (sample_rate, interval:: Interval{Nanosecond,Closed,Closed} ,
73
+ mode :: RoundingModeFullyContainedSampleSpans )
77
74
# here we are in `RoundingModeFullyContainedSampleSpans` which means we treat each sample
78
75
# as a closed-open span starting from each sample to just before the next sample,
79
76
# and we are trying to round down to the last fully-enclosed sample span
@@ -94,6 +91,9 @@ function stop_index_from_time(sample_rate, interval::Interval,
94
91
msg = """
95
92
[AlignedSpans] Unexpected error in `stop_index_from_time` with `RoundFullyContainedSampleSpans`:
96
93
94
+ - `sample_rate = $(sample_rate) `
95
+ - `interval = $(interval) `
96
+ - `mode = $(mode) `
97
97
- `end_of_span_time = $(end_of_span_time) `
98
98
- `interval = $(interval) `
99
99
- Expected `end_of_span_time in interval`
@@ -103,7 +103,7 @@ function stop_index_from_time(sample_rate, interval::Interval,
103
103
if ASSERTS_ON[]
104
104
error (msg)
105
105
else
106
- @warn msg
106
+ @warn msg maxlog = 100
107
107
end
108
108
end
109
109
@@ -129,8 +129,16 @@ TimeSpans.start(span::AlignedSpan) = time_from_index(span.sample_rate, span.firs
129
129
TimeSpans. stop (span:: AlignedSpan ) = time_from_index (span. sample_rate, span. last_index + 1 )
130
130
131
131
# TimeSpan -> AlignedSpan is supported by passing to Intervals
132
- to_interval (span) = Interval {Nanosecond,Closed,Open} (start (span), stop (span))
133
- to_interval (span:: Interval ) = span
132
+ function to_interval (span)
133
+ # we could return clopen intervals, but it's easier to work with closed-closed ones
134
+ # Interval{Nanosecond,Closed,Open}(start(span), stop(span))
135
+ # convert from open endpoint to closed by removing last ns
136
+ return Interval {Nanosecond,Closed,Closed} (start (span), stop (span) - Nanosecond (1 ))
137
+ end
138
+ to_interval (span:: Interval{Nanosecond,Closed,Closed} ) = span
139
+ function to_interval (span:: Interval{Nanosecond,Closed,Open} )
140
+ return Interval {Nanosecond,Closed,Closed} (first (span), last (span) - Nanosecond (1 ))
141
+ end
134
142
135
143
# Interface methods:
136
144
0 commit comments