Skip to content

Commit 57276aa

Browse files
committed
breaking apart the large rrule.go
1 parent 53b19f9 commit 57276aa

File tree

10 files changed

+514
-491
lines changed

10 files changed

+514
-491
lines changed

expansions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func expandBySeconds(tt []time.Time, seconds ...int) []time.Time {
1212

1313
e := make([]time.Time, 0, len(tt)*len(seconds))
1414
for _, t := range tt {
15-
tmpl := t.Add(time.Duration(-1 * t.Second())* time.Second)
15+
tmpl := t.Add(time.Duration(-1*t.Second()) * time.Second)
1616
for _, s := range seconds {
1717
if s < 0 {
1818
s += 60
@@ -31,7 +31,7 @@ func expandByMinutes(tt []time.Time, minutes ...int) []time.Time {
3131

3232
e := make([]time.Time, 0, len(tt)*len(minutes))
3333
for _, t := range tt {
34-
tmpl := t.Add(time.Duration(-1 * t.Minute()) *time.Minute)
34+
tmpl := t.Add(time.Duration(-1*t.Minute()) * time.Minute)
3535
for _, m := range minutes {
3636
if m < 0 {
3737
m += 60
@@ -50,7 +50,7 @@ func expandByHours(tt []time.Time, hours ...int) []time.Time {
5050

5151
e := make([]time.Time, 0, len(tt)*len(hours))
5252
for _, t := range tt {
53-
tmpl := t.Add(time.Duration(-1 * t.Hour()) * time.Hour)
53+
tmpl := t.Add(time.Duration(-1*t.Hour()) * time.Hour)
5454
for _, h := range hours {
5555
if h < 0 {
5656
h += 24

frequency.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package rrule
2+
3+
type Frequency int
4+
5+
func (f Frequency) String() string {
6+
switch f {
7+
case Secondly:
8+
return "SECONDLY"
9+
case Minutely:
10+
return "MINUTELY"
11+
case Hourly:
12+
return "HOURLY"
13+
case Daily:
14+
return "DAILY"
15+
case Weekly:
16+
return "WEEKLY"
17+
case Monthly:
18+
return "MONTHLY"
19+
case Yearly:
20+
return "YEARLY"
21+
}
22+
return ""
23+
}
24+
25+
const (
26+
Secondly Frequency = iota
27+
Minutely
28+
Hourly
29+
Daily
30+
Weekly
31+
Monthly
32+
Yearly
33+
)

invalidBehavior.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package rrule
2+
3+
type InvalidBehavior int
4+
5+
const (
6+
OmitInvalid InvalidBehavior = iota
7+
NextInvalid
8+
PrevInvalid
9+
)

iterator.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package rrule
2+
3+
import "time"
4+
5+
type Iterator interface {
6+
Next() *time.Time
7+
}
8+
9+
type iterator struct {
10+
queue []time.Time
11+
totalQueued uint64
12+
queueCap uint64
13+
minTime time.Time
14+
maxTime time.Time
15+
pastMaxTime bool
16+
17+
// next finds the next key time.
18+
next func() *time.Time
19+
20+
// variations returns all the possible variations
21+
// of the key time t
22+
variations func(t *time.Time) []time.Time
23+
24+
// valid determines if a particular key time is a valid recurrence.
25+
valid func(t *time.Time) bool
26+
27+
setpos []int
28+
}
29+
30+
func (i *iterator) Next() *time.Time {
31+
if len(i.queue) > 0 {
32+
r := i.queue[0]
33+
i.queue = i.queue[1:]
34+
return &r
35+
}
36+
37+
if i.queueCap > 0 {
38+
if i.totalQueued >= i.queueCap {
39+
return nil
40+
}
41+
}
42+
43+
for {
44+
if i.pastMaxTime {
45+
return nil
46+
}
47+
48+
key := i.next()
49+
if key == nil {
50+
return nil
51+
}
52+
53+
if !i.valid(key) {
54+
continue
55+
}
56+
57+
variations := i.variations(key)
58+
59+
// remove any variations before the min time
60+
for len(variations) > 0 && variations[0].Before(i.minTime) {
61+
variations = variations[1:]
62+
}
63+
64+
// remove any variations after the max time
65+
if !i.maxTime.IsZero() {
66+
for idx, v := range variations {
67+
if v.After(i.maxTime) {
68+
variations = variations[:idx]
69+
i.pastMaxTime = true
70+
break
71+
}
72+
}
73+
}
74+
75+
// if we're left with nothing (or started there) skip this key time
76+
if len(variations) == 0 {
77+
continue
78+
}
79+
80+
if i.queueCap > 0 {
81+
if i.totalQueued+uint64(len(variations)) > i.queueCap {
82+
variations = variations[:i.queueCap-i.totalQueued]
83+
}
84+
}
85+
86+
i.totalQueued += uint64(len(variations))
87+
88+
i.queue = variations[1:]
89+
return &variations[0]
90+
}
91+
}

limiters.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package rrule
2+
3+
import (
4+
"sort"
5+
"time"
6+
)
7+
8+
func limitBySetPos(tt []time.Time, setpos []int) []time.Time {
9+
if len(setpos) == 0 {
10+
return tt
11+
}
12+
13+
// a map of tt indexes to include
14+
include := map[int]bool{}
15+
16+
for _, sp := range setpos {
17+
if sp < 0 {
18+
sp = len(tt) + sp
19+
} else {
20+
sp-- // setpos is 1-indexed in the rrule. adjust here
21+
}
22+
23+
include[sp] = true
24+
}
25+
26+
ret := make([]time.Time, 0, len(include))
27+
for included := range include {
28+
if len(tt) > included {
29+
ret = append(ret, tt[included])
30+
}
31+
}
32+
33+
sort.Slice(ret, func(i, j int) bool {
34+
return ret[i].Before(ret[j])
35+
})
36+
37+
return ret
38+
}
39+
40+
func limitInstancesBySetPos(tt []int, setpos []int) []int {
41+
if len(setpos) == 0 {
42+
return tt
43+
}
44+
45+
// a map of tt indexes to include
46+
include := make(map[int]bool, len(setpos))
47+
48+
for _, sp := range setpos {
49+
if sp < 0 {
50+
sp = len(tt) + sp
51+
} else {
52+
sp-- // setpos is 1-indexed in the rrule. adjust here
53+
}
54+
55+
include[sp] = true
56+
}
57+
58+
ret := make([]int, 0, len(include))
59+
for included := range include {
60+
if len(tt) > included {
61+
ret = append(ret, tt[included])
62+
}
63+
}
64+
65+
sort.Ints(ret)
66+
67+
return ret
68+
}
69+
70+
func combineLimiters(ll ...validFunc) func(t *time.Time) bool {
71+
return func(t *time.Time) bool {
72+
for _, l := range ll {
73+
if !l(t) {
74+
return false
75+
}
76+
}
77+
return true
78+
}
79+
}
80+
81+
func checkLimiters(t *time.Time, ll ...validFunc) bool {
82+
for _, l := range ll {
83+
if !l(t) {
84+
return false
85+
}
86+
}
87+
return true
88+
}

mappers.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package rrule
2+
3+
import "time"
4+
5+
func intmap(ints []int) map[int]bool {
6+
m := make(map[int]bool, len(ints))
7+
for _, v := range ints {
8+
m[v] = true
9+
}
10+
return m
11+
}
12+
13+
func weekdaymap(weekdays []QualifiedWeekday) map[time.Weekday]bool {
14+
m := make(map[time.Weekday]bool, len(weekdays))
15+
for _, v := range weekdays {
16+
m[v.WD] = true
17+
}
18+
return m
19+
}
20+
21+
func monthmap(months []time.Month) map[time.Month]bool {
22+
m := make(map[time.Month]bool, len(months))
23+
for _, v := range months {
24+
m[v] = true
25+
}
26+
return m
27+
}

0 commit comments

Comments
 (0)