forked from monzo/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
53 lines (45 loc) · 993 Bytes
/
bucket.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
package gocassa
import (
"time"
)
func bucket(t time.Time, step time.Duration) time.Time {
return bucketIter{
v: t,
step: step}.Bucket()
}
type bucketIter struct {
v time.Time
step time.Duration
field string
invariant Filter
}
func (b bucketIter) String() string {
return b.v.String()
}
func (b bucketIter) Bucket() time.Time {
step := b.step
if step < time.Second {
step = time.Second
}
secs := b.v.Unix()
return time.Unix((secs - secs%int64(step/time.Second)), 0)
}
func (b bucketIter) Next() Buckets {
return bucketIter{
v: b.v.Add(b.step),
step: b.step,
invariant: b.invariant,
field: b.field}
}
func (b bucketIter) Prev() Buckets {
return bucketIter{
v: b.v.Add(-b.step),
step: b.step,
invariant: b.invariant,
field: b.field}
}
func (b bucketIter) Filter() Filter {
rels := b.invariant.Relations()
rels = append(rels, Eq(b.field, b.Bucket()))
return b.invariant.Table().Where(rels...)
}