@opengis/fastify-table
Version:
core-plugins
37 lines (36 loc) • 1.54 kB
JavaScript
const intervalStringMs = {
everyMin: 1000 * 60,
tenMin: 1000 * 60 * 10,
everyHour: 1000 * 60 * 60,
isHalfday: 1000 * 60 * 60 * 12,
dailyHour: 1000 * 60 * 60 * 24,
};
const interval2ms = {
string: (interval) => {
const date = new Date();
const intervarSplit = interval.match(/^(\*{2}|(\*)?(\d{1,2})):(\*(\d)|(\d{2}))/);
if (!intervarSplit) {
throw new Error(`interval ${interval} not suported`);
}
const [, , isHalfday, dailyHour, , tenMin, HourlyMin] = intervarSplit;
const intervalMs = (isHalfday && intervalStringMs.isHalfday) ||
(dailyHour && intervalStringMs.dailyHour) ||
(tenMin && intervalStringMs.tenMin) ||
intervalStringMs.everyHour;
const offsetDay = (+(dailyHour || 0) * 60 + +(tenMin || HourlyMin || 0)) * 60 * 1000;
const offsetCur = (date.getTime() - date.getTimezoneOffset() * 1000 * 60) % intervalMs;
const waitMs = (offsetDay - offsetCur + intervalMs) % intervalMs;
return [waitMs, intervalMs];
},
number: (interval) => {
const date = new Date();
const intervalMs = interval * 1000;
const dateWithTZ = date.getTime() - date.getTimezoneOffset() * 1000 * 60;
const offsetCur = dateWithTZ % intervalMs;
// start every cron within 1 hour
const sixtyMinutesStartMs = 3600000;
const waitMs = (intervalMs - offsetCur) % sixtyMinutesStartMs;
return [waitMs, intervalMs];
},
};
export default interval2ms;