forked from VioletSY/cron-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.js
77 lines (68 loc) · 2.46 KB
/
scheduler.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
import SimpleTime from './SimpleTime';
import Cron from './Cron-es6';
export function getCronExpression(onceTime, cron, startTime, cycle, hourFlag, minuteFlag, interValDay, endTime, hourInterval, minuteInterval, weekArray, MonthArray, day) {
let cronExpression = "";
if (cycle == "1") {
cronExpression = getCronByStartTime(onceTime);
} else if (cycle == "5") {
cronExpression = getCronBySelf(cron);
} else {
cronExpression = getCronByTimePoint(startTime, cycle, hourFlag, minuteFlag, interValDay, endTime, hourInterval, minuteInterval, weekArray, MonthArray, day);
}
return cronExpression;
}
//onlyOne
function getCronByStartTime (startTime) {
const _startTime = new SimpleTime(startTime);
let cron = "";
cron = new Cron(_startTime);
return cron.toCron();
}
//selfDef
function getCronBySelf (cron) {
if (cron == null || cron == "") {
return;
}
return cron;
}
//getCronByTimePoint
function getCronByTimePoint (timePoint, planType, hourFlag, minuteFlag, interValDay, endTime, hourInt, minuteInt, weekDayArr, selectMonth, day) {
endTime = endTime || timePoint;
let cron = "";
if (planType == "2") {
cron = createCronByTimePoint(timePoint, hourFlag, minuteFlag, endTime, hourInt, minuteInt);
cron.set('day', 'setBase', '*');
cron.set('day', 'inc', interValDay + "");
} else if (planType == "3") {
let selectedDays = Cron.pack(weekDayArr);
if (selectedDays == "") {
selectedDays = "1-7";
}
cron = createCronByTimePoint(timePoint, hourFlag, minuteFlag, endTime, hourInt, minuteInt);
cron.set('weekDay', 'manual', selectedDays);
} else if (planType == "4") {
cron = createCronByTimePoint(timePoint, hourFlag, minuteFlag, endTime, hourInt, minuteInt);
cron.set('month', 'manual', selectMonth);
day == "-1" ? cron.set('day', 'last', 1) : cron.set('day',
'manual', day);
}
return cron.toCron();
}
function createCronByTimePoint (timePoint, hourFlag, minuteFlag, endTime, hourInt, minuteInt) {
const cron = new Cron(timePoint);
if (endTime) {
const timeAry = endTime.split(":");
if (timeAry[0].charAt(0) == "0") {
timeAry[0] = timeAry[0].charAt(1);
}
cron.set('hour', 'setBase', timePoint.split(":")[0].replace(/^0/g,
"")
+ "-" + timeAry[0]);
}
if (minuteFlag) {
cron.set('minute', 'inc', minuteInt);
} else if (hourFlag) {
cron.set('hour', 'inc', hourInt);
}
return cron;
}