Skip to content

Commit

Permalink
fix(core): Ensure waiting executions account for workflow timezone (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Jan 16, 2024
1 parent d4c93b1 commit 3734c89
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"file-type": "16.5.4",
"form-data": "4.0.0",
"lodash": "4.17.21",
"luxon": "^3.4.4",
"mime-types": "2.1.35",
"n8n-workflow": "workspace:*",
"oauth-1.0a": "2.2.6",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ import Container from 'typedi';
import type { BinaryData } from './BinaryData/types';
import merge from 'lodash/merge';
import { InstanceSettings } from './InstanceSettings';
import { toUtcDate } from './utils';

axios.defaults.timeout = 300000;
// Prevent axios from adding x-form-www-urlencoded headers by default
Expand Down Expand Up @@ -3489,7 +3490,7 @@ export function getExecuteFunctions(
binaryToBuffer: async (body: Buffer | Readable) =>
Container.get(BinaryDataService).toBuffer(body),
async putExecutionToWait(waitTill: Date): Promise<void> {
runExecutionData.waitTill = waitTill;
runExecutionData.waitTill = toUtcDate(waitTill, getTimezone(workflow));
if (additionalData.setExecutionStatus) {
additionalData.setExecutionStatus('waiting');
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DateTime } from 'luxon';

export function toUtcDate(datetime: Date, tz: string) {
return DateTime.fromISO(datetime.toISOString().slice(0, -1), { zone: tz }).toUTC().toJSDate();
}
35 changes: 35 additions & 0 deletions packages/core/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { toUtcDate } from '@/utils';

describe('utils', () => {
describe('toUtcDate()', () => {
test('should convert to UTC date by adding', () => {
const originalDate = new Date('2020-01-01T00:00:00.000Z');
const timezone = 'America/New_York'; // +5 to reach Z

const utcDate = toUtcDate(originalDate, timezone);

expect(utcDate).toBeInstanceOf(Date);
expect(utcDate.toISOString()).toBe('2020-01-01T05:00:00.000Z');
});

test('should convert to UTC date by subtracting', () => {
const originalDate = new Date('2020-01-01T00:00:00.000Z');
const timezone = 'Europe/Paris'; // -1 to reach Z

const utcDate = toUtcDate(originalDate, timezone);

expect(utcDate).toBeInstanceOf(Date);
expect(utcDate.toISOString()).toBe('2019-12-31T23:00:00.000Z');
});

test('should convert to UTC date when already UTC', () => {
const originalDate = new Date('2020-01-01T00:00:00.000Z');
const timezone = 'UTC'; // already at Z

const utcDate = toUtcDate(originalDate, timezone);

expect(utcDate).toBeInstanceOf(Date);
expect(utcDate.toISOString()).toBe('2020-01-01T00:00:00.000Z');
});
});
});
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3734c89

Please sign in to comment.