Skip to content

Commit

Permalink
fix: formula WORKDAY_DIFF calculation error (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sky-FE committed Feb 22, 2023
1 parent 5a6ea4d commit a27b782
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/formula_parser/__tests__/date_time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ describe('DateTime function test', () => {
evaluate('WORKDAY_DIFF({c}, "2020-10-18")', mergeContext({ a: 0, b: '456', c: new Date('2020/6/6 00:00:00').getTime(), d: ['opt1', 'opt2'] })),
).toEqual(95);

expect(
evaluate('WORKDAY_DIFF({c}, "2023-02-28")', mergeContext({ a: 0, b: '456', c: new Date('2023/2/21 10:00:00').getTime(), d: ['opt1', 'opt2'] })),
).toEqual(6);

expect(
evaluate(
'WORKDAY_DIFF({c}, "2020-10-18", "2020-7-13")',
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/formula_parser/functions/date_time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ enum WeekdayUnits {
saturday,
}

type Tuple2Or3<T> = [T, T] | [T, T, T];

const UnitMapBase = new Map([
[Units.milliseconds, [Units.milliseconds, 'ms']],
[Units.seconds, [Units.seconds, 's']],
Expand All @@ -107,6 +109,7 @@ const getPureUnit = (unitStr: string) => {
}
return UnitMapBase.get(unit)![1] as QUnitType;
};

export const getDayjs = (timeStamp: any) => {
// TODO follow-up and lookup synchronous transformation (the timeStamp of string should not be passed in)
if (timeStamp == null) {
Expand All @@ -126,6 +129,11 @@ export const getDayjs = (timeStamp: any) => {
}
return date;
};

const getStartOfDay = (timeStamp: string | number) => {
return getDayjs(timeStamp).hour(0).minute(0).second(0).millisecond(0);
};

// Convert the day of the week and the first letter of the month in the date string to uppercase,
// This is compatible with the localization of dayjs
const formatDateTimeStr = (dateStr: string | number) => {
Expand Down Expand Up @@ -531,9 +539,9 @@ export class WorkDayDiff extends DateFunc {
return BasicValueType.Number;
}

static override func(params: IFormulaParam<number | string>[]): number {
let startDate = getDayjs(params[0]!.value);
let endDate = getDayjs(params[1]!.value);
static override func(params: Tuple2Or3<IFormulaParam<number | string>>): number {
let startDate = getStartOfDay(params[0].value);
let endDate = getStartOfDay(params[1].value);
const isMinus = startDate.valueOf() > endDate.valueOf(); // Whether with a negative sign
isMinus && ([startDate, endDate] = [endDate, startDate]);
const holidayStrList = params.length > 2 ? String(params[2]!.value).split(',') : [];
Expand Down

0 comments on commit a27b782

Please sign in to comment.