mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Change moment to luxon for the rest of the operations
This commit is contained in:
parent
5297e68324
commit
e67d2d5854
|
@ -1,23 +1,20 @@
|
||||||
import type {
|
import type {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
ILoadOptionsFunctions,
|
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodePropertyOptions,
|
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeBaseDescription,
|
INodeTypeBaseDescription,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
import { NodeOperationError } from 'n8n-workflow';
|
|
||||||
|
|
||||||
import moment from 'moment-timezone';
|
|
||||||
import { CurrentDateDescription } from './CurrentDateDescription';
|
import { CurrentDateDescription } from './CurrentDateDescription';
|
||||||
import { AddToDateDescription } from './AddToDateDescription';
|
import { AddToDateDescription } from './AddToDateDescription';
|
||||||
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
|
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
|
||||||
import { FormatDateDescription } from './FormatDateDescription';
|
import { FormatDateDescription } from './FormatDateDescription';
|
||||||
import { RoundDateDescription } from './RoundDateDescription';
|
import { RoundDateDescription } from './RoundDateDescription';
|
||||||
import { GetTimeBetweenDatesDescription } from './GetTimeBetweenDates';
|
import { GetTimeBetweenDatesDescription } from './GetTimeBetweenDates';
|
||||||
import type { DateTime, DateTimeUnit, DurationUnit } from 'luxon';
|
import type { DateTimeUnit, DurationUnit } from 'luxon';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
import { ExtractDateDescription } from './ExtractDateDescription';
|
import { ExtractDateDescription } from './ExtractDateDescription';
|
||||||
import { parseDate } from './GenericFunctions';
|
import { parseDate } from './GenericFunctions';
|
||||||
|
|
||||||
|
@ -83,25 +80,6 @@ export class DateTimeV2 implements INodeType {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
methods = {
|
|
||||||
loadOptions: {
|
|
||||||
// Get all the timezones to display them to user so that he can
|
|
||||||
// select them easily
|
|
||||||
async getTimezones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
||||||
const returnData: INodePropertyOptions[] = [];
|
|
||||||
for (const timezone of moment.tz.names()) {
|
|
||||||
const timezoneName = timezone;
|
|
||||||
const timezoneId = timezone;
|
|
||||||
returnData.push({
|
|
||||||
name: timezoneName,
|
|
||||||
value: timezoneId,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return returnData;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: INodeExecutionData[] = [];
|
const returnData: INodeExecutionData[] = [];
|
||||||
|
@ -115,53 +93,41 @@ export class DateTimeV2 implements INodeType {
|
||||||
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
||||||
responseData.push(
|
responseData.push(
|
||||||
includeTime
|
includeTime
|
||||||
? { [outputFieldName]: moment.tz(workflowTimezone) }
|
? { [outputFieldName]: DateTime.now().setZone(workflowTimezone) }
|
||||||
: { [outputFieldName]: moment().startOf('day').tz(workflowTimezone) },
|
: {
|
||||||
|
[outputFieldName]: DateTime.now().setZone(workflowTimezone).startOf('day'),
|
||||||
|
},
|
||||||
);
|
);
|
||||||
} else if (operation === 'addToDate') {
|
} else if (operation === 'addToDate') {
|
||||||
const addToDate = this.getNodeParameter('addToDate', i) as string;
|
const addToDate = this.getNodeParameter('addToDate', i) as string;
|
||||||
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
|
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
|
||||||
const duration = this.getNodeParameter('duration', i) as number;
|
const duration = this.getNodeParameter('duration', i) as number;
|
||||||
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
||||||
try {
|
const dateToAdd = parseDate.call(this, addToDate, workflowTimezone);
|
||||||
const dateToAdd = moment.tz(addToDate, workflowTimezone);
|
const returnedDate = dateToAdd.plus({ [timeUnit]: duration });
|
||||||
const returnedDate = moment(dateToAdd).add(
|
responseData.push({ [outputFieldName]: returnedDate });
|
||||||
duration,
|
|
||||||
timeUnit as moment.unitOfTime.DurationConstructor,
|
|
||||||
);
|
|
||||||
responseData.push({ [outputFieldName]: returnedDate });
|
|
||||||
} catch {
|
|
||||||
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
|
||||||
}
|
|
||||||
} else if (operation === 'subtractFromDate') {
|
} else if (operation === 'subtractFromDate') {
|
||||||
const subtractFromDate = this.getNodeParameter('subtractFromDate', i) as string;
|
const subtractFromDate = this.getNodeParameter('subtractFromDate', i) as string;
|
||||||
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
|
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
|
||||||
const duration = this.getNodeParameter('duration', i) as number;
|
const duration = this.getNodeParameter('duration', i) as number;
|
||||||
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
||||||
try {
|
const dateToAdd = parseDate.call(this, subtractFromDate, workflowTimezone);
|
||||||
const dateToAdd = moment.tz(subtractFromDate, workflowTimezone);
|
const returnedDate = dateToAdd.minus({ [timeUnit]: duration });
|
||||||
const returnedDate = moment(dateToAdd).subtract(
|
responseData.push({ [outputFieldName]: returnedDate });
|
||||||
duration,
|
|
||||||
timeUnit as moment.unitOfTime.DurationConstructor,
|
|
||||||
);
|
|
||||||
responseData.push({ [outputFieldName]: returnedDate });
|
|
||||||
} catch {
|
|
||||||
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
|
||||||
}
|
|
||||||
} else if (operation === 'formatDate') {
|
} else if (operation === 'formatDate') {
|
||||||
const date = this.getNodeParameter('date', i) as string;
|
const date = this.getNodeParameter('date', i) as string;
|
||||||
const format = this.getNodeParameter('format', i) as string;
|
const format = this.getNodeParameter('format', i) as string;
|
||||||
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
||||||
try {
|
const dateLuxon = parseDate.call(this, date, workflowTimezone);
|
||||||
const momentDate = moment.tz(date, workflowTimezone);
|
if (format === 'custom') {
|
||||||
if (format === 'custom') {
|
const customFormat = this.getNodeParameter('customFormat', i) as string;
|
||||||
const customFormat = this.getNodeParameter('customFormat', i) as string;
|
responseData.push({
|
||||||
responseData.push({ [outputFieldName]: momentDate.format(customFormat) });
|
[outputFieldName]: DateTime.fromFormat(dateLuxon.toISO(), customFormat),
|
||||||
} else {
|
});
|
||||||
responseData.push({ [outputFieldName]: momentDate.format(format) });
|
} else {
|
||||||
}
|
responseData.push({
|
||||||
} catch {
|
[outputFieldName]: DateTime.fromFormat(dateLuxon.toISO(), format),
|
||||||
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
});
|
||||||
}
|
}
|
||||||
} else if (operation === 'roundDate') {
|
} else if (operation === 'roundDate') {
|
||||||
const date = this.getNodeParameter('date', i) as string;
|
const date = this.getNodeParameter('date', i) as string;
|
||||||
|
|
|
@ -7,10 +7,12 @@ export function parseDate(this: IExecuteFunctions, date: string | DateTime, time
|
||||||
let parsedDate;
|
let parsedDate;
|
||||||
|
|
||||||
if (date instanceof DateTime) {
|
if (date instanceof DateTime) {
|
||||||
|
console.log('date is a DateTime object');
|
||||||
parsedDate = date;
|
parsedDate = date;
|
||||||
} else {
|
} else {
|
||||||
parsedDate = DateTime.fromISO(moment.tz(date, timezone).toISOString());
|
parsedDate = DateTime.fromISO(moment.tz(date, timezone).toISOString());
|
||||||
if (parsedDate.invalidReason === 'unparsable') {
|
if (parsedDate.invalidReason === 'unparsable') {
|
||||||
|
console.log(parsedDate.invalidReason);
|
||||||
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue