fix(grid): format Mongo shell date values
This commit is contained in:
1 parent
b855923a21
commit
cd51e247cb
2 files changed
+26
No files matched your search
@@ -51,6 +51,7 @@ const STRICT_LOCAL_DATETIME_INPUT_PATTERNS = [
|
|||||||
];
|
];
|
||||||
const ISO_OFFSET_DATETIME_PATTERN = /^(\d{4})([-/])(\d{2})\2(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/;
|
const ISO_OFFSET_DATETIME_PATTERN = /^(\d{4})([-/])(\d{2})\2(\d{2})T(\d{2}):(\d{2}):(\d{2})(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/;
|
||||||
const FRACTIONAL_LOCAL_DATETIME_PATTERN = /^(\d{4})([-/])(\d{1,2})\2(\d{1,2})([ T])(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9})$/;
|
const FRACTIONAL_LOCAL_DATETIME_PATTERN = /^(\d{4})([-/])(\d{1,2})\2(\d{1,2})([ T])(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9})$/;
|
||||||
|
const MONGO_SHELL_DATE_PATTERN = /^(?:ISODate|new Date)\(\s*(["'])(.+)\1\s*\)$/;
|
||||||
|
|
||||||
export interface CustomColumnFormatterConfig {
|
export interface CustomColumnFormatterConfig {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -262,6 +263,11 @@ function resolveDateTimeValue(value: string | number, unit: DateTimeFormatterUni
|
|||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
const mongoShellDate = unwrapMongoShellDate(trimmed);
|
||||||
|
if (mongoShellDate !== undefined) {
|
||||||
|
return parseStrictDateTimeString(mongoShellDate);
|
||||||
|
}
|
||||||
|
|
||||||
const timestamp = parseTimestampMilliseconds(trimmed, unit);
|
const timestamp = parseTimestampMilliseconds(trimmed, unit);
|
||||||
if (timestamp !== undefined) {
|
if (timestamp !== undefined) {
|
||||||
const parsedTimestamp = dayjs(timestamp);
|
const parsedTimestamp = dayjs(timestamp);
|
||||||
@@ -279,6 +285,10 @@ function resolveDateTimeValue(value: string | number, unit: DateTimeFormatterUni
|
|||||||
return parsedTimestamp.isValid() ? parsedTimestamp : undefined;
|
return parsedTimestamp.isValid() ? parsedTimestamp : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unwrapMongoShellDate(value: string): string | undefined {
|
||||||
|
return value.match(MONGO_SHELL_DATE_PATTERN)?.[2];
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeDateTimePattern(pattern: unknown): string {
|
function normalizeDateTimePattern(pattern: unknown): string {
|
||||||
return typeof pattern === "string" && pattern.trim() ? pattern : DEFAULT_DATETIME_PATTERN;
|
return typeof pattern === "string" && pattern.trim() ? pattern : DEFAULT_DATETIME_PATTERN;
|
||||||
}
|
}
|
||||||
|
|||||||
+16
@@ -85,6 +85,22 @@ test("formats Oracle timestamp fractional precision in the data grid", () => {
|
|||||||
assert.equal(applyColumnFormatter("2024-02-25 13:02:15.123456", { kind: "datetime", unit: "auto", pattern: "YYYY/MM/DD HH:mm:ss.SSS", timezone: undefined }), "2024/02/25 13:02:15.123");
|
assert.equal(applyColumnFormatter("2024-02-25 13:02:15.123456", { kind: "datetime", unit: "auto", pattern: "YYYY/MM/DD HH:mm:ss.SSS", timezone: undefined }), "2024/02/25 13:02:15.123");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("formats only strict Mongo shell date wrappers", () => {
|
||||||
|
for (const unit of ["auto", "seconds", "milliseconds"] as const) {
|
||||||
|
const formatter: ColumnFormatterConfig = { kind: "datetime", unit, pattern: "YYYY-MM-DD HH:mm:ss.SSSZ", timezone: "Asia/Shanghai" };
|
||||||
|
|
||||||
|
assert.equal(applyColumnFormatter('ISODate("2024-06-28T09:15:36.439Z")', formatter), "2024-06-28 17:15:36.439+08:00");
|
||||||
|
assert.equal(applyColumnFormatter(" new Date( '2024-06-28T09:15:36.439Z' ) ", formatter), "2024-06-28 17:15:36.439+08:00");
|
||||||
|
|
||||||
|
for (const value of ['ISODate("not-a-date")', "new Date('2024-02-30T09:15:36.439Z')", 'ISODate("1715758200")', "new Date('1715758200001')"]) {
|
||||||
|
assert.equal(applyColumnFormatter(value, formatter), value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.equal(applyColumnFormatter("1715758200", { kind: "datetime", unit: "seconds", pattern: "YYYY-MM-DD HH:mm:ssZ", timezone: "Asia/Shanghai" }), "2024-05-15 15:30:00+08:00");
|
||||||
|
assert.equal(applyColumnFormatter("1715758200001", { kind: "datetime", unit: "milliseconds", pattern: "YYYY-MM-DD HH:mm:ss.SSSZ", timezone: "Asia/Shanghai" }), "2024-05-15 15:30:00.001+08:00");
|
||||||
|
});
|
||||||
|
|
||||||
test("does not treat compact date strings as unix timestamps", () => {
|
test("does not treat compact date strings as unix timestamps", () => {
|
||||||
dayjs.extend(utc);
|
dayjs.extend(utc);
|
||||||
dayjs.extend(timezone);
|
dayjs.extend(timezone);
|
||||||
|
|||||||
Reference in new issue
Block a user