diff --git a/src/cron.ts b/src/cron.ts index a5bc87a..4ad7c48 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -18,6 +18,7 @@ export interface MongoCronCfg { intervalFieldPath?: string; repeatUntilFieldPath?: string; autoRemoveFieldPath?: string; + timezone?: string; onDocument?(doc: any): (any | Promise); onStart?(doc: any): (any | Promise); onStop?(): (any | Promise); @@ -25,6 +26,18 @@ export interface MongoCronCfg { onError?(err: any): (any | Promise); } +/** + * Copy of the ParserOptions interface from cron-parser. + */ +interface ParserOptions { + currentDate?: string | number | Date; + startDate?: string | number | Date; + endDate?: string | number | Date; + iterator?: boolean; + utc?: boolean; + tz?: string; +} + /** * Main class for converting a collection into cron. */ @@ -33,6 +46,7 @@ export class MongoCron { protected processing = false; protected idle = false; protected readonly config: MongoCronCfg; + protected readonly parserOptions: ParserOptions; /** * Class constructor. @@ -52,6 +66,16 @@ export class MongoCron { autoRemoveFieldPath: 'autoRemove', ...config, }; + if (!this.config.timezone) { + this.parserOptions = { + utc: true, + }; + } else { + this.parserOptions = { + utc: false, + tz: this.config.timezone, + }; + } } /** @@ -187,6 +211,7 @@ export class MongoCron { try { const interval = parser.parseExpression(dot.pick(this.config.intervalFieldPath, doc), { + ...this.parserOptions, currentDate: future.toDate(), endDate: dot.pick(this.config.repeatUntilFieldPath, doc), });