|
|
|
@ -2,7 +2,34 @@ import jsep from 'jsep';
|
|
|
|
|
|
|
|
|
|
import { ColumnType } from './Api'; |
|
|
|
|
import UITypes from './UITypes'; |
|
|
|
|
import {validateDateWithUnknownFormat} from '../../../nc-gui/utils'; |
|
|
|
|
import dayjs from 'dayjs'; |
|
|
|
|
|
|
|
|
|
// todo: move to date utils and export, remove duplicate from gui
|
|
|
|
|
|
|
|
|
|
export const dateFormats = [ |
|
|
|
|
'YYYY-MM-DD', |
|
|
|
|
'YYYY/MM/DD', |
|
|
|
|
'DD-MM-YYYY', |
|
|
|
|
'MM-DD-YYYY', |
|
|
|
|
'DD/MM/YYYY', |
|
|
|
|
'MM/DD/YYYY', |
|
|
|
|
'DD MM YYYY', |
|
|
|
|
'MM DD YYYY', |
|
|
|
|
'YYYY MM DD', |
|
|
|
|
]; |
|
|
|
|
function validateDateWithUnknownFormat(v: string) { |
|
|
|
|
for (const format of dateFormats) { |
|
|
|
|
if (dayjs(v, format, true).isValid() as any) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
for (const timeFormat of ['HH:mm', 'HH:mm:ss', 'HH:mm:ss.SSS']) { |
|
|
|
|
if (dayjs(v, `${format} ${timeFormat}`, true).isValid() as any) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const jsepCurlyHook = { |
|
|
|
|
name: 'curly', |
|
|
|
@ -340,7 +367,6 @@ const formulas: Record<string, FormulaMeta> = {
|
|
|
|
|
max: 3, |
|
|
|
|
}, |
|
|
|
|
custom: (args: FormulaDataTypes[], parsedTree: any) => { |
|
|
|
|
|
|
|
|
|
if (parsedTree.arguments[0].type === JSEPNode.LITERAL) { |
|
|
|
|
if (!validateDateWithUnknownFormat(parsedTree.arguments[0].value)) { |
|
|
|
|
throw new FormulaError( |
|
|
|
@ -361,7 +387,8 @@ const formulas: Record<string, FormulaMeta> = {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (parsedTree.arguments[2].type === JSEPNode.LITERAL) { |
|
|
|
|
if (![ |
|
|
|
|
if ( |
|
|
|
|
![ |
|
|
|
|
'milliseconds', |
|
|
|
|
'ms', |
|
|
|
|
'seconds', |
|
|
|
@ -380,15 +407,16 @@ const formulas: Record<string, FormulaMeta> = {
|
|
|
|
|
'Q', |
|
|
|
|
'years', |
|
|
|
|
'y', |
|
|
|
|
].includes(parsedTree.arguments[0].value)) { |
|
|
|
|
].includes(parsedTree.arguments[0].value) |
|
|
|
|
) { |
|
|
|
|
throw new FormulaError( |
|
|
|
|
FormulaErrorType.TYPE_MISMATCH, |
|
|
|
|
{ key: 'msg.formula.thirdParamDateDiffHaveDate' }, |
|
|
|
|
'Third parameter of DATETIME_DIFF should be one of \'milliseconds\', \'ms\', \'seconds\', \'s\', \'minutes\', \'m\', \'hours\', \'h\', \'days\', \'d\', \'weeks\', \'w\', \'months\', \'M\', \'quarters\', \'Q\', \'years\', \'y\'' |
|
|
|
|
"Third parameter of DATETIME_DIFF should be one of 'milliseconds', 'ms', 'seconds', 's', 'minutes', 'm', 'hours', 'h', 'days', 'd', 'weeks', 'w', 'months', 'M', 'quarters', 'Q', 'years', 'y'" |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
description: |
|
|
|
|
'Calculate the difference of two given date / datetime in specified units.', |
|
|
|
|