-
Notifications
You must be signed in to change notification settings - Fork 0
/
nextrun_addweekdays.go
64 lines (53 loc) · 1.48 KB
/
nextrun_addweekdays.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
package goscheduler
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
days = 7
secsInDay = 86400
)
// addWeekdaysToDaysOfMonth will convert any weekdays to daysOfMonth format and add it to daysOfMonth list.
// Returns a combined string with daysOfMonth and weekdays.
func (cron *cron) addWeekdaysToDaysOfMonth(org *string) *string {
s := strings.Split(cron.normalized, columnSeparator)[4]
switch {
case *org == wildCard && s == wildCard:
w := wildCard
return &w
case s == wildCard:
return org
}
slice := []string{}
for _, val := range *cron.createSliceAndSort(&s) {
slice = append(slice, *(cron).addWeekdays(&val))
}
if *org == wildCard {
str := strings.Join(slice, ",")
return &str
}
str := fmt.Sprintf("%v,%v", *org, strings.Join(slice, ","))
return &str
}
// addWeekdays will calculate which dayOfMonth the specified weekday is.
func (cron *cron) addWeekdays(to *int) *string {
date := *cron.next.time
today := int(cron.next.time.Weekday())
diff := *to - today
// If we started from a new month set current day to 01.
// Since we need to start parsing from day 01 in these cases.
if cron.next.future {
if cron.next.futureMonth {
date, _ = time.ParseInLocation("2006-01-02", fmt.Sprintf("%v-%v-01", cron.next.year, cron.convertAndAppendMonth()), time.Local)
}
today = int(date.Weekday())
diff = *to - today
}
if diff < 0 {
diff = days + diff
}
str := strconv.Itoa(date.Add(time.Duration(secsInDay*(diff)) * time.Second).Day())
return &str
}