Skip to content

Commit

Permalink
feat(core/reports): xlsreport date functions
Browse files Browse the repository at this point in the history
  • Loading branch information
robzan8 committed Apr 11, 2023
1 parent d095ff6 commit 8fce741
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions projects/core/models/src/utils/expression-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,17 @@ For example, `OP([1, 2, 3], [10, 20, 30], elemA + elemB)` returns `[11, 22, 33]`

## Date functions

Dates are strings in the format 2000-12-31, unless otherwise specified.

`TODAY(format = "yyyy-MM-dd")`
Returns today's date.

`ADD_DAYS(date, days)`
Returns the date obtained by adding days to date.

`DAYS_DIFF(a, b)`
Returns the difference in days (a - b) between the two dates.

`IS_BEFORE(date, dateToCompare)`
Returns true if date is before dateToCompare.

Expand Down
24 changes: 23 additions & 1 deletion projects/core/models/src/utils/expression-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import {ALL_VALUES_OF, SUM, MainForm, MEAN, COUNT_FORMS, JOIN_FORMS, FILTER_BY, evaluateExpression} from './expression-utils';
import {
MainForm,
evaluateExpression,
ALL_VALUES_OF,
SUM,
MEAN,
COUNT_FORMS,
JOIN_FORMS,
FILTER_BY,
ADD_DAYS,
DAYS_DIFF,
} from './expression-utils';

const forms: MainForm[] = [
{
Expand Down Expand Up @@ -116,3 +127,14 @@ describe('MAP', () => {
expect(result).toEqual([2, 1, 0]);
});
});

describe('date functions', () => {
it('ADD_DAYS', () => {
expect(ADD_DAYS('2000-12-31', 1)).toBe('2001-01-01');
expect(ADD_DAYS('2000-12-30', -15)).toBe('2000-12-15');
});
it('DAYS_DIFF', () => {
expect(DAYS_DIFF('2000-12-31', '2000-12-29')).toBe(2);
expect(DAYS_DIFF('1998-12-31', '1999-12-31')).toBe(-365);
});
});
21 changes: 21 additions & 0 deletions projects/core/models/src/utils/expression-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export class AjfExpressionUtils {
buildWidgetDatasetWithDialog: {fn: buildWidgetDatasetWithDialog},
FILTER_BY_VARS: {fn: FILTER_BY_VARS},
FILTER_BY: {fn: FILTER_BY},
ADD_DAYS: {fn: ADD_DAYS},
DAYS_DIFF: {fn: DAYS_DIFF},
IS_BEFORE: {fn: IS_BEFORE},
IS_AFTER: {fn: IS_AFTER},
IS_WITHIN_INTERVAL: {fn: IS_WITHIN_INTERVAL},
Expand Down Expand Up @@ -1799,6 +1801,25 @@ export function REMOVE_DUPLICATES(arr: any[]): any[] {
return [...new Map(arr.map(v => [JSON.stringify(v), v])).values()];
}

// Returns the date obtained by adding days to date.
export function ADD_DAYS(date: string, days: number): string {
const d = new Date(date);
d.setDate(d.getDate() + days);
return d.toJSON().slice(0, 10);
}

// Returns the difference in days (a - b) between the two dates.
export function DAYS_DIFF(a: string, b: string): number {
const dateA = new Date(a);
const dateB = new Date(b);
// UTC avoids bugs with daylight saving time.
const utcA = Date.UTC(dateA.getFullYear(), dateA.getMonth(), dateA.getDate());
const utcB = Date.UTC(dateB.getFullYear(), dateB.getMonth(), dateB.getDate());

const millisPerDay = 1000 * 60 * 60 * 24;
return Math.floor((utcA - utcB) / millisPerDay);
}

/**
* Returns true if date is before dateToCompare.
*
Expand Down
2 changes: 2 additions & 0 deletions projects/core/reports/src/xls-report/hindikit-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ const functionArgs: {[name: string]: string[]} = {
APPLY_LABELS: ["arg", "arg", "arg"],
BUILD_DATASET: ["arg", "arg?"],
ROUND: ["arg", "arg?"],
ADD_DAYS: ["arg", "arg"],
DAYS_DIFF: ["arg", "arg"],
IS_BEFORE: ["arg", "arg"],
IS_AFTER: ["arg", "arg"],
IS_WITHIN_INTERVAL: ["arg", "arg", "arg"],
Expand Down

0 comments on commit 8fce741

Please sign in to comment.