-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronadd.js
145 lines (121 loc) · 6.15 KB
/
cronadd.js
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
var CombineCrons = function (cronBegin, cronAdd) {
//Set the increment and explode the initial cron value
var increment = 0;
cronAdd = cronAdd.split(" ");
var AddCrons = function (cronStart, timeType) {
if (cronAdd[timeType] != "*" && cronStart != "*") {
var timeAdded;
var timesDivisible = 0;
var divisor = 0;
//Minute
if (timeType == 0) {
divisor = 60;
timeAdded = (+cronStart + +cronAdd[timeType]) % divisor;
if (((+cronStart + +cronAdd[timeType] - timeAdded) / divisor) >= 1)
increment = 1;
} //Hour
else if (timeType == 1) {
divisor = 24;
timeAdded = (+cronStart + +cronAdd[timeType]) % divisor;
if (((+cronStart + +cronAdd[timeType] - timeAdded) / divisor) >= 1)
increment = 1;
} //DotM
else if (timeType == 2) {
divisor = 32;
timeAdded = (+cronStart + +cronAdd[timeType]) % divisor;
if (((+cronStart + +cronAdd[timeType] - timeAdded) / divisor) >= 1)
increment = 1;
if (timeAdded < cronStart)
timeAdded++; //Can't be zero
} //Month
else if (timeType == 3) {
divisor = 13;
timeAdded = (+cronStart + +cronAdd[timeType]) % divisor;
if (((+cronStart + +cronAdd[timeType] - timeAdded) / divisor) >= 1)
increment = 1;
if (timeAdded < cronStart)
timeAdded++; //Can't be zero
} //DotW
else if (timeType == 4) {
divisor = 7;
timeAdded = (+cronStart + +cronAdd[timeType]) % divisor;
if (((+cronStart + +cronAdd[timeType] - timeAdded) / divisor) >= 1)
increment = 1;
}
else {
timeAdded = +cronStart + +cronAdd[timeType];
}
return timeAdded;
} else {
return cronStart;
};
};
var SplitCrons = function (cronStart, timeType) {
var cronStart_split = "";
if (cronStart.indexOf(" ") > -1) {
cronStart_split = cronStart.split(" ");
cronStart = "";
for (var count = 0; count < cronStart_split.length; count++) {
cronStart += SplitCrons(cronStart_split[count], count);
if (count != (cronStart_split.length - 1))
cronStart += " ";
if (increment > 0) {
//Minute
if (count == 0) {
if (cronAdd[count + 1] != "*")
cronAdd[count + 1] = +cronAdd[count + 1] + +increment;
else
cronAdd[count + 1] = +increment;
} //Hour
else if (count == 1) {
if (cronAdd[count + 1] != "*" && cronAdd[cronAdd.length - 1] != "*") {
cronAdd[count + 1] = +cronAdd[count + 1] + +increment;
cronAdd[cronAdd.length - 1] = +cronAdd[cronAdd.length - 1] + +increment;
} else if (cronAdd[count + 1] != "*") {
cronAdd[count + 1] = +cronAdd[count + 1] + +increment;
cronAdd[cronAdd.length - 1] = +increment;
} else if (cronAdd[cronAdd.length - 1] != "*") {
cronAdd[count + 1] = +increment;
cronAdd[cronAdd.length - 1] = +cronAdd[cronAdd.length - 1] + +increment;
} else {
cronAdd[count + 1] = +increment;
cronAdd[cronAdd.length - 1] = +increment;
}
} //DotM
else if (count == 2) {
if (cronAdd[count + 1] != "*")
cronAdd[count + 1] = +cronAdd[count + 1] + +increment;
else
cronAdd[count + 1] = +increment;
} //Month
else if (count == 3) {
//No year to add
} //DotW
else if (count == 4)
if (cronAdd[count - 1] != "*")
cronAdd[count - 1] = +cronAdd[count - 1] + +increment;
else
cronAdd[count - 1] = +increment;
};
increment = 0;
};
} else if (cronStart.indexOf(",") > -1) {
cronStart = cronStart.split(",");
for (var count = 0; count < cronStart.length; count++) {
cronStart[count] = SplitCrons(cronStart[count], timeType);
}
cronStart.sort(function (a, b) {return a - b;});
cronStart = cronStart.join(",");
} else if (cronStart.indexOf("-") > -1) {
cronStart = cronStart.split("-");
for (var count = 0; count < cronStart.length; count++) {
cronStart[count] = SplitCrons(cronStart[count], timeType);
}
cronStart = cronStart.join("-");
} else {
cronStart = AddCrons(cronStart, timeType);
};
return cronStart;
};
return SplitCrons(cronBegin);
};