From 230a9178e1e1c26856c4326f64eaeb7ff98f3116 Mon Sep 17 00:00:00 2001 From: Pierre Cavin Date: Mon, 25 Sep 2023 18:08:25 +0200 Subject: [PATCH] fix: restore legacy behavior after merging #685 --- src/time.ts | 24 +++++++++++++++--------- tests/cron.test.ts | 22 ++++++++++------------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/time.ts b/src/time.ts index 20332a59..963e7b29 100644 --- a/src/time.ts +++ b/src/time.ts @@ -24,7 +24,7 @@ import { getRecordKeys } from './utils'; export class CronTime { source: string | DateTime; zone?: string; - utcOffset?: number; + utcOffset?: number | string; realDate = false; private second: TimeUnitField<'second'> = {}; @@ -49,8 +49,7 @@ export class CronTime { } if (utcOffset != null) { - this.utcOffset = - typeof utcOffset === 'string' ? parseInt(utcOffset) : utcOffset; + this.utcOffset = utcOffset; } if (source instanceof Date || source instanceof DateTime) { @@ -127,19 +126,26 @@ export class CronTime { date = date.setZone(this.zone); } - if (typeof this.utcOffset !== 'undefined') { - const offsetHours = + if (this.utcOffset != null) { + const offsetHours = parseInt( + // @ts-expect-error old undocumented behavior going to be removed in V3 this.utcOffset >= 60 || this.utcOffset <= -60 - ? this.utcOffset / 60 - : this.utcOffset; + ? // @ts-expect-error old undocumented behavior going to be removed in V3 + this.utcOffset / 60 + : this.utcOffset + ); + const offsetMins = + // @ts-expect-error old undocumented behavior going to be removed in V3 this.utcOffset >= 60 || this.utcOffset <= -60 - ? Math.abs(this.utcOffset - offsetHours * 60) + ? // @ts-expect-error old undocumented behavior going to be removed in V3 + Math.abs(this.utcOffset - offsetHours * 60) : 0; - const offsetMinsStr = offsetMins >= 10 ? offsetMins : '0' + offsetMins; + const offsetMinsStr = offsetMins >= 10 ? offsetMins : `0${offsetMins}`; let utcZone = 'UTC'; + // @ts-expect-error old undocumented behavior going to be removed in V3 if (this.utcOffset < 0) { utcZone += `${offsetHours === 0 ? '-0' : offsetHours}:${offsetMinsStr}`; } else { diff --git a/tests/cron.test.ts b/tests/cron.test.ts index 1534e10f..f996592a 100644 --- a/tests/cron.test.ts +++ b/tests/cron.test.ts @@ -438,14 +438,13 @@ describe('cron', () => { it('should run a job using cron syntax with a "UTC+HH:mm" offset as timezone', () => { const clock = sinon.useFakeTimers(); const callback = jest.fn(); - const luxon = require('luxon'); // Current time - const d = luxon.DateTime.local(); + const d = DateTime.local(); // Current time with zone offset let zone = 'UTC+5:30'; - let t = luxon.DateTime.local().setZone(zone); + let t = DateTime.local().setZone(zone); // If current offset is UTC+5:30, switch to UTC+6:30.. if (t.hour === d.hour && t.minute === d.minute) { @@ -461,8 +460,8 @@ describe('cron', () => { // Run a job designed to be executed at a given // time in `zone`, making sure that it is a different // hour than local time. - const job = new cron.CronJob( - t.second + ' ' + t.minute + ' ' + t.hour + ' * * *', + const job = new CronJob( + `${t.second} ${t.minute} ${t.hour} * * *`, callback, null, true, @@ -868,9 +867,8 @@ describe('cron', () => { it('should run a job using cron syntax with numeric format utcOffset with minute support', () => { const clock = sinon.useFakeTimers(); const callback = jest.fn(); - const luxon = require('luxon'); // Current time - const t = luxon.DateTime.local(); + const t = DateTime.local(); /** * in order to avoid the minute offset being treated as hours (when `-60 < utcOffset < 60`) regardless of the local timezone, @@ -883,8 +881,8 @@ describe('cron', () => { // UTC Offset decreased by minutesOffset const utcOffset = t.offset - minutesOffset; - const job = new cron.CronJob( - t.second + ' ' + t.minute + ' ' + t.hour + ' * * *', + const job = new CronJob( + `${t.second} ${t.minute} ${t.hour} * * *`, callback, null, true, @@ -919,9 +917,9 @@ describe('cron', () => { // We support only HH support in offset as we support string offset in Timezone. const minutesOffset = t.offset - Math.floor((t.offset - 60) / 60) * 60; const utcOffset = t.offset - minutesOffset; - const utcOffsetString = `${utcOffset > 0 ? '+' : '-'}${( - '0' + Math.floor(Math.abs(utcOffset) / 60) - ).slice(-2)}:${('0' + (utcOffset % 60)).slice(-2)}`; + const utcOffsetString = `${utcOffset > 0 ? '+' : '-'}${`0${Math.floor( + Math.abs(utcOffset) / 60 + )}`.slice(-2)}:${`0${utcOffset % 60}`.slice(-2)}`; const job = new CronJob( `${t.second} ${t.minute} ${t.hour} * * *`,