mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-04-19 21:49:43 +00:00
Update:Remove node-cron dependency
This commit is contained in:
parent
26ef275ab4
commit
b7e546f2f5
19 changed files with 686 additions and 18 deletions
54
server/libs/nodeCron/time-matcher.js
Normal file
54
server/libs/nodeCron/time-matcher.js
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
const validatePattern = require('./pattern-validation');
|
||||
const convertExpression = require('./convert-expression');
|
||||
|
||||
function matchPattern(pattern, value){
|
||||
if( pattern.indexOf(',') !== -1 ){
|
||||
const patterns = pattern.split(',');
|
||||
return patterns.indexOf(value.toString()) !== -1;
|
||||
}
|
||||
return pattern === value.toString();
|
||||
}
|
||||
|
||||
class TimeMatcher{
|
||||
constructor(pattern, timezone){
|
||||
validatePattern(pattern);
|
||||
this.pattern = convertExpression(pattern);
|
||||
this.timezone = timezone;
|
||||
this.expressions = this.pattern.split(' ');
|
||||
}
|
||||
|
||||
match(date){
|
||||
date = this.apply(date);
|
||||
|
||||
const runOnSecond = matchPattern(this.expressions[0], date.getSeconds());
|
||||
const runOnMinute = matchPattern(this.expressions[1], date.getMinutes());
|
||||
const runOnHour = matchPattern(this.expressions[2], date.getHours());
|
||||
const runOnDay = matchPattern(this.expressions[3], date.getDate());
|
||||
const runOnMonth = matchPattern(this.expressions[4], date.getMonth() + 1);
|
||||
const runOnWeekDay = matchPattern(this.expressions[5], date.getDay());
|
||||
|
||||
return runOnSecond && runOnMinute && runOnHour && runOnDay && runOnMonth && runOnWeekDay;
|
||||
}
|
||||
|
||||
apply(date){
|
||||
if(this.timezone){
|
||||
const dtf = new Intl.DateTimeFormat('en-US', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
hourCycle: 'h23',
|
||||
fractionalSecondDigits: 3,
|
||||
timeZone: this.timezone
|
||||
});
|
||||
|
||||
return new Date(dtf.format(date));
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TimeMatcher;
|
||||
Loading…
Add table
Add a link
Reference in a new issue