mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
Using cron-parse lib to parse the cron expression. Cron-parse can handle with more scenarios.
This commit is contained in:
parent
e56164aa5a
commit
7c7a6df6e4
@ -16,6 +16,7 @@
|
|||||||
"@nuxtjs/axios": "^5.13.6",
|
"@nuxtjs/axios": "^5.13.6",
|
||||||
"@nuxtjs/proxy": "^2.1.0",
|
"@nuxtjs/proxy": "^2.1.0",
|
||||||
"core-js": "^3.16.0",
|
"core-js": "^3.16.0",
|
||||||
|
"cron-parser": "^4.7.1",
|
||||||
"date-fns": "^2.25.0",
|
"date-fns": "^2.25.0",
|
||||||
"epubjs": "^0.3.88",
|
"epubjs": "^0.3.88",
|
||||||
"hls.js": "^1.0.7",
|
"hls.js": "^1.0.7",
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
|
import cronParser from 'cron-parser'
|
||||||
|
|
||||||
Vue.prototype.$bytesPretty = (bytes, decimals = 2) => {
|
Vue.prototype.$bytesPretty = (bytes, decimals = 2) => {
|
||||||
if (isNaN(bytes) || bytes == 0) {
|
if (isNaN(bytes) || bytes == 0) {
|
||||||
@ -137,44 +138,8 @@ Vue.prototype.$parseCronExpression = (expression) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Vue.prototype.$getNextScheduledDate = (expression) => {
|
Vue.prototype.$getNextScheduledDate = (expression) => {
|
||||||
const cronFields = expression.split(' ');
|
const interval = cronParser.parseExpression(expression);
|
||||||
const currentDate = new Date();
|
return interval.next().toDate()
|
||||||
const nextDate = new Date();
|
|
||||||
|
|
||||||
// Calculate next minute
|
|
||||||
const minute = cronFields[0];
|
|
||||||
const currentMinute = currentDate.getMinutes();
|
|
||||||
const nextMinute = getNextValue(minute, currentMinute, 59);
|
|
||||||
nextDate.setMinutes(nextMinute);
|
|
||||||
|
|
||||||
// Calculate next hour
|
|
||||||
const hour = cronFields[1];
|
|
||||||
const currentHour = currentDate.getHours();
|
|
||||||
const nextHour = getNextValue(hour, currentHour, 23);
|
|
||||||
nextDate.setHours(nextHour);
|
|
||||||
|
|
||||||
// Calculate next day of month
|
|
||||||
const dayOfMonth = cronFields[2];
|
|
||||||
const currentDayOfMonth = currentDate.getDate();
|
|
||||||
const lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0).getDate();
|
|
||||||
const nextDayOfMonth = getNextValue(dayOfMonth, currentDayOfMonth, lastDayOfMonth);
|
|
||||||
nextDate.setDate(nextDayOfMonth);
|
|
||||||
|
|
||||||
// Calculate next month
|
|
||||||
const month = cronFields[3];
|
|
||||||
const currentMonth = currentDate.getMonth() + 1;
|
|
||||||
const nextMonth = getNextValue(month, currentMonth, 12);
|
|
||||||
nextDate.setMonth(nextMonth - 1);
|
|
||||||
|
|
||||||
// Calculate next day of week
|
|
||||||
const dayOfWeek = cronFields[4];
|
|
||||||
const currentDayOfWeek = currentDate.getDay();
|
|
||||||
const nextDayOfWeek = getNextValue(dayOfWeek, currentDayOfWeek, 6);
|
|
||||||
const daysUntilNextDayOfWeek = getNextDaysUntilDayOfWeek(currentDate, nextDayOfWeek);
|
|
||||||
nextDate.setDate(currentDate.getDate() + daysUntilNextDayOfWeek);
|
|
||||||
|
|
||||||
// Return the next scheduled date
|
|
||||||
return nextDate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function supplant(str, subs) {
|
export function supplant(str, subs) {
|
||||||
@ -186,25 +151,3 @@ export function supplant(str, subs) {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNextValue(cronField, currentValue, maxValue) {
|
|
||||||
if (cronField === '*') {
|
|
||||||
return currentValue + 1 <= maxValue ? currentValue + 1 : 0;
|
|
||||||
}
|
|
||||||
const values = cronField.split(',');
|
|
||||||
const len = values.length;
|
|
||||||
let nextValue = parseInt(values[0]);
|
|
||||||
for (let i = 0; i < len; i++) {
|
|
||||||
const value = parseInt(values[i]);
|
|
||||||
if (value > currentValue) {
|
|
||||||
nextValue = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nextValue <= maxValue ? nextValue : parseInt(values[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getNextDaysUntilDayOfWeek(currentDate, nextDayOfWeek) {
|
|
||||||
const daysUntilNextDayOfWeek = (nextDayOfWeek + 7 - currentDate.getDay()) % 7;
|
|
||||||
return daysUntilNextDayOfWeek === 0 ? 7 : daysUntilNextDayOfWeek;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user