-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpattern.go
61 lines (52 loc) · 1.21 KB
/
pattern.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
package recurrence
import (
"time"
)
type Frequence int
const (
NotRepeating Frequence = iota
// Repeat every X days
Daily
// Repeat every X weeks on some of the Week-Days
Weekly
// Repeat every X months on the n-th day of the month
MonthlyXth
// Repeat every X months on the n-th weekday of the month
Monthly
// Repeat every X years
Yearly
)
func WeeklyPatternToInt(firstDayOfWeek time.Weekday, days ...time.Weekday) int {
result := 0
for _, d := range days {
result = result | (1 << uint(d))
}
return result | int(firstDayOfWeek<<8)
}
func IntToWeeklyPattern(value int) (time.Weekday, []time.Weekday) {
result := make([]time.Weekday, 0, 7)
for i := time.Sunday; i <= time.Saturday; i++ {
test := 1 << uint(i)
if value&test == test {
result = append(result, i)
}
}
firstDay := time.Weekday(value >> 8)
return firstDay, result
}
type Occurrence byte
const (
First Occurrence = iota
Second
Third
Fourth
Last
)
func MonthlyPatternToInt(occ Occurrence, weekDay time.Weekday) int {
return ((int(occ) & 255) << 8) | (int(weekDay) & 255)
}
func IntToMonthlyPattern(value int) (occ Occurrence, weekDay time.Weekday) {
weekDay = time.Weekday(value & 255)
occ = Occurrence((value >> 8) & 255)
return
}