Skip to content

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

chore: replace moment with dayjs in time series (#64451)

Uladzimir Havenchykbbd050dd9585
diff --git a/frontend/src/metabase/visualizations/lib/date-validation.ts b/frontend/src/metabase/visualizations/lib/date-validation.ts
new file mode 100644
index 00000000000..a1af83b2904
--- /dev/null
+++ b/frontend/src/metabase/visualizations/lib/date-validation.ts
@@ -0,0 +1,156 @@
+// based on https://github.com/Garbee/Iso8601/blob/main/src/index.ts
+
+/**
+ * Years are represented from the gregorian calendar only
+ * as 0000 to 9999. (If anyone has to worry about after 9999
+ * with this code, I am deeply sorry.) Technically 0000 -
+ * 1582 are not allowed by the standard except by mutual
+ * agreement between parties. This is explicitly forced
+ * when validating in order to ensure we are only working
+ * with data browsers can reasonably parse and get correct.
+ */
+const yearPattern = /(?<year>158[3-9]|159\d|1[6-9]\d{2}|[2-9]\d{3})/;
+/**
+ * Months are always 2-digits in a non-index format.
+ * This differs from JavaScripts Date constructor for
+ * example where months are 0-11 rather than ISO's 1-12.
+ */
+const monthPattern = /(?<month>0[1-9]|1[0-2])/;
+/**
+ * Days are always 2 digits.
+ * 01-09, 10-19, 20-29, 30-31.
+ */
+const dayPattern = /(?<day>0[1-9]|[12]\d|3[01])/;
+/**
+ * Hours are always 2 digits and in 24 hour time.
+ * 00-23
+ */
+const hourPattern = /(?<hour>[01]\d|2[0-3])/;
+/**
+ * Minutes are always 2 digits.
+ */
+const minutePattern = /(?<minute>[0-5]\d)/;
+/**
+ * Seconds are always 2 digits.
+ * 60 is used to represent leap seconds.
+ */
+const secondsPattern = /(?<seconds>([0-5]\d)|60)/;
+/**
+ * Milliseconds have no defined precision.
+ * Allowing for up to 9 since this gets to nanoseconds.
+ * Going beyond nanoseconds seems unreasonable to expect:
+ * https://nickb.dev/blog/iso8601-and-nanosecond-precision-across-languages/
+ * From 0-999999999.
+ * The period to denote them is required if MS are present.
+ */
+const millisecondsPattern = /(?:\.(?<milliseconds>\d{1,9}))?/;
+
+/**
+ * Timezone is complex and difficult to break down further
+ * since it is all one group.
+ * Gist of it is timezones have 4 possible modes:
+ * * Positive Offset
+ * * Negative Offset
+ * * 'Z' string for UTC time (+00:00)
+ * * Unqualified (not present) (meaning local time.)
+ *
+ * With subgroups this could be made to give specific
+ * outputs like `positiveOffset`, `negativeOffset`, or
+ * `utcString` for determining which timezone style is
+ * present.
+ * However, without a use-case this effort wasn't completed
+ * only validated that it could be possible.
+ *
+ * This pattern technically allows a bit more than standards
+ * currently have. Up to +14:45 and down to -12:45. Should
+ * anyone care to correct this and make it more strict, go
+ * ahead just add tests to prove it when you do.
+ */
+const fullTimeZonePattern =
+ /(?<offset>([+]((0\d|1[0-3])(?::(00|15|30|45))?|14(?::00)?)|([-]((0\d|1[0-1])(?::(00|15|30|45))?|12(?::00)?)|Z))?)/;
+
+/**
+ * Pattern to determine if a full calendar date is present.
+ */
+const fullCalendarDatePattern =
+ yearPattern.source +
+ /-/.source +
+ monthPattern.source +
+ /-/.source +
+ dayPattern.source;
+
+/**
+ * Pattern to validate a complete date and time string.
+ */
+const fullIso8601DateTimePattern =
+ /^/.source +
+ yearPattern.source +
+ /-?/.source +
+ monthPattern.source +
+ /-?/.source +
+ dayPattern.source +
+ /T/.source +
+ hourPattern.source +
+ /:?/.source +
+ minutePattern.source +
+ /:?/.source +
+ secondsPattern.source +
+ millisecondsPattern.source +
+ fullTimeZonePattern.source +
+ /$/.source;
+
+/**
+ * Week date pattern: YYYY-Www or YYYY-Www-D
+ * where w is the week number (01-53) and D is the day of week (1-7)
+ */
+const weekDatePattern =
+ /^(?:158[3-9]|159\d|1[6-9]\d{2}|[2-9]\d{3})-W([0-4]\d|5[0-3])(-[1-7])?$/;
+
+/**
+ * Ordinal date pattern: YYYY-DDD
+ * where DDD is the day of the year (001-366)
+ */
+const ordinalDatePattern =
+ /^(?:158[3-9]|159\d|1[6-9]\d{2}|[2-9]\d{3})-([0-2]\d{2}|3[0-5]\d|36[0-6])$/;
+
+/**
+ * Space-separated datetime pattern: YYYY-MM-DD HH:MM:SS with optional milliseconds
+ */
+const spaceSeparatedDateTimePattern =
+ /^/.source +
+ yearPattern.source +
+ /-/.source +
+ monthPattern.source +
+ /-/.source +
+ dayPattern.source +
+ / /.source +
+ hourPattern.source +
+ /:/.source +
+ minutePattern.source +
+ /:/.source +
+ secondsPattern.source +
+ millisecondsPattern.source +
+ /$/.source;
+
+const iso8601Date = new RegExp(`^${fullCalendarDatePattern}$`);
+const isIso8601DateTime = new RegExp(fullIso8601DateTimePattern);
+const isSpaceSeparatedDateTime = new RegExp(spaceSeparatedDateTimePattern);
+
+/**
+ * Compact date pattern: YYYYMMDD
+ * where YYYY is year (1583-9999), MM is month (01-12), DD is day (01-31)
+ */
+const compactDatePattern =
+ /^(?:158[3-9]|159\d|1[6-9]\d{2}|[2-9]\d{3})(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])$/;
+
+export function isValidIso8601(value: string | number): boolean {
+ const stringValue = String(value);
+ return (
+ iso8601Date.test(stringValue) ||
+ isIso8601DateTime.test(stringValue) ||
+ weekDatePattern.test(stringValue) ||
+ ordinalDatePattern.test(stringValue) ||
+ isSpaceSeparatedDateTime.test(stringValue) ||
+ compactDatePattern.test(stringValue)
+ );
+}
diff --git a/frontend/src/metabase/visualizations/lib/date-validation.unit.spec.ts b/frontend/src/metabase/visualizations/lib/date-validation.unit.spec.ts
new file mode 100644
index 00000000000..3d586237e49
--- /dev/null
+++ b/frontend/src/metabase/visualizations/lib/date-validation.unit.spec.ts
@@ -0,0 +1,120 @@
+import { isValidIso8601 } from "metabase/visualizations/lib/date-validation";
+
+describe("isValidIso8601", () => {
+ // examples from https://en.wikipedia.org/wiki/ISO_8601
+ const VALID_ISO_8601_DATES = [
+ "2016-02-12",
+ "2016-02-12T03:21:55+00:00",
+ "2016-02-12T03:21:55Z",
+ "20160212T032155Z",
+ "2016-W06",
+ "2016-W06-5",
+ "2016-043",
+ "2024-06-28 00:00:00",
+ ];
+
+ VALID_ISO_8601_DATES.forEach((isoDate) => {
+ it(
+ "should detect ISO 8601 formatted string '" + isoDate + "' as valid",
+ () => {
+ expect(isValidIso8601(isoDate)).toBe(true);
+ },
+ );
+ });
+
+ const INVALID_DATES = ["100", "100 %", "scanner 005"];
+
+ INVALID_DATES.forEach((notDate) => {
+ it("should detect value '" + notDate + "' as invalid", () => {
+ expect(isValidIso8601(notDate)).toBe(false);
+ });
+ });
+
+ it("should detect full calendar date", () => {
+ expect(isValidIso8601("2024-01-15")).toBe(true);
+ });
+
+ it("should detect datetime with timezone offset", () => {
+ expect(isValidIso8601("2024-01-15T14:30:00+05:30")).toBe(true);
+ });
+
+ it("should detect datetime with negative timezone offset", () => {
+ expect(isValidIso8601("2024-01-15T14:30:00-08:00")).toBe(true);
+ });
+
+ it("should detect datetime with milliseconds", () => {
+ expect(isValidIso8601("2024-01-15T14:30:00.123Z")).toBe(true);
+ });
+
+ it("should detect compact datetime format", () => {
+ expect(isValidIso8601("20240115T143000Z")).toBe(true);
+ });
+
+ it("should detect week date format", () => {
+ expect(isValidIso8601("2024-W03")).toBe(true);
+ });
+
+ it("should reject wrong week date format", () => {
+ expect(isValidIso8601("1500-W01")).toBe(false);
+ });
+
+ it("should detect week date with day", () => {
+ expect(isValidIso8601("2024-W03-5")).toBe(true);
+ });
+
+ it("should detect ordinal date format", () => {
+ expect(isValidIso8601("2024-365")).toBe(true);
+ });
+
+ it("should reject wrong ordinal date format", () => {
+ expect(isValidIso8601("1500-365")).toBe(false);
+ });
+
+ it("should detect space-separated datetime", () => {
+ expect(isValidIso8601("2024-01-15 14:30:00")).toBe(true);
+ });
+
+ it("should reject invalid month", () => {
+ expect(isValidIso8601("2024-13-01")).toBe(false);
+ });
+
+ it("should reject invalid day", () => {
+ expect(isValidIso8601("2024-01-32")).toBe(false);
+ });
+
+ it("should reject invalid hour", () => {
+ expect(isValidIso8601("2024-01-15T25:00:00Z")).toBe(false);
+ });
+
+ it("should reject invalid minute", () => {
+ expect(isValidIso8601("2024-01-15T14:60:00Z")).toBe(false);
+ });
+
+ it("should reject year before 1583", () => {
+ expect(isValidIso8601("1582-01-01")).toBe(false);
+ });
+
+ it("should accept year 1583", () => {
+ expect(isValidIso8601("1583-01-01")).toBe(true);
+ });
+
+ it("should accept leap second (60)", () => {
+ expect(isValidIso8601("2024-06-30T23:59:60Z")).toBe(true);
+ });
+
+ it("should reject empty string", () => {
+ expect(isValidIso8601("")).toBe(false);
+ });
+
+ it("should reject partial date", () => {
+ expect(isValidIso8601("2024-01")).toBe(false);
+ });
+
+ it("should reject invalid week number", () => {
+ expect(isValidIso8601("2024-W54")).toBe(false);
+ });
+
+ it("should reject invalid ordinal day", () => {
+ expect(isValidIso8601("2024-367")).toBe(false);
+ });
+});
diff --git a/frontend/src/metabase/visualizations/lib/timeseries.js b/frontend/src/metabase/visualizations/lib/timeseries.js
index f440db009a8..086d36cfbe5 100644
--- a/frontend/src/metabase/visualizations/lib/timeseries.js
+++ b/frontend/src/metabase/visualizations/lib/timeseries.js
@@ -1,7 +1,7 @@
-import moment from "moment-timezone"; // eslint-disable-line no-restricted-imports -- deprecated usage
-
import { isDate } from "metabase-lib/v1/types/utils/isa";
+import { isValidIso8601 } from "./date-validation";
+
const TIMESERIES_UNITS = new Set([
"minute",
"hour",
@@ -32,7 +32,7 @@ export function dimensionIsTimeseries({ cols, rows }, i = 0) {
return false;
}
- if (!moment(value, moment.ISO_8601).isValid()) {
+ if (!isValidIso8601(value)) {
return false;
}
}
diff --git a/frontend/src/metabase/visualizations/lib/timeseries.unit.spec.js b/frontend/src/metabase/visualizations/lib/timeseries.unit.spec.js
index 79d8870488b..f67d96180c3 100644
--- a/frontend/src/metabase/visualizations/lib/timeseries.unit.spec.js
+++ b/frontend/src/metabase/visualizations/lib/timeseries.unit.spec.js
@@ -15,9 +15,11 @@ describe("visualization.lib.timeseries", () => {
"2016-W06",
"2016-W06-5",
"2016-043",
+ "2024-06-28 00:00:00",
+ 19210413,
];
- const NOT_DATES = ["100", "100 %", "scanner 005"];
+ const NOT_DATES = ["100", "100 %", "scanner 005", 99999999];
it("should detect Date column as timeseries", () => {
expect(dimensionIsTimeseries({ cols: [{ base_type: TYPE.Date }] })).toBe(
@@ -62,5 +64,23 @@ describe("visualization.lib.timeseries", () => {
).toBe(false);
});
});
+
+ it("should detect integer values in YYYYMMDD format as timeseries", () => {
+ expect(
+ dimensionIsTimeseries({
+ cols: [{ base_type: TYPE.Integer }],
+ rows: [[19210413], [20210413]],
+ }),
+ ).toBe(true);
+ });
+
+ it("should not detect integer values that are not valid dates as timeseries", () => {
+ expect(
+ dimensionIsTimeseries({
+ cols: [{ base_type: TYPE.Integer }],
+ rows: [[99999999], [12345678]],
+ }),
+ ).toBe(false);
+ });
});
});