-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlimiters.go
88 lines (71 loc) · 1.4 KB
/
limiters.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
package rrule
import (
"sort"
"time"
)
func limitBySetPos(tt []time.Time, setpos []int) []time.Time {
if len(setpos) == 0 {
return tt
}
// a map of tt indexes to include
include := map[int]bool{}
for _, sp := range setpos {
if sp < 0 {
sp = len(tt) + sp
} else {
sp-- // setpos is 1-indexed in the rrule. adjust here
}
include[sp] = true
}
ret := make([]time.Time, 0, len(include))
for included := range include {
if len(tt) > included {
ret = append(ret, tt[included])
}
}
sort.Slice(ret, func(i, j int) bool {
return ret[i].Before(ret[j])
})
return ret
}
func limitInstancesBySetPos(tt []int, setpos []int) []int {
if len(setpos) == 0 {
return tt
}
// a map of tt indexes to include
include := make(map[int]bool, len(setpos))
for _, sp := range setpos {
if sp < 0 {
sp = len(tt) + sp
} else {
sp-- // setpos is 1-indexed in the rrule. adjust here
}
include[sp] = true
}
ret := make([]int, 0, len(include))
for included := range include {
if len(tt) > included {
ret = append(ret, tt[included])
}
}
sort.Ints(ret)
return ret
}
func combineLimiters(ll ...validFunc) func(t *time.Time) bool {
return func(t *time.Time) bool {
for _, l := range ll {
if !l(t) {
return false
}
}
return true
}
}
func checkLimiters(t *time.Time, ll ...validFunc) bool {
for _, l := range ll {
if !l(t) {
return false
}
}
return true
}