From 7c7a6df6e45a82d55fda1c7bb4877d9f3d287dc5 Mon Sep 17 00:00:00 2001 From: mfcar Date: Tue, 28 Feb 2023 17:04:46 +0000 Subject: [PATCH] Using cron-parse lib to parse the cron expression. Cron-parse can handle with more scenarios. --- client/package.json | 3 +- client/plugins/utils.js | 63 ++--------------------------------------- 2 files changed, 5 insertions(+), 61 deletions(-) diff --git a/client/package.json b/client/package.json index 26c40356..65f95b28 100644 --- a/client/package.json +++ b/client/package.json @@ -16,6 +16,7 @@ "@nuxtjs/axios": "^5.13.6", "@nuxtjs/proxy": "^2.1.0", "core-js": "^3.16.0", + "cron-parser": "^4.7.1", "date-fns": "^2.25.0", "epubjs": "^0.3.88", "hls.js": "^1.0.7", @@ -36,4 +37,4 @@ "postcss": "^8.3.6", "tailwindcss": "^3.1.4" } -} \ No newline at end of file +} diff --git a/client/plugins/utils.js b/client/plugins/utils.js index 57982c0f..c9ece333 100644 --- a/client/plugins/utils.js +++ b/client/plugins/utils.js @@ -1,4 +1,5 @@ import Vue from 'vue' +import cronParser from 'cron-parser' Vue.prototype.$bytesPretty = (bytes, decimals = 2) => { if (isNaN(bytes) || bytes == 0) { @@ -137,44 +138,8 @@ Vue.prototype.$parseCronExpression = (expression) => { } Vue.prototype.$getNextScheduledDate = (expression) => { - const cronFields = expression.split(' '); - const currentDate = new Date(); - 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; + const interval = cronParser.parseExpression(expression); + return interval.next().toDate() } 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; -}