Skip to content

Commit cdc94dc

Browse files
dbeaver/pro#8089 validate date before formatting (#4091)
* dbeaver/pro#8089 validate date before formatting * dbeaver/pro#8089 treat date and time the same --------- Co-authored-by: Daria Marutkina <125263541+dariamarutkina@users.noreply.github.com>
1 parent 70b67ce commit cdc94dc

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

webapp/packages/plugin-data-spreadsheet-new/src/DataGrid/Formatters/CellFormatters/DateTimeFormatter.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { DateTimeKind, useFormattingContext } from '../../FormattingContext.js';
1616
import { TableDataContext } from '../../TableDataContext.js';
1717
import type { ICellFormatterProps } from '../ICellFormatterProps.js';
1818

19+
function isValidDate(date: Date): boolean {
20+
return !isNaN(date.getTime());
21+
}
22+
1923
export const DateTimeFormatter = observer<ICellFormatterProps>(function DateTimeFormatter() {
2024
const tableDataContext = useContext(TableDataContext);
2125
const formattingContext = useFormattingContext();
@@ -34,12 +38,13 @@ export const DateTimeFormatter = observer<ICellFormatterProps>(function DateTime
3438
return <GridNullFormatter />;
3539
}
3640

37-
let value = displayValue;
41+
let date = new Date(displayValue);
42+
let dateFormatter: Intl.DateTimeFormat | null = null;
3843

3944
if (formattingContext.formatters) {
4045
const extendedDateKind = formattingContext.getExtendedDateKind(cellContext.cell.column);
4146

42-
let dateFormatter: Intl.DateTimeFormat | null = null;
47+
4348
switch (extendedDateKind) {
4449
case DateTimeKind.DateTime:
4550
dateFormatter = formattingContext.formatters.dateTime;
@@ -51,19 +56,22 @@ export const DateTimeFormatter = observer<ICellFormatterProps>(function DateTime
5156
dateFormatter = formattingContext.formatters.dateOnly;
5257
break;
5358
}
54-
if (dateFormatter) {
55-
if (DateTimeKind.TimeOnly === extendedDateKind) {
56-
const [h = 0, m = 0, s = 0] = displayValue.split(':').map(Number);
57-
const date = new Date();
58-
date.setHours(h, m, s, 0);
59-
value = dateFormatter.format(date);
60-
} else {
61-
const date = new Date(displayValue);
62-
value = dateFormatter.format(date);
63-
}
59+
60+
if (DateTimeKind.TimeOnly === extendedDateKind) {
61+
const [h = 0, m = 0, s = 0] = displayValue.split(':').map(Number);
62+
const time = new Date();
63+
time.setHours(h, m, s, 0);
64+
65+
date = time;
6466
}
6567
}
6668

69+
let value = displayValue;
70+
71+
if (dateFormatter && isValidDate(date)) {
72+
value = dateFormatter.format(date);
73+
}
74+
6775
return (
6876
<div className="tw:flex tw:items-center tw:overflow-hidden">
6977
<div className="tw:overflow-hidden tw:text-ellipsis">{value}</div>

0 commit comments

Comments
 (0)