forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodulo.go
45 lines (42 loc) · 1.29 KB
/
modulo.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
package reads
func Modulo(dividend, modulus int64) int64 {
r := dividend % modulus
if r < 0 {
r += modulus
}
return r
}
// WindowStart calculates the start time of a window given a timestamp,
// the window period (every), and the offset starting from the epoch.
//
// Note that the normalized offset value can fall on either side of the
// normalized timestamp. If it lies to the left we know it represents
// the start time. Otherwise it represents the stop time, in which case
// we decrement by the window period to get the start time.
//
func WindowStart(t, every, offset int64) int64 {
mod := Modulo(t, every)
off := Modulo(offset, every)
beg := t - mod + off
if mod < off {
beg -= every
}
return beg
}
// WindowStop calculates the stop time of a window given a timestamp,
// the window period (every), and the offset starting from the epoch.
//
// Note that the normalized offset value can fall on either side of the
// normalized timestamp. If it lies to the right we know it represents
// the stop time. Otherwise it represents the start time, in which case
// we increment by the window period to get the stop time.
//
func WindowStop(t, every, offset int64) int64 {
mod := Modulo(t, every)
off := Modulo(offset, every)
end := t - mod + off
if mod >= off {
end += every
}
return end
}