Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

chore: migrate renderer_utils from moment to dayjs (#64413)

Uladzimir Havenchyka5d6067de161
diff --git a/frontend/src/metabase/lib/time-dayjs.ts b/frontend/src/metabase/lib/time-dayjs.ts
index 70088e4a8de..6dd0d647550 100644
--- a/frontend/src/metabase/lib/time-dayjs.ts
+++ b/frontend/src/metabase/lib/time-dayjs.ts
@@ -72,6 +72,25 @@ export function parseTimestamp(
let result: Dayjs;
if (dayjs.isDayjs(value)) {
result = value;
+ } else if (typeof value === "string" && /^\d{4}-W\d{2}$/.test(value)) {
+ // Parse ISO week format (e.g., "2019-W33")
+ const match = value.match(/^(\d{4})-W(\d{2})$/);
+ if (match) {
+ const year = parseInt(match[1], 10);
+ const week = parseInt(match[2], 10);
+ // Validate week number (ISO weeks range from 1-53, most years have 52)
+ if (week >= 1 && week <= 53) {
+ // ISO week 1 is the week that contains January 4th
+ // Calculate the Monday of week 1
+ const jan4 = dayjs.utc().year(year).month(0).date(4);
+ const mondayOfWeek1 = jan4.startOf("isoWeek");
+ result = mondayOfWeek1.add(week - 1, "week");
+ } else {
+ result = dayjs.utc(value); // will be invalid
+ }
+ } else {
+ result = dayjs.utc(value);
+ }
} else if (typeof value === "string" && /(Z|[+-]\d\d:?\d\d)$/.test(value)) {
result = dayjs.parseZone(value);
} else if (unit && unit in TEXT_UNIT_FORMATS && typeof value === "string") {
diff --git a/frontend/src/metabase/visualizations/lib/renderer_utils.js b/frontend/src/metabase/visualizations/lib/renderer_utils.js
index 42d2b2e4237..5be38adb9a8 100644
--- a/frontend/src/metabase/visualizations/lib/renderer_utils.js
+++ b/frontend/src/metabase/visualizations/lib/renderer_utils.js
@@ -2,7 +2,7 @@ import { getIn } from "icepick";
import _ from "underscore";
import { formatNullable } from "metabase/lib/formatting/nullable";
-import { parseTimestamp } from "metabase/lib/time";
+import { parseTimestamp } from "metabase/lib/time-dayjs";
import { datasetContainsNoResults } from "metabase-lib/v1/queries/utils/dataset";
import { dimensionIsNumeric } from "./numeric";
diff --git a/frontend/src/metabase/visualizations/lib/renderer_utils.unit.spec.js b/frontend/src/metabase/visualizations/lib/renderer_utils.unit.spec.js
index 23d34022838..6d5b8afc080 100644
--- a/frontend/src/metabase/visualizations/lib/renderer_utils.unit.spec.js
+++ b/frontend/src/metabase/visualizations/lib/renderer_utils.unit.spec.js
@@ -1,4 +1,4 @@
-import moment from "moment-timezone"; // eslint-disable-line no-restricted-imports -- deprecated usage
+import dayjs from "dayjs";
import {
getXValues,
@@ -122,7 +122,7 @@ describe("getXValues", () => {
series._raw = series;
const settings = { "graph.dimensions": ["date"] };
const [xVal] = getXValues({ settings, series });
- expect(moment.isMoment(xVal)).toBe(true);
+ expect(dayjs.isDayjs(xVal)).toBe(true);
});
it("should sort values according to parsed value", () => {
@@ -181,8 +181,8 @@ describe("parseXValue", () => {
it("should use options as part of the cache key", () => {
const value1 = parseXValue("2018-08-23", { isTimeseries: true });
const value2 = parseXValue("2018-08-23", { isTimeseries: false });
- expect(moment.isMoment(value1)).toBe(true);
- expect(moment.isMoment(value2)).toBe(false);
+ expect(dayjs.isDayjs(value1)).toBe(true);
+ expect(dayjs.isDayjs(value2)).toBe(false);
});
it("should warn repeatedly (despite caching)", () => {