harrisiirak/cron-parser

Feature Request: interval.includes

MichaelLeeHobbs opened this issue · 0 comments

/**
 * Returns true if the dt is within the cron range
 * @param {Date|string} [dt=(new Date()).toISOString()] - The date to check or an ISO string that can be parsed by luxon
 * @returns {boolean}
 */
CronExpression.prototype.includes = function (dt) {
  dt ??= (new Date()).toISOString()
  let _dt = DateTime.fromISO(dt).setZone(this._options.tz)
  let {second, minute, hour, dayOfMonth, month, dayOfWeek} = this._fields
  return dayOfMonth.includes(_dt.day) && dayOfWeek.includes(_dt.weekday) && month.includes(_dt.month) && hour.includes(_dt.hour) && minute.includes(_dt.minute) && second.includes(_dt.second)
}
const interval = cronParser.parseExpression('* * 0-8,16-23 ? * MON,TUE,WED,THU,FRI', {tz: 'America/New_York'})
console.log(`Now with in the cron interval? ${interval.includes()}`)
console.log(`5:59 am PDT Los Angeles with in the cron interval? ${interval.includes('2022-12-01T05:59:59-0800')}`)
console.log(`6:00 am PDT Los Angeles with in the cron interval? ${interval.includes('2022-12-01T06:00:00-0800')}`)