diff --git a/src/helpers/pivot/pivot_time_adapter.ts b/src/helpers/pivot/pivot_time_adapter.ts index 8292059034..803c682a1d 100644 --- a/src/helpers/pivot/pivot_time_adapter.ts +++ b/src/helpers/pivot/pivot_time_adapter.ts @@ -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>(); @@ -48,8 +48,8 @@ const dayAdapter: PivotTimeAdapterNotNull = { }; }, 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()})`; }, }; @@ -193,7 +193,12 @@ const quarterNumberAdapter: PivotTimeAdapterNotNull = { const yearAdapter: PivotTimeAdapterNotNull = { 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 { diff --git a/src/index.ts b/src/index.ts index b59e76ea7e..6083000fa3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -128,6 +128,7 @@ import { parseDimension, pivotNormalizationValueRegistry, pivotToFunctionValueRegistry, + toFunctionPivotValue, toNormalizedPivotValue, } from "./helpers/pivot/pivot_helpers"; import { getPivotHighlights } from "./helpers/pivot/pivot_highlight"; @@ -296,6 +297,7 @@ export const helpers = { toNumber, toString, toNormalizedPivotValue, + toFunctionPivotValue, toXC, toZone, toUnboundedZone, diff --git a/tests/pivots/pivot_helpers.test.ts b/tests/pivots/pivot_helpers.test.ts index 5d34716655..1c2df65453 100644 --- a/tests/pivots/pivot_helpers.test.ts +++ b/tests/pivots/pivot_helpers.test.ts @@ -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); @@ -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");