UNPKG

aws-cron-parser

Version:

A util to parse cron expressions used by AWS services

63 lines 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prev = void 0; var common_1 = require("./common"); var iter; var findOnce = function (parsed, from) { if (iter > 10) { throw new Error("AwsCronParser : this shouldn't happen, but iter > 10"); } iter += 1; var cYear = from.getUTCFullYear(); var cMonth = from.getUTCMonth() + 1; var cDayOfMonth = from.getUTCDate(); var cHour = from.getUTCHours(); var cMinute = from.getUTCMinutes(); var year = common_1.arrayFindLast(parsed.years, function (c) { return c <= cYear; }); if (!year) { return null; } var month = common_1.arrayFindLast(parsed.months, function (c) { return c <= (year === cYear ? cMonth : 12); }); if (!month) { return findOnce(parsed, new Date(Date.UTC(year, 0) - 1)); } var isSameMonth = year === cYear && month === cMonth; var pDaysOfMonth = parsed.daysOfMonth; if (pDaysOfMonth.length === 0) { pDaysOfMonth = common_1.getDaysOfMonthFromDaysOfWeek(year, month, parsed.daysOfWeek); } else if (pDaysOfMonth[0] === 'L') { pDaysOfMonth = common_1.getDaysOfMonthForL(year, month, pDaysOfMonth[1]); } else if (pDaysOfMonth[0] === 'W') { pDaysOfMonth = common_1.getDaysOfMonthForW(year, month, pDaysOfMonth[1]); } var dayOfMonth = common_1.arrayFindLast(pDaysOfMonth, function (c) { return c <= (isSameMonth ? cDayOfMonth : 31); }); if (!dayOfMonth) { return findOnce(parsed, new Date(Date.UTC(year, month - 1) - 1)); } var isSameDate = isSameMonth && dayOfMonth === cDayOfMonth; var hour = common_1.arrayFindLast(parsed.hours, function (c) { return c <= (isSameDate ? cHour : 23); }); if (typeof hour === 'undefined') { return findOnce(parsed, new Date(Date.UTC(year, month - 1, dayOfMonth) - 1)); } var minute = common_1.arrayFindLast(parsed.minutes, function (c) { return c <= (isSameDate && hour === cHour ? cMinute : 59); }); if (typeof minute === 'undefined') { return findOnce(parsed, new Date(Date.UTC(year, month - 1, dayOfMonth, hour) - 1)); } return new Date(Date.UTC(year, month - 1, dayOfMonth, hour, minute)); }; /** * generate the next occurrence BEFORE the "from" date value * returns NULL when there is no more future occurrence * @param {*} parsed the value returned by "parse" function of this module * @param {*} from the Date to start from */ function prev(parsed, from) { // iter is just a safety net to prevent infinite recursive calls // because I'm not 100% sure this won't happen iter = 0; return findOnce(parsed, new Date((Math.ceil(from.getTime() / 60000) - 1) * 60000)); } exports.prev = prev; //# sourceMappingURL=prev.js.map