forked from apognu/gocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrule.go
170 lines (145 loc) · 3.19 KB
/
rrule.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
package gocal
import (
"fmt"
"strconv"
"strings"
"github.com/etherlabsio/gocal/parser"
)
const YmdHis = "2006-01-02 15:04:05"
func (gc *Gocal) ExpandRecurringEvent(buf *Event) []Event {
freq := buf.RecurrenceRule["FREQ"]
until, err := parser.ParseTime(buf.RecurrenceRule["UNTIL"], map[string]string{}, parser.TimeEnd)
hasUntil := err == nil
count, err := strconv.Atoi(buf.RecurrenceRule["COUNT"])
if err != nil {
count = -1
}
interval, err := strconv.Atoi(buf.RecurrenceRule["INTERVAL"])
if err != nil {
interval = 1
}
byMonth, err := strconv.Atoi(buf.RecurrenceRule["BYMONTH"])
hasByMonth := err == nil
byDay, ok := buf.RecurrenceRule["BYDAY"]
hasByDay := ok
var years, days, months int
switch freq {
case "DAILY":
days = interval
months = 0
years = 0
break
case "WEEKLY":
days = 7 * interval
months = 0
years = 0
break
case "MONTHLY":
days = 0
months = interval
years = 0
break
case "YEARLY":
days = 0
months = 0
years = interval
break
default:
return []Event{}
}
currentCount := 0
freqDateStart := buf.Start
freqDateEnd := buf.End
ev := make([]Event, 0)
for {
weekDaysStart := freqDateStart
weekDaysEnd := freqDateEnd
if !hasByMonth || strings.Contains(fmt.Sprintf("%d", byMonth), weekDaysStart.Format("1")) {
if hasByDay {
for i := 0; i < 7; i++ {
excluded := false
for _, ex := range buf.ExcludeDates {
if ex.Equal(*weekDaysStart) {
excluded = true
break
}
}
if !excluded {
day := parseDayNameToIcsName(weekDaysStart.Format("Mon"))
if strings.Contains(byDay, day) {
currentCount++
count--
e := *buf
e.Start = weekDaysStart
e.End = weekDaysEnd
e.Uid = buf.Uid
e.Sequence = currentCount
if !hasUntil || (hasUntil && until.Format(YmdHis) >= weekDaysStart.Format(YmdHis)) {
if gc.IsInRange(e) {
ev = append(ev, e)
}
}
}
}
newStart := weekDaysStart.AddDate(0, 0, 1)
newEnd := weekDaysEnd.AddDate(0, 0, 1)
weekDaysStart = &newStart
weekDaysEnd = &newEnd
}
} else {
excluded := false
for _, ex := range buf.ExcludeDates {
if ex.Equal(*weekDaysStart) {
excluded = true
break
}
}
currentCount++
count--
if !excluded {
e := *buf
e.Start = weekDaysStart
e.End = weekDaysEnd
e.Uid = buf.Uid
e.Sequence = currentCount
if !hasUntil || (hasUntil && until.Format(YmdHis) >= weekDaysStart.Format(YmdHis)) {
if gc.IsInRange(e) {
ev = append(ev, e)
}
}
}
}
}
newStart := freqDateStart.AddDate(years, months, days)
newEnd := freqDateEnd.AddDate(years, months, days)
freqDateStart = &newStart
freqDateEnd = &newEnd
if (count < 0 && weekDaysStart.After(*gc.End)) || count == 0 {
break
}
if hasUntil && until.Format(YmdHis) <= freqDateStart.Format(YmdHis) {
break
}
}
return ev
}
func parseDayNameToIcsName(day string) string {
switch day {
case "Mon":
return "MO"
case "Tue":
return "TU"
case "Wed":
return "WE"
case "Thu":
return "TH"
case "Fri":
return "FR"
case "Sat":
return "SA"
case "Sun":
return "SU"
default:
return ""
}
}