-
Notifications
You must be signed in to change notification settings - Fork 153
/
bounds.go
97 lines (78 loc) · 1.76 KB
/
bounds.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package execute
import (
"fmt"
"math"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/values"
)
type Time = values.Time
type Duration = values.Duration
const (
MaxTime = math.MaxInt64
MinTime = math.MinInt64
)
type Bounds struct {
Start Time
Stop Time
}
var AllTime = Bounds{
Start: MinTime,
Stop: MaxTime,
}
func (b Bounds) IsEmpty() bool {
return b.Start >= b.Stop
}
func (b Bounds) String() string {
return fmt.Sprintf("[%v, %v)", b.Start, b.Stop)
}
func (b Bounds) Contains(t Time) bool {
return t >= b.Start && t < b.Stop
}
func (b Bounds) Overlaps(o Bounds) bool {
return b.Contains(o.Start) || (b.Contains(o.Stop) && o.Stop > b.Start) || o.Contains(b.Start)
}
// Intersect returns the intersection of two bounds.
// It returns empty bounds if one of the input bounds are empty.
// TODO: there are several places that implement bounds and related utilities.
//
// consider a central place for them?
func (b *Bounds) Intersect(o Bounds) Bounds {
if b.IsEmpty() || o.IsEmpty() || !b.Overlaps(o) {
return Bounds{
Start: b.Start,
Stop: b.Start,
}
}
i := Bounds{}
i.Start = b.Start
if o.Start > b.Start {
i.Start = o.Start
}
i.Stop = b.Stop
if o.Stop < b.Stop {
i.Stop = o.Stop
}
return i
}
func (b Bounds) Equal(o Bounds) bool {
return b == o
}
func (b Bounds) Shift(d Duration) Bounds {
return Bounds{Start: b.Start.Add(d), Stop: b.Stop.Add(d)}
}
func (b Bounds) Duration() Duration {
if b.IsEmpty() {
return values.ConvertDurationNsecs(0)
}
return b.Stop.Sub(b.Start)
}
func Now() Time {
return values.ConvertTime(time.Now())
}
func FromFluxBounds(bounds flux.Bounds) Bounds {
return Bounds{
Start: values.ConvertTime(bounds.Start.Time(bounds.Now)),
Stop: values.ConvertTime(bounds.Stop.Time(bounds.Now)),
}
}