mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-09-24 16:04:36 +08:00
dbeaver/pro#8051 fix: support time-only formatting in DateTimeFormatter and update related tests (#4065)
This commit is contained in:
+12
-3
@@ -42,16 +42,25 @@ export const DateTimeFormatter = observer<ICellFormatterProps>(function DateTime
|
||||
let dateFormatter: Intl.DateTimeFormat | null = null;
|
||||
switch (extendedDateKind) {
|
||||
case DateTimeKind.DateTime:
|
||||
case DateTimeKind.TimeOnly:
|
||||
dateFormatter = formattingContext.formatters.dateTime;
|
||||
break;
|
||||
case DateTimeKind.TimeOnly:
|
||||
dateFormatter = formattingContext.formatters.timeOnly;
|
||||
break;
|
||||
case DateTimeKind.DateOnly:
|
||||
dateFormatter = formattingContext.formatters.dateOnly;
|
||||
break;
|
||||
}
|
||||
if (dateFormatter) {
|
||||
const date = new Date(displayValue);
|
||||
value = dateFormatter.format(date);
|
||||
if (DateTimeKind.TimeOnly === extendedDateKind) {
|
||||
const [h = 0, m = 0, s = 0] = displayValue.split(':').map(Number);
|
||||
const date = new Date();
|
||||
date.setHours(h, m, s, 0);
|
||||
value = dateFormatter.format(date);
|
||||
} else {
|
||||
const date = new Date(displayValue);
|
||||
value = dateFormatter.format(date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ export interface IDataGridFormatters {
|
||||
locale: string;
|
||||
dateTime: Intl.DateTimeFormat;
|
||||
dateOnly: Intl.DateTimeFormat;
|
||||
timeOnly: Intl.DateTimeFormat;
|
||||
number: Intl.NumberFormat;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* CloudBeaver - Cloud Database Manager
|
||||
* Copyright (C) 2020-2025 DBeaver Corp and others
|
||||
* Copyright (C) 2020-2026 DBeaver Corp and others
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0.
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,10 +17,10 @@ describe('detectDateTimeKind', () => {
|
||||
expect(detectDateTimeKind('1999-12-31')).toBe(DateTimeKind.DateOnly);
|
||||
});
|
||||
|
||||
test('should detect time-only format (HH:MM:SS)', () => {
|
||||
test('should detect time-only format (HH:MM:SS or HH:MM)', () => {
|
||||
expect(detectDateTimeKind('14:30:00')).toBe(DateTimeKind.TimeOnly);
|
||||
expect(detectDateTimeKind('00:00:00')).toBe(DateTimeKind.TimeOnly);
|
||||
expect(detectDateTimeKind('23:59:59')).toBe(DateTimeKind.TimeOnly);
|
||||
expect(detectDateTimeKind('23:59')).toBe(DateTimeKind.TimeOnly);
|
||||
expect(detectDateTimeKind('14:30:00.123')).toBe(DateTimeKind.TimeOnly);
|
||||
});
|
||||
|
||||
|
||||
+2
-1
@@ -10,13 +10,14 @@ import { DateTimeKind } from '../FormattingContext.js';
|
||||
|
||||
const DATE_ONLY_REGEX = /^\d{4}-\d{2}-\d{2}$/;
|
||||
const TIME_ONLY_REGEX = /^\d{2}:\d{2}:\d{2}/;
|
||||
const TIME_ONLY_NO_SECONDS_REGEX = /^\d{2}:\d{2}$/;
|
||||
|
||||
export function detectDateTimeKind(displayValue: string): DateTimeKind {
|
||||
if (DATE_ONLY_REGEX.test(displayValue)) {
|
||||
return DateTimeKind.DateOnly;
|
||||
}
|
||||
|
||||
if (TIME_ONLY_REGEX.test(displayValue) && !displayValue.includes('-')) {
|
||||
if ((TIME_ONLY_REGEX.test(displayValue) || TIME_ONLY_NO_SECONDS_REGEX.test(displayValue)) && !displayValue.includes('-')) {
|
||||
return DateTimeKind.TimeOnly;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,11 @@ export function useFormatting(tableData: ITableData, cache: ResultSetCacheAction
|
||||
|
||||
return {
|
||||
locale,
|
||||
timeOnly: new Intl.DateTimeFormat(locale, {
|
||||
hour: 'numeric',
|
||||
minute: 'numeric',
|
||||
second: 'numeric',
|
||||
}),
|
||||
dateTime: new Intl.DateTimeFormat(locale, {
|
||||
year: 'numeric',
|
||||
month: 'numeric',
|
||||
@@ -50,7 +55,6 @@ export function useFormatting(tableData: ITableData, cache: ResultSetCacheAction
|
||||
year: 'numeric',
|
||||
month: 'numeric',
|
||||
day: 'numeric',
|
||||
timeZone: 'UTC',
|
||||
}),
|
||||
number: new Intl.NumberFormat(locale),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user