Improve dates, times and schedule backup info

This commit is contained in:
mfcar 2023-02-27 18:04:26 +00:00
parent 97b5cf04f5
commit 071444a9e7
No known key found for this signature in database
28 changed files with 295 additions and 53 deletions

View file

@ -136,6 +136,47 @@ 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;
}
export function supplant(str, subs) {
// source: http://crockford.com/javascript/remedial.html
return str.replace(/{([^{}]*)}/g,
@ -144,4 +185,26 @@ export function supplant(str, subs) {
return typeof r === 'string' || typeof r === 'number' ? r : a
}
)
}
}
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;
}