forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_util.go
46 lines (38 loc) · 1.13 KB
/
time_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"time"
"github.com/timescale/tsbs/internal/utils"
)
type TimeIntervals []*utils.TimeInterval
// implement sort.Interface
func (x TimeIntervals) Len() int { return len(x) }
func (x TimeIntervals) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x TimeIntervals) Less(i, j int) bool {
return x[i].Start().Before(x[j].Start())
}
// bucketTimeIntervals is a helper that creates a slice of TimeInterval
// over the given span of time, in chunks of duration `window`.
func bucketTimeIntervals(start, end time.Time, window time.Duration) []*utils.TimeInterval {
if end.Before(start) {
panic("logic error in bucketTimeIntervals: bad input times")
}
ret := []*utils.TimeInterval{}
start = start.Truncate(window)
for start.Before(end) {
ti, err := utils.NewTimeInterval(start, start.Add(window))
if err != nil {
panic(fmt.Sprintf("unexpected error: %v", err))
}
ret = append(ret, ti)
start = start.Add(window)
}
// sanity check
tis := TimeIntervals(ret)
for i := 0; i < len(tis)-1; i++ {
if !tis.Less(i, i+1) {
panic("logic error: unsorted buckets in bucketTimeIntervals")
}
}
return ret
}