Skip to content

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

chore: drop deprecated time handling utils (#64380)

Uladzimir Havenchyk4260236d6113
diff --git a/frontend/src/metabase/common/components/Schedule/components.tsx b/frontend/src/metabase/common/components/Schedule/components.tsx
index 4d932b84c23..95ec8f02bc9 100644
--- a/frontend/src/metabase/common/components/Schedule/components.tsx
+++ b/frontend/src/metabase/common/components/Schedule/components.tsx
@@ -7,7 +7,7 @@ import {
} from "metabase/admin/performance/utils";
import { capitalize } from "metabase/lib/formatting/strings";
import { useSelector } from "metabase/lib/redux";
-import { has24HourModeSetting } from "metabase/lib/time";
+import { has24HourModeSetting } from "metabase/lib/time-dayjs";
import { getApplicationName } from "metabase/selectors/whitelabel";
import { Box, Group, SegmentedControl, Tooltip } from "metabase/ui";
import type {
diff --git a/frontend/src/metabase/common/components/Schedule/strings.ts b/frontend/src/metabase/common/components/Schedule/strings.ts
index bc798f121de..b78d94ae116 100644
--- a/frontend/src/metabase/common/components/Schedule/strings.ts
+++ b/frontend/src/metabase/common/components/Schedule/strings.ts
@@ -1,7 +1,7 @@
import { c, msgid, ngettext, t } from "ttag";
import _ from "underscore";
-import { has24HourModeSetting } from "metabase/lib/time";
+import { has24HourModeSetting } from "metabase/lib/time-dayjs";
import type { ScheduleDayType, ScheduleFrameType } from "metabase-types/api";
function intToOption(n: number) {
diff --git a/frontend/src/metabase/common/components/Schedule/strings.unit.spec.ts b/frontend/src/metabase/common/components/Schedule/strings.unit.spec.ts
index bf40c30e0de..87061e16a5a 100644
--- a/frontend/src/metabase/common/components/Schedule/strings.unit.spec.ts
+++ b/frontend/src/metabase/common/components/Schedule/strings.unit.spec.ts
@@ -1,8 +1,8 @@
-import { has24HourModeSetting } from "metabase/lib/time";
+import { has24HourModeSetting } from "metabase/lib/time-dayjs";
import { getHours } from "./strings";
-jest.mock("metabase/lib/time", () => ({
+jest.mock("metabase/lib/time-dayjs", () => ({
has24HourModeSetting: jest.fn(),
}));
diff --git a/frontend/src/metabase/lib/cron.ts b/frontend/src/metabase/lib/cron.ts
index 207254b3860..302894eb1b3 100644
--- a/frontend/src/metabase/lib/cron.ts
+++ b/frontend/src/metabase/lib/cron.ts
@@ -4,7 +4,7 @@ import { t } from "ttag";
import { memoize } from "underscore";
import MetabaseSettings from "metabase/lib/settings";
-import { has24HourModeSetting } from "metabase/lib/time";
+import { has24HourModeSetting } from "metabase/lib/time-dayjs";
function translateErrorMessage(message: string) {
const errorMessageMap: Record<string, string> = {
diff --git a/frontend/src/metabase/lib/time-dayjs.ts b/frontend/src/metabase/lib/time-dayjs.ts
index 6dd0d647550..3bff6113bf2 100644
--- a/frontend/src/metabase/lib/time-dayjs.ts
+++ b/frontend/src/metabase/lib/time-dayjs.ts
@@ -2,6 +2,7 @@ import type { Dayjs } from "dayjs";
import dayjs from "dayjs";
import { t } from "ttag";
+import MetabaseSettings from "metabase/lib/settings";
import type { DatetimeUnit } from "metabase-types/api/query";
const DAYLIGHT_SAVINGS_CHANGE_TOLERANCE: Record<string, number> = {
@@ -150,3 +151,15 @@ export function parseTime(value: Dayjs | string) {
return dayjs.utc(value);
}
+
+function getTimeStyleFromSettings() {
+ const customFormattingSettings = MetabaseSettings.get("custom-formatting");
+ return customFormattingSettings?.["type/Temporal"]?.time_style;
+}
+
+const TIME_FORMAT_24_HOUR = "HH:mm";
+
+export function has24HourModeSetting() {
+ const timeStyle = getTimeStyleFromSettings();
+ return timeStyle === TIME_FORMAT_24_HOUR;
+}
diff --git a/frontend/src/metabase/lib/time.ts b/frontend/src/metabase/lib/time.ts
deleted file mode 100644
index b4d8ec005f0..00000000000
--- a/frontend/src/metabase/lib/time.ts
+++ /dev/null
@@ -1,144 +0,0 @@
-import moment from "moment-timezone"; // eslint-disable-line no-restricted-imports -- deprecated usage
-import { t } from "ttag";
-
-import MetabaseSettings from "metabase/lib/settings";
-import type { DatetimeUnit } from "metabase-types/api/query";
-
-addAbbreviatedLocale();
-
-// NOTE for developer:
-// this file is deprecated, do not modify it unless it is really necessary
-// use ./time-dayjs.ts instead
-
-const TIME_FORMAT_24_HOUR = "HH:mm";
-
-const TEXT_UNIT_FORMATS = {
- "day-of-week": (value: string) => {
- const day = moment.parseZone(value, "ddd").startOf("day");
- return day.isValid() ? day : moment.parseZone(value).startOf("day");
- },
-};
-
-const NUMERIC_UNIT_FORMATS = {
- // workaround for https://github.com/metabase/metabase/issues/1992
- "minute-of-hour": (value: number) => moment().minute(value).startOf("minute"),
- "hour-of-day": (value: number) => moment().hour(value).startOf("hour"),
- "day-of-week": (value: number) =>
- moment()
- .weekday(value - 1)
- .startOf("day"),
- "day-of-month": (value: number) =>
- moment("2016-01-01") // initial date must be in month with 31 days to format properly
- .date(value)
- .startOf("day"),
- "day-of-year": (value: number) =>
- moment("2016-01-01") // initial date must be in leap year to format properly
- .dayOfYear(value)
- .startOf("day"),
- "week-of-year": (value: number) =>
- moment("2016-01-01") // initial date must be in a year with 53 iso weeks to format properly
- .isoWeek(value) // set the iso week number to not depend on the first day of week
- .startOf("isoWeek"),
- "month-of-year": (value: number) =>
- moment()
- .month(value - 1)
- .startOf("month"),
- "quarter-of-year": (value: number) =>
- moment().quarter(value).startOf("quarter"),
- year: (value: number) => moment().year(value).startOf("year"),
-};
-
-// when you define a custom locale, moment automatically makes it the active global locale,
-// so we need to return to the user's initial locale.
-// also, you can't define a custom locale on a local instance
-function addAbbreviatedLocale() {
- const initialLocale = moment.locale();
-
- moment.locale("en-abbreviated", {
- relativeTime: {
- future: "in %s",
- past: "%s",
- s: t`just now`,
- ss: t`just now`,
- m: "%d m",
- mm: "%d m",
- h: "%d h",
- hh: "%d h",
- d: "%d d",
- dd: "%d d",
- w: "%d wk",
- ww: "%d wks",
- M: "a mth",
- MM: "%d mths",
- y: "%d y",
- yy: "%d y",
- },
- });
-
- moment.locale(initialLocale);
-}
-
-function getTimeStyleFromSettings() {
- const customFormattingSettings = MetabaseSettings.get("custom-formatting");
- return customFormattingSettings?.["type/Temporal"]?.time_style;
-}
-
-export function has24HourModeSetting() {
- const timeStyle = getTimeStyleFromSettings();
- return timeStyle === TIME_FORMAT_24_HOUR;
-}
-
-export function parseTime(value: moment.Moment | string) {
- if (moment.isMoment(value)) {
- return value;
- } else if (typeof value === "string") {
- // removing the timezone part if it exists, so we can parse the time correctly
- return moment(value.split(/[+-]/)[0], [
- "HH:mm:ss.SSSZ",
- "HH:mm:ss.SSS",
- "HH:mm:ss",
- "HH:mm:ss",
- "HH:mm",
- ]);
- }
-
- return moment.utc(value);
-}
-
-type NUMERIC_UNIT_FORMATS_KEY_TYPE =
- | "minute-of-hour"
- | "hour-of-day"
- | "day-of-week"
- | "day-of-month"
- | "day-of-year"
- | "week-of-year"
- | "month-of-year"
- | "quarter-of-year"
- | "year";
-
-// only attempt to parse the timezone if we're sure we have one (either Z or ±hh:mm or +-hhmm)
-// moment normally interprets the DD in YYYY-MM-DD as an offset :-/
-/**
- * @deprecated use dayjs version from ./time-dayjs.ts
- */
-export function parseTimestamp(
- value: any,
- unit: DatetimeUnit | null = null,
- isLocal = false,
-) {
- let m: any;
- if (moment.isMoment(value)) {
- m = value;
- } else if (typeof value === "string" && /(Z|[+-]\d\d:?\d\d)$/.test(value)) {
- m = moment.parseZone(value);
- } else if (unit && unit in TEXT_UNIT_FORMATS && typeof value === "string") {
- m = TEXT_UNIT_FORMATS[unit as "day-of-week"](value);
- } else if (unit && unit in NUMERIC_UNIT_FORMATS && typeof value == "number") {
- m = NUMERIC_UNIT_FORMATS[unit as NUMERIC_UNIT_FORMATS_KEY_TYPE](value);
- } else if (typeof value === "number") {
- m = moment.utc(value, moment.ISO_8601);
- } else {
- m = moment.utc(value);
- }
- return isLocal ? m.local() : m;
-}
diff --git a/frontend/src/metabase/lib/time.unit.spec.ts b/frontend/src/metabase/lib/time.unit.spec.ts
deleted file mode 100644
index 99e61786367..00000000000
--- a/frontend/src/metabase/lib/time.unit.spec.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import moment from "moment-timezone"; // eslint-disable-line no-restricted-imports -- deprecated usage
-
-import { parseTime } from "./time";
-
-describe("parseTime", () => {
- it("should return a moment object for valid time strings", () => {
- const timeString = "12:34:56";
- const result = parseTime(timeString);
- expect(result.isValid()).toBe(true);
- expect(result.format("HH:mm:ss")).toBe("12:34:56");
- });
-
- it("should return a moment object for valid moment objects", () => {
- const momentObj = moment("2023-10-01T12:34:56");
- const result = parseTime(momentObj);
- expect(result.isValid()).toBe(true);
- expect(result.format()).toBe(momentObj.format());
- });
-
- it("should return an invalid moment object for invalid input", () => {
- const invalidInput = "invalid-time";
- const result = parseTime(invalidInput);
- expect(result.isValid()).toBe(false);
- });
-
- it("should work with seconds", () => {
- const timeString = "13:22:11+01:00";
- const result = parseTime(timeString);
- expect(result.isValid()).toBe(true);
- expect(result.format("HH:mm:ss")).toBe("13:22:11");
- });
-
- it("should handle time with milliseconds", () => {
- const timeString = "13:22:11+01:00";
- const result = parseTime(timeString);
- expect(result.isValid()).toBe(true);
- expect(result.format("HH:mm:ss.SSS")).toBe("13:22:11.000");
- });
-});
diff --git a/jest.tz.unit.conf.js b/jest.tz.unit.conf.js
new file mode 100644
index 00000000000..f672045ea99
--- /dev/null
+++ b/jest.tz.unit.conf.js
@@ -0,0 +1,72 @@
+// @ts-check
+
+const esmPackages = [
+ "ccount",
+ "character-entities-html4",
+ "comma-separated-tokens",
+ "csv-parse",
+ "d3-*",
+ "d3",
+ "devlop",
+ "echarts",
+ "fetch-mock",
+ "hast.*",
+ "html-void-elements",
+ "is-absolute-url",
+ "jose",
+ "property-information",
+ "rehype-external-links",
+ "screenfull",
+ "space-separated-tokens",
+ "stringify-entities",
+ "unist-util-visit-parents",
+ "unist-util-visit",
+ "vfile-location",
+ "vfile-message",
+ "vfile",
+ "web-namespaces",
+ "zrender",
+ "zwitch",
+];
+
+/** @type {import('jest').Config} */
+module.exports = {
+ moduleNameMapper: {
+ "\\.(css|less)$": "<rootDir>/frontend/test/__mocks__/styleMock.js",
+ "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
+ "<rootDir>/frontend/test/__mocks__/fileMock.js",
+ "^cljs/(.*)$": "<rootDir>/target/cljs_dev/$1",
+ "^d3-(.*)$": "<rootDir>/node_modules/d3-$1/dist/d3-$1",
+ "react-markdown":
+ "<rootDir>/node_modules/react-markdown/react-markdown.min.js",
+ "\\.svg\\?(component|source)":
+ "<rootDir>/frontend/test/__mocks__/svgMock.jsx",
+ },
+ transformIgnorePatterns: [
+ `<rootDir>/node_modules/(?!(${esmPackages.join("|")})/)`,
+ ],
+ testMatch: ["<rootDir>/frontend/**/*.tz.unit.spec.{js,ts,jsx,tsx}"],
+ modulePaths: [
+ "<rootDir>/frontend/test",
+ "<rootDir>/frontend/src",
+ "<rootDir>/enterprise/frontend/src",
+ ],
+ setupFiles: [
+ "<rootDir>/frontend/test/jest-setup.js",
+ "<rootDir>/frontend/test/metabase-bootstrap.js",
+ "<rootDir>/frontend/test/register-visualizations.js",
+ ],
+ globals: {
+ ace: {},
+ ga: {},
+ },
+ coverageDirectory: "./",
+ coverageReporters: ["text", "json-summary"],
+ collectCoverageFrom: ["frontend/src/**/*.{js,ts,jsx,tsx}"],
+ coveragePathIgnorePatterns: [
+ "/node_modules/",
+ "/frontend/src/metabase/visualizations/lib/errors.js",
+ ],
+ testEnvironment: "jest-environment-jsdom",
+ testTimeout: 30000,
+};
diff --git a/jest.tz.unit.conf.json b/jest.tz.unit.conf.json
deleted file mode 100644
index b0f4ce9bb5e..00000000000
--- a/jest.tz.unit.conf.json
+++ /dev/null
@@ -1,37 +0,0 @@
-{
- "moduleNameMapper": {
- "\\.(css|less)$": "<rootDir>/frontend/test/__mocks__/styleMock.js",
- "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/frontend/test/__mocks__/fileMock.js",
- "^cljs/(.*)$": "<rootDir>/target/cljs_dev/$1",
- "^d3-(.*)$": "<rootDir>/node_modules/d3-$1/dist/d3-$1",
- "react-markdown": "<rootDir>/node_modules/react-markdown/react-markdown.min.js",
- "\\.svg\\?(component|source)": "<rootDir>/frontend/test/__mocks__/svgMock.jsx"
- },
- "transformIgnorePatterns": [
- "<rootDir>/node_modules/(?!rehype-external-links|d3/)"
- ],
- "testMatch": ["<rootDir>/frontend/**/*.tz.unit.spec.{js,ts,jsx,tsx}"],
- "modulePaths": [
- "<rootDir>/frontend/test",
- "<rootDir>/frontend/src",
- "<rootDir>/enterprise/frontend/src"
- ],
- "setupFiles": [
- "<rootDir>/frontend/test/jest-setup.js",
- "<rootDir>/frontend/test/metabase-bootstrap.js",
- "<rootDir>/frontend/test/register-visualizations.js"
- ],
- "globals": {
- "ace": {},
- "ga": {}
- },
- "coverageDirectory": "./",
- "coverageReporters": ["text", "json-summary"],
- "collectCoverageFrom": ["frontend/src/**/*.{js,ts,jsx,tsx}"],
- "coveragePathIgnorePatterns": [
- "/node_modules/",
- "/frontend/src/metabase/visualizations/lib/errors.js"
- ],
- "testEnvironment": "jest-environment-jsdom",
- "testTimeout": 30000
-}
diff --git a/package.json b/package.json
index a17996bfade..0172c5ad0e1 100644
--- a/package.json
+++ b/package.json
@@ -478,7 +478,7 @@
"test-cypress": "tsx ./e2e/runner/run_cypress_local.ts",
"test-debug": "yarn build:cljs && node --inspect-brk node_modules/.bin/jest --runInBand --detectOpenHandles",
"test-timezones": "yarn && ./frontend/test/__runner__/run_timezone_tests",
- "test-timezones-unit": "yarn build:cljs && jest --silent --maxWorkers=4 --config jest.tz.unit.conf.json",
+ "test-timezones-unit": "yarn build:cljs && jest --silent --maxWorkers=4 --config jest.tz.unit.conf.js",
"test-unit": "yarn build:cljs && jest --maxWorkers=4",
"test-unit-keep-cljs": "jest --maxWorkers=4",
"test-unit-watch": "yarn test-unit --watch",