diff --git a/src/job.ts b/src/job.ts index b1d8be75..09564130 100644 --- a/src/job.ts +++ b/src/job.ts @@ -10,6 +10,7 @@ import { CronOnCompleteCommand, WithOnComplete } from './types/cron.types'; +import { getTimeZoneAndOffset } from './utils'; export class CronJob { cronTime: CronTime; @@ -61,17 +62,12 @@ export class CronJob { this.context = (context ?? this) as CronContext; // runtime check for JS users - if (timeZone != null && utcOffset != null) { - throw new ExclusiveParametersError('timeZone', 'utcOffset'); - } + const { timeZone: tz, utcOffset: uo } = getTimeZoneAndOffset( + timeZone, + utcOffset + ); - if (timeZone != null) { - this.cronTime = new CronTime(cronTime, timeZone, null); - } else if (utcOffset != null) { - this.cronTime = new CronTime(cronTime, null, utcOffset); - } else { - this.cronTime = new CronTime(cronTime, timeZone, utcOffset); - } + this.cronTime = new CronTime(cronTime, tz, uo as null | undefined); if (unrefTimeout != null) { this.unrefTimeout = unrefTimeout; diff --git a/src/utils.ts b/src/utils.ts index 6f53350b..551c2e48 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,4 @@ +import { ExclusiveParametersError } from './errors'; import { Ranges } from './types/cron.types'; export const getRecordKeys = ( @@ -5,3 +6,20 @@ export const getRecordKeys = ( ) => { return Object.keys(record) as unknown as (keyof typeof record)[]; }; + +export const getTimeZoneAndOffset = ( + timeZone?: string | null, + utcOffset?: number | null +) => { + if (timeZone != null && utcOffset != null) { + throw new ExclusiveParametersError('timeZone', 'utcOffset'); + } + + if (timeZone != null) { + return { timeZone, utcOffset: null }; + } else if (utcOffset != null) { + return { timeZone: null, utcOffset }; + } else { + return { timeZone: null, utcOffset: null }; + } +};