forked from teambition/rrule-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.go
247 lines (230 loc) · 6.3 KB
/
str.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package rrule
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
)
func timeToStr(time time.Time) string {
return time.UTC().Format("20060102T150405Z")
}
func strToTime(str string) (time.Time, error) {
return time.Parse("20060102T150405Z", str)
}
func (f Frequency) String() string {
return [...]string{
"YEARLY", "MONTHLY", "WEEKLY", "DAILY",
"HOURLY", "MINUTELY", "SECONDLY"}[f]
}
func strToFreq(str string) (Frequency, error) {
freqMap := map[string]Frequency{
"YEARLY": YEARLY, "MONTHLY": MONTHLY, "WEEKLY": WEEKLY, "DAILY": DAILY,
"HOURLY": HOURLY, "MINUTELY": MINUTELY, "SECONDLY": SECONDLY,
}
result, ok := freqMap[str]
if !ok {
return 0, errors.New("undefined frequency: " + str)
}
return result, nil
}
func (wday Weekday) String() string {
s := [...]string{"MO", "TU", "WE", "TH", "FR", "SA", "SU"}[wday.weekday]
if wday.n == 0 {
return s
}
return fmt.Sprintf("%+d%s", wday.n, s)
}
func strToWeekday(str string) (Weekday, error) {
if len(str) < 2 {
return Weekday{}, errors.New("undefined weekday: " + str)
}
weekMap := map[string]Weekday{
"MO": MO, "TU": TU, "WE": WE, "TH": TH,
"FR": FR, "SA": SA, "SU": SU}
result, ok := weekMap[str[len(str)-2:]]
if !ok {
return Weekday{}, errors.New("undefined weekday: " + str)
}
if len(str) > 2 {
n, e := strconv.Atoi(str[:len(str)-2])
if e != nil {
return Weekday{}, e
}
result.n = n
}
return result, nil
}
func strToWeekdays(value string) ([]Weekday, error) {
contents := strings.Split(value, ",")
result := make([]Weekday, len(contents))
var e error
for i, s := range contents {
result[i], e = strToWeekday(s)
if e != nil {
return nil, e
}
}
return result, nil
}
func appendIntsOption(options []string, key string, value []int) []string {
if len(value) == 0 {
return options
}
valueStr := make([]string, len(value))
for i, v := range value {
valueStr[i] = strconv.Itoa(v)
}
return append(options, fmt.Sprintf("%s=%s", key, strings.Join(valueStr, ",")))
}
func strToInts(value string) ([]int, error) {
contents := strings.Split(value, ",")
result := make([]int, len(contents))
var e error
for i, s := range contents {
result[i], e = strconv.Atoi(s)
if e != nil {
return nil, e
}
}
return result, nil
}
func (option *ROption) String() string {
result := []string{fmt.Sprintf("FREQ=%v", option.Freq)}
if !option.Dtstart.IsZero() {
result = append(result, fmt.Sprintf("DTSTART=%s", timeToStr(option.Dtstart)))
}
if option.Interval != 0 {
result = append(result, fmt.Sprintf("INTERVAL=%v", option.Interval))
}
if option.Wkst != MO {
result = append(result, fmt.Sprintf("WKST=%v", option.Wkst))
}
if option.Count != 0 {
result = append(result, fmt.Sprintf("COUNT=%v", option.Count))
}
if !option.Until.IsZero() {
result = append(result, fmt.Sprintf("UNTIL=%v", timeToStr(option.Until)))
}
result = appendIntsOption(result, "BYSETPOS", option.Bysetpos)
result = appendIntsOption(result, "BYMONTH", option.Bymonth)
result = appendIntsOption(result, "BYMONTHDAY", option.Bymonthday)
result = appendIntsOption(result, "BYYEARDAY", option.Byyearday)
result = appendIntsOption(result, "BYWEEKNO", option.Byweekno)
if len(option.Byweekday) != 0 {
valueStr := make([]string, len(option.Byweekday))
for i, wday := range option.Byweekday {
valueStr[i] = wday.String()
}
result = append(result, fmt.Sprintf("BYDAY=%s", strings.Join(valueStr, ",")))
}
result = appendIntsOption(result, "BYHOUR", option.Byhour)
result = appendIntsOption(result, "BYMINUTE", option.Byminute)
result = appendIntsOption(result, "BYSECOND", option.Bysecond)
result = appendIntsOption(result, "BYEASTER", option.Byeaster)
return strings.Join(result, ";")
}
// StrToROption converts string to ROption
func StrToROption(rfcString string) (*ROption, error) {
rfcString = strings.TrimSpace(rfcString)
if len(rfcString) == 0 {
return nil, errors.New("empty string")
}
result := ROption{}
for _, attr := range strings.Split(rfcString, ";") {
keyValue := strings.Split(attr, "=")
if len(keyValue) != 2 {
return nil, errors.New("wrong format")
}
key, value := keyValue[0], keyValue[1]
if len(value) == 0 {
return nil, errors.New(key + " option has no value")
}
var e error
switch key {
case "FREQ":
result.Freq, e = strToFreq(value)
case "DTSTART":
result.Dtstart, e = strToTime(value)
case "INTERVAL":
result.Interval, e = strconv.Atoi(value)
case "WKST":
result.Wkst, e = strToWeekday(value)
case "COUNT":
result.Count, e = strconv.Atoi(value)
case "UNTIL":
result.Until, e = strToTime(value)
case "BYSETPOS":
result.Bysetpos, e = strToInts(value)
case "BYMONTH":
result.Bymonth, e = strToInts(value)
case "BYMONTHDAY":
result.Bymonthday, e = strToInts(value)
case "BYYEARDAY":
result.Byyearday, e = strToInts(value)
case "BYWEEKNO":
result.Byweekno, e = strToInts(value)
case "BYDAY":
result.Byweekday, e = strToWeekdays(value)
case "BYHOUR":
result.Byhour, e = strToInts(value)
case "BYMINUTE":
result.Byminute, e = strToInts(value)
case "BYSECOND":
result.Bysecond, e = strToInts(value)
case "BYEASTER":
result.Byeaster, e = strToInts(value)
default:
return nil, errors.New("unknown RRULE property: " + key)
}
if e != nil {
return nil, e
}
}
return &result, nil
}
func (r *RRule) String() string {
return r.OrigOptions.String()
}
func (s *Set) String() string {
return strings.Join(s.spec(), "\n")
}
func (s *Set) spec() []string {
ret := make([]string, 0, len(s.rrule)+len(s.rdate)+len(s.exrule)+len(s.exdate))
for _, r := range s.rrule {
ret = append(ret, "RRULE:"+r.String())
}
for _, r := range s.rdate {
ret = append(ret, "RDATE:"+timeToStr(r))
}
for _, r := range s.exrule {
ret = append(ret, "EXRULE:"+r.String())
}
for _, r := range s.exdate {
ret = append(ret, "EXDATE:"+timeToStr(r))
}
return ret
}
// StrToRRule converts string to RRule
func StrToRRule(rfcString string) (*RRule, error) {
option, e := StrToROption(rfcString)
if e != nil {
return nil, e
}
return NewRRule(*option)
}
// StrToRRuleSet converts string to RRuleSet
func StrToRRuleSet(s string) (*Set, error) {
s = strings.TrimSpace(strings.ToUpper(s))
if s == "" {
return nil, errors.New("empty string")
}
set := Set{}
for _, line := range strings.Split(s, "\n") {
err := set.add(line)
if err != nil {
return nil, err
}
}
return &set, nil
}