Skip to content

[IMP] pivot: insert pivot with real date #5943

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

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 10 additions & 5 deletions src/helpers/pivot/pivot_time_adapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { toNumber } from "../../functions/helpers";
import { toJsDate, toNumber } from "../../functions/helpers";
import { Registry } from "../../registries/registry";
import { _t } from "../../translation";
import { CellValue, DEFAULT_LOCALE } from "../../types";
import { EvaluationError } from "../../types/errors";
import { Granularity, PivotTimeAdapter, PivotTimeAdapterNotNull } from "../../types/pivot";
import { DAYS, MONTHS, formatValue } from "../format/format";
import { DAYS, MONTHS } from "../format/format";

export const pivotTimeAdapterRegistry = new Registry<PivotTimeAdapter<CellValue>>();

Expand Down Expand Up @@ -48,8 +48,8 @@ const dayAdapter: PivotTimeAdapterNotNull<number> = {
};
},
toFunctionValue(normalizedValue) {
const date = toNumber(normalizedValue, DEFAULT_LOCALE);
return `"${formatValue(date, { locale: DEFAULT_LOCALE, format: "mm/dd/yyyy" })}"`;
const jsDate = toJsDate(normalizedValue, DEFAULT_LOCALE);
return `DATE(${jsDate.getFullYear()}, ${jsDate.getMonth() + 1}, ${jsDate.getDate()})`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: there's no space at all inside generated PIVOT.VALUE formulas ... except here. We should stay consistent.

},
};

Expand Down Expand Up @@ -193,7 +193,12 @@ const quarterNumberAdapter: PivotTimeAdapterNotNull<number> = {

const yearAdapter: PivotTimeAdapterNotNull<number> = {
normalizeFunctionValue(value) {
return toNumber(value, DEFAULT_LOCALE);
const year = toNumber(value, DEFAULT_LOCALE);
if (year > 3000) {
// interpret the value as a full date
return toJsDate(year, DEFAULT_LOCALE).getFullYear();
}
return year;
},
toValueAndFormat(normalizedValue) {
return {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import {
parseDimension,
pivotNormalizationValueRegistry,
pivotToFunctionValueRegistry,
toFunctionPivotValue,
toNormalizedPivotValue,
} from "./helpers/pivot/pivot_helpers";
import { getPivotHighlights } from "./helpers/pivot/pivot_highlight";
Expand Down Expand Up @@ -296,6 +297,7 @@ export const helpers = {
toNumber,
toString,
toNormalizedPivotValue,
toFunctionPivotValue,
toXC,
toZone,
toUnboundedZone,
Expand Down
3 changes: 2 additions & 1 deletion tests/pivots/pivot_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe("toNormalizedPivotValue", () => {
expect(toNormalizedPivotValue(dimension, 2020)).toBe(2020);
expect(toNormalizedPivotValue(dimension, "false")).toBe(false);
expect(toNormalizedPivotValue(dimension, false)).toBe(false);
expect(toNormalizedPivotValue(dimension, "2020-12-31")).toBe(2020);

dimension.granularity = "day_of_month";
expect(toNormalizedPivotValue(dimension, "1")).toBe(1);
Expand Down Expand Up @@ -194,7 +195,7 @@ describe("ToFunctionValue", () => {
test.each(["date", "datetime"])("Format values of %s fields", (type: string) => {
const dimension = { type, granularity: "day" };
// day
expect(toFunctionPivotValue("1/11/2020", dimension)).toBe(`"01/11/2020"`);
expect(toFunctionPivotValue("1/11/2020", dimension)).toBe("DATE(2020, 1, 11)");
// year
dimension.granularity = "year";
expect(toFunctionPivotValue("2020", dimension)).toBe("2020");
Expand Down