Skip to content

Commit

Permalink
fix: restore legacy behavior after merging kelektiv#685
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Sep 25, 2023
1 parent 6ab4d0b commit 230a917
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
24 changes: 15 additions & 9 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'> = {};
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 10 additions & 12 deletions tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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} * * *`,
Expand Down

0 comments on commit 230a917

Please sign in to comment.