Created
November 18, 2022 15:46
-
-
Save jenol/2c75fc1b23b5a2f0975052f39bd1336c to your computer and use it in GitHub Desktop.
scheduling_dsl_idea.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# ┌───────────── start minute (0 - 59) | |
# │ ┌───────────── start hour (0 - 23) | |
# │ │ ┌──────────── end minute (0 - 59) | |
# │ │ │ ┌───────────── end hour (0 - 23) | |
# │ │ │ │ ┌───────────── day of the month (1 - 31) | |
# │ │ │ │ │ ┌───────────── month (1 - 12) | |
# │ │ │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday) | |
# │ │ │ │ │ │ │ | |
# │ │ │ │ │ │ │ | |
# * * * * * * * <command to execute> | |
*/ | |
const testDsl = [ | |
// createDsl(11, 6, 2, 22, 3, 33), | |
// createDsl(11, 6, 3, 22, 3, 33), | |
// createDsl("*", 6, 3, 22, 3, 33), // every month | |
// createDsl(11, "*", 3, 22, 3, 33), // every day | |
createDsl(11, "*", 3, 22, 3, 33, "0,4"), // only on Mon and Fri | |
] | |
function createDsl(month, dayOfMonth, startHour, startMinute, endHour, endMinute, dayOfWeek) { | |
return `${startMinute} ${startHour} ${endMinute} ${endHour}` + | |
` ${dayOfMonth} ${month} ${dayOfWeek === undefined ? "*" : dayOfWeek}` | |
} | |
function *getRange(dsl, startDate, endDate) { | |
const parts = dsl.split(" "); | |
const startYear = startDate.getFullYear() | |
const endYear = endDate.getFullYear() | |
const monthParam = parts[5] | |
const dayOfMonthParam = parts[4] | |
const dayOfWeekParam = parts[6] | |
const processDays = function*(year, month, day) { | |
const dayOfWeek = new Date(year, month, day).getDay() | |
const selectedDays = dayOfWeekParam === "*" ? [] : dayOfWeekParam | |
.split(",") | |
.map(d => parseInt(d.trim())) | |
.filter(d => !isNaN(d)) | |
if (selectedDays.length === 0 || selectedDays.find(d => d === dayOfWeek)) { | |
const startTime = new Date(year, month, day, parseInt(parts[1]) + 1, parseInt(parts[0])) | |
const endTime = new Date(year, month, day, parseInt(parts[3]) + 1, parseInt(parts[2])) | |
yield [startTime, endTime] | |
} | |
} | |
const processMonths = function*(year, month) { | |
if (dayOfMonthParam === "*") { | |
const daysInMonth = new Date(year, month, 0).getDate() | |
for (let day = 1; day < daysInMonth; day ++) { | |
yield* processDays(year, month, day) | |
} | |
} else { | |
yield* processDays(year, month, parseInt(dayOfMonthParam)) | |
} | |
} | |
for (let year = startYear; year <= endYear; year ++) { | |
if (monthParam === "*") { | |
for (let month = 0; month < 12; month ++) { | |
yield* processMonths(year, month) | |
} | |
} else { | |
yield* processMonths(year, parseInt(monthParam) - 1) | |
} | |
} | |
} | |
for (let dsl of testDsl) { | |
console.log(dsl) | |
for (let range of getRange(dsl, new Date(2001, 1, 1), new Date(2001, 1, 1))) { | |
console.log(range) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment