forked from teambition/rrule-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rruleset.go
200 lines (172 loc) · 5.5 KB
/
rruleset.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package rrule
import (
"fmt"
"sort"
"time"
)
// Set allows more complex recurrence setups, mixing multiple rules, dates, exclusion rules, and exclusion dates
type Set struct {
rrule []*RRule
rdate []time.Time
exrule []*RRule
exdate []time.Time
dtstart time.Time
}
// Recurrence returns a slice of all the recurrence rules for a set
func (set *Set) Recurrence() []string {
var res []string
if !set.dtstart.IsZero() {
// No colon, DTSTART may have TZID, which would require a semicolon after DTSTART
res = append(res, fmt.Sprintf("DTSTART%s", timeToDtStartStr(set.dtstart)))
}
for _, item := range set.rrule {
res = append(res, fmt.Sprintf("RRULE:%s", item))
}
for _, item := range set.rdate {
res = append(res, fmt.Sprintf("RDATE:%s", timeToStr(item)))
}
for _, item := range set.exrule {
res = append(res, fmt.Sprintf("EXRULE:%s", item))
}
for _, item := range set.exdate {
res = append(res, fmt.Sprintf("EXDATE:%s", timeToStr(item)))
}
return res
}
// DTStart sets dtstart property for all rules in set
func (set *Set) DTStart(dtstart time.Time) {
set.dtstart = dtstart.Truncate(time.Second)
for _, r := range set.rrule {
r.DTStart(set.dtstart)
}
for _, r := range set.exrule {
r.DTStart(set.dtstart)
}
}
// GetDTStart gets dtstart for set
func (set *Set) GetDTStart() time.Time {
return set.dtstart
}
// RRule include the given rrule instance in the recurrence set generation.
func (set *Set) RRule(rrule *RRule) {
if !set.dtstart.IsZero() {
rrule.DTStart(set.dtstart)
}
set.rrule = append(set.rrule, rrule)
}
// GetRRule return the rrules in the set
func (set *Set) GetRRule() []*RRule {
return set.rrule
}
// RDate include the given datetime instance in the recurrence set generation.
func (set *Set) RDate(rdate time.Time) {
set.rdate = append(set.rdate, rdate)
}
// GetRDate returns explicitly added dates (rdates) in the set
func (set *Set) GetRDate() []time.Time {
return set.rdate
}
// ExRule include the given rrule instance in the recurrence set exclusion list.
// Dates which are part of the given recurrence rules will not be generated,
// even if some inclusive rrule or rdate matches them.
func (set *Set) ExRule(exrule *RRule) {
if !set.dtstart.IsZero() {
exrule.DTStart(set.dtstart)
}
set.exrule = append(set.exrule, exrule)
}
// GetExRule returns exclusion rrules list from in the set
func (set *Set) GetExRule() []*RRule {
return set.exrule
}
// ExDate include the given datetime instance in the recurrence set exclusion list.
// Dates included that way will not be generated,
// even if some inclusive rrule or rdate matches them.
func (set *Set) ExDate(exdate time.Time) {
set.exdate = append(set.exdate, exdate)
}
// GetExDate returns explicitly excluded dates (exdates) in the set
func (set *Set) GetExDate() []time.Time {
return set.exdate
}
type genItem struct {
dt time.Time
gen Next
}
type genItemSlice []genItem
func (s genItemSlice) Len() int { return len(s) }
func (s genItemSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s genItemSlice) Less(i, j int) bool { return s[i].dt.Before(s[j].dt) }
func addGenList(genList *[]genItem, next Next) {
dt, ok := next()
if ok {
*genList = append(*genList, genItem{dt, next})
}
}
// Iterator returns an iterator for rrule.Set
func (set *Set) Iterator() (next func() (time.Time, bool)) {
rlist := []genItem{}
exlist := []genItem{}
sort.Sort(timeSlice(set.rdate))
addGenList(&rlist, timeSliceIterator(set.rdate))
for _, r := range set.rrule {
addGenList(&rlist, r.Iterator())
}
sort.Sort(genItemSlice(rlist))
sort.Sort(timeSlice(set.exdate))
addGenList(&exlist, timeSliceIterator(set.exdate))
for _, r := range set.exrule {
addGenList(&exlist, r.Iterator())
}
sort.Sort(genItemSlice(exlist))
lastdt := time.Time{}
return func() (time.Time, bool) {
for len(rlist) != 0 {
dt := rlist[0].dt
var ok bool
rlist[0].dt, ok = rlist[0].gen()
if !ok {
rlist = rlist[1:]
}
sort.Sort(genItemSlice(rlist))
if lastdt.IsZero() || !lastdt.Equal(dt) {
for len(exlist) != 0 && exlist[0].dt.Before(dt) {
exlist[0].dt, ok = exlist[0].gen()
if !ok {
exlist = exlist[1:]
}
sort.Sort(genItemSlice(exlist))
}
lastdt = dt
if len(exlist) == 0 || !dt.Equal(exlist[0].dt) {
return dt, true
}
}
}
return time.Time{}, false
}
}
// All returns all occurrences of the rrule.Set.
func (set *Set) All() []time.Time {
return all(set.Iterator())
}
// Between returns all the occurrences of the rrule between after and before.
// The inc keyword defines what happens if after and/or before are themselves occurrences.
// With inc == True, they will be included in the list, if they are found in the recurrence set.
func (set *Set) Between(after, before time.Time, inc bool) []time.Time {
return between(set.Iterator(), after, before, inc)
}
// Before Returns the last recurrence before the given datetime instance,
// or time.Time's zero value if no recurrence match.
// The inc keyword defines what happens if dt is an occurrence.
// With inc == True, if dt itself is an occurrence, it will be returned.
func (set *Set) Before(dt time.Time, inc bool) time.Time {
return before(set.Iterator(), dt, inc)
}
// After returns the first recurrence after the given datetime instance,
// or time.Time's zero value if no recurrence match.
// The inc keyword defines what happens if dt is an occurrence.
// With inc == True, if dt itself is an occurrence, it will be returned.
func (set *Set) After(dt time.Time, inc bool) time.Time {
return after(set.Iterator(), dt, inc)
}