Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: formula WORKDAY_DIFF calculation error #435

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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