forked from teambition/rrule-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rruleset.go
196 lines (166 loc) · 5.47 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
package rrule
import (
"fmt"
"sort"
"time"
)
// Set allows more complex recurrence setups, mixing multiple rules, dates, exclusion rules, and exclusion dates
type Set struct {
dtstart time.Time
rrule *RRule
rdate []time.Time
exdate []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", timeToRFCDatetimeStr(set.dtstart)))
}
if set.rrule != nil {
res = append(res, fmt.Sprintf("RRULE:%s", set.rrule.OrigOptions.RRuleString()))
}
for _, item := range set.rdate {
res = append(res, fmt.Sprintf("RDATE:%s", timeToStr(item)))
}
for _, item := range set.exdate {
res = append(res, fmt.Sprintf("EXDATE:%s", timeToStr(item)))
}
return res
}
// DTStart sets dtstart property for set
func (set *Set) DTStart(dtstart time.Time) {
set.dtstart = dtstart.Truncate(time.Second)
if set.rrule != nil {
set.rrule.DTStart(set.dtstart)
}
}
// GetDTStart gets dtstart for set
func (set *Set) GetDTStart() time.Time {
return set.dtstart
}
// RRule set the RRULE for set.
// There is the only one RRULE in the set as https://tools.ietf.org/html/rfc5545#appendix-A.1
func (set *Set) RRule(rrule *RRule) {
if !rrule.OrigOptions.Dtstart.IsZero() {
set.dtstart = rrule.dtstart
} else if !set.dtstart.IsZero() {
rrule.DTStart(set.dtstart)
}
set.rrule = rrule
}
// GetRRule returns 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.Truncate(time.Second))
}
// SetRDates sets explicitly added dates (rdates) in the set
func (set *Set) SetRDates(rdates []time.Time) {
set.rdate = make([]time.Time, 0, len(rdates))
for _, rdate := range rdates {
set.rdate = append(set.rdate, rdate.Truncate(time.Second))
}
}
// GetRDate returns explicitly added dates (rdates) in the set
func (set *Set) GetRDate() []time.Time {
return set.rdate
}
// 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.Truncate(time.Second))
}
// SetExDates sets explicitly excluded dates (exdates) in the set
func (set *Set) SetExDates(exdates []time.Time) {
set.exdate = make([]time.Time, 0, len(exdates))
for _, exdate := range exdates {
set.exdate = append(set.exdate, exdate.Truncate(time.Second))
}
}
// 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))
if set.rrule != nil {
addGenList(&rlist, set.rrule.Iterator())
}
sort.Sort(genItemSlice(rlist))
sort.Sort(timeSlice(set.exdate))
addGenList(&exlist, timeSliceIterator(set.exdate))
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)
}