forked from teambition/rrule-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
203 lines (176 loc) · 5.42 KB
/
example_test.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
// 2017-2022, Teambition. All rights reserved.
package rrule_test
import (
"fmt"
"time"
"github.com/teambition/rrule-go"
)
func printTimeSlice(ts []time.Time) {
for _, t := range ts {
fmt.Println(t)
}
}
func ExampleRRule() {
// Daily, for 10 occurrences.
r, _ := rrule.NewRRule(rrule.ROption{
Freq: rrule.DAILY,
Count: 10,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC),
})
fmt.Println(r.String())
// DTSTART:19970902T090000Z
// RRULE:FREQ=DAILY;COUNT=10
printTimeSlice(r.All())
// 1997-09-02 09:00:00 +0000 UTC
// 1997-09-03 09:00:00 +0000 UTC
// ...
// 1997-09-07 09:00:00 +0000 UTC
printTimeSlice(r.Between(
time.Date(1997, 9, 6, 0, 0, 0, 0, time.UTC),
time.Date(1997, 9, 8, 0, 0, 0, 0, time.UTC), true))
// [1997-09-06 09:00:00 +0000 UTC
// 1997-09-07 09:00:00 +0000 UTC]
// Every four years, the first Tuesday after a Monday in November, 3 occurrences (U.S. Presidential Election day).
r, _ = rrule.NewRRule(rrule.ROption{
Freq: rrule.YEARLY,
Interval: 4,
Count: 3,
Bymonth: []int{11},
Byweekday: []rrule.Weekday{rrule.TU},
Bymonthday: []int{2, 3, 4, 5, 6, 7, 8},
Dtstart: time.Date(1996, 11, 5, 9, 0, 0, 0, time.UTC),
})
fmt.Println(r.String())
// DTSTART:19961105T090000Z
// RRULE:FREQ=YEARLY;INTERVAL=4;COUNT=3;BYMONTH=11;BYMONTHDAY=2,3,4,5,6,7,8;BYDAY=TU
printTimeSlice(r.All())
// 1996-11-05 09:00:00 +0000 UTC
// 2000-11-07 09:00:00 +0000 UTC
// 2004-11-02 09:00:00 +0000 UTC
// Output:
// DTSTART:19970902T090000Z
// RRULE:FREQ=DAILY;COUNT=10
// 1997-09-02 09:00:00 +0000 UTC
// 1997-09-03 09:00:00 +0000 UTC
// 1997-09-04 09:00:00 +0000 UTC
// 1997-09-05 09:00:00 +0000 UTC
// 1997-09-06 09:00:00 +0000 UTC
// 1997-09-07 09:00:00 +0000 UTC
// 1997-09-08 09:00:00 +0000 UTC
// 1997-09-09 09:00:00 +0000 UTC
// 1997-09-10 09:00:00 +0000 UTC
// 1997-09-11 09:00:00 +0000 UTC
// 1997-09-06 09:00:00 +0000 UTC
// 1997-09-07 09:00:00 +0000 UTC
// DTSTART:19961105T090000Z
// RRULE:FREQ=YEARLY;INTERVAL=4;COUNT=3;BYMONTH=11;BYMONTHDAY=2,3,4,5,6,7,8;BYDAY=TU
// 1996-11-05 09:00:00 +0000 UTC
// 2000-11-07 09:00:00 +0000 UTC
// 2004-11-02 09:00:00 +0000 UTC
}
func ExampleSet() {
// Daily, for 7 days, jumping Saturday and Sunday occurrences.
set := rrule.Set{}
r, _ := rrule.NewRRule(rrule.ROption{
Freq: rrule.DAILY,
Count: 7,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
fmt.Println(set.String())
// DTSTART:19970902T090000Z
// RRULE:FREQ=DAILY;COUNT=7
printTimeSlice(set.All())
// 1997-09-02 09:00:00 +0000 UTC
// 1997-09-03 09:00:00 +0000 UTC
// 1997-09-04 09:00:00 +0000 UTC
// 1997-09-05 09:00:00 +0000 UTC
// 1997-09-06 09:00:00 +0000 UTC
// 1997-09-07 09:00:00 +0000 UTC
// 1997-09-08 09:00:00 +0000 UTC
// Weekly, for 4 weeks, plus one time on day 7, and not on day 16.
set = rrule.Set{}
r, _ = rrule.NewRRule(rrule.ROption{
Freq: rrule.WEEKLY,
Count: 4,
Dtstart: time.Date(1997, 9, 2, 9, 0, 0, 0, time.UTC)})
set.RRule(r)
set.RDate(time.Date(1997, 9, 7, 9, 0, 0, 0, time.UTC))
set.ExDate(time.Date(1997, 9, 16, 9, 0, 0, 0, time.UTC))
fmt.Println(set.String())
// DTSTART:19970902T090000Z
// RRULE:FREQ=WEEKLY;COUNT=4
// RDATE:19970907T090000Z
// EXDATE:19970916T090000Z
printTimeSlice(set.All())
// 1997-09-02 09:00:00 +0000 UTC
// 1997-09-07 09:00:00 +0000 UTC
// 1997-09-09 09:00:00 +0000 UTC
// 1997-09-23 09:00:00 +0000 UTC
// Output:
// DTSTART:19970902T090000Z
// RRULE:FREQ=DAILY;COUNT=7
// 1997-09-02 09:00:00 +0000 UTC
// 1997-09-03 09:00:00 +0000 UTC
// 1997-09-04 09:00:00 +0000 UTC
// 1997-09-05 09:00:00 +0000 UTC
// 1997-09-06 09:00:00 +0000 UTC
// 1997-09-07 09:00:00 +0000 UTC
// 1997-09-08 09:00:00 +0000 UTC
// DTSTART:19970902T090000Z
// RRULE:FREQ=WEEKLY;COUNT=4
// RDATE:19970907T090000Z
// EXDATE:19970916T090000Z
// 1997-09-02 09:00:00 +0000 UTC
// 1997-09-07 09:00:00 +0000 UTC
// 1997-09-09 09:00:00 +0000 UTC
// 1997-09-23 09:00:00 +0000 UTC
}
func ExampleStrToRRule() {
// Compatible with old DTSTART
r, _ := rrule.StrToRRule("FREQ=DAILY;DTSTART=20060101T150405Z;COUNT=5")
fmt.Println(r.OrigOptions.RRuleString())
// FREQ=DAILY;COUNT=5
fmt.Println(r.OrigOptions.String())
// DTSTART:20060101T150405Z
// RRULE:FREQ=DAILY;COUNT=5
fmt.Println(r.String())
// DTSTART:20060101T150405Z
// RRULE:FREQ=DAILY;COUNT=5
printTimeSlice(r.All())
// 2006-01-01 15:04:05 +0000 UTC
// 2006-01-02 15:04:05 +0000 UTC
// 2006-01-03 15:04:05 +0000 UTC
// 2006-01-04 15:04:05 +0000 UTC
// 2006-01-05 15:04:05 +0000 UTC
// Output:
// FREQ=DAILY;COUNT=5
// DTSTART:20060101T150405Z
// RRULE:FREQ=DAILY;COUNT=5
// DTSTART:20060101T150405Z
// RRULE:FREQ=DAILY;COUNT=5
// 2006-01-01 15:04:05 +0000 UTC
// 2006-01-02 15:04:05 +0000 UTC
// 2006-01-03 15:04:05 +0000 UTC
// 2006-01-04 15:04:05 +0000 UTC
// 2006-01-05 15:04:05 +0000 UTC
}
func ExampleStrToRRuleSet() {
s, _ := rrule.StrToRRuleSet("DTSTART:20060101T150405Z\nRRULE:FREQ=DAILY;COUNT=5\nEXDATE:20060102T150405Z")
fmt.Println(s.String())
// DTSTART:20060101T150405Z
// RRULE:FREQ=DAILY;COUNT=5
// EXDATE:20060102T150405Z
printTimeSlice(s.All())
// 2006-01-01 15:04:05 +0000 UTC
// 2006-01-03 15:04:05 +0000 UTC
// 2006-01-04 15:04:05 +0000 UTC
// 2006-01-05 15:04:05 +0000 UTC
// Output:
// DTSTART:20060101T150405Z
// RRULE:FREQ=DAILY;COUNT=5
// EXDATE:20060102T150405Z
// 2006-01-01 15:04:05 +0000 UTC
// 2006-01-03 15:04:05 +0000 UTC
// 2006-01-04 15:04:05 +0000 UTC
// 2006-01-05 15:04:05 +0000 UTC
}