Skip to content

Commit 51978f8

Browse files
committed
fix time parsing
1 parent a0e6542 commit 51978f8

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

packages/sit-onyx/src/components/OnyxDataGrid/features/renderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ export const timeFormatter = <TEntry extends DataGridEntry>(
196196
opts?: DateCellOptions,
197197
): string => {
198198
// using loose "==" here to catch both undefined and null
199-
if (value == undefined || typeof value !== "string") return fallback(opts);
199+
if (value == undefined || typeof value === "boolean") return fallback(opts);
200200

201201
const { d } = injectI18n();
202202

203203
const seconds = parseTimeSeconds(value);
204204
if (seconds) {
205-
const now = new Date();
205+
const base = new Date(0);
206206
return d.value(
207-
new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, seconds),
207+
new Date(base.getFullYear(), base.getMonth(), base.getDate(), 0, 0, seconds),
208208
"time",
209209
);
210210
}

packages/sit-onyx/src/utils/time.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe("parseTimeSeconds", () => {
3333
{ timeString: "12:", expected: null },
3434
{ timeString: "12:34:", expected: null },
3535
{ timeString: "12:foo", expected: null },
36-
{ timeString: "foo:12", expected: null },
3736
{ timeString: "12:34:bar", expected: null },
3837
{ timeString: "00:00:00", expected: 0 },
3938
{ timeString: "00:00:01", expected: 1 },

packages/sit-onyx/src/utils/time.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { DateValue } from "../components/OnyxDatePicker/types.js";
2+
13
/**
24
* Calculate seconds, minutes and hours for a given number of milliseconds.
35
*/
@@ -57,9 +59,22 @@ export const timeToDurationString = (timeLeft: number): `PT${number}H${number}M$
5759
/**
5860
* Parses an RFC 9557 time string (`HH:mm:ss.sssssssss`) and converts it into seconds.
5961
*/
60-
export const parseTimeSeconds = (timeString?: string): number | null => {
61-
if (!timeString) return null;
62-
const parts = timeString.split(":").map((p) => Number.parseInt(p, 10));
63-
if (parts.length < 2 || parts.some(Number.isNaN)) return null;
64-
return parts[0]! * 3600 + parts[1]! * 60 + (parts[2] || 0);
62+
export const parseTimeSeconds = (value?: unknown): number | null => {
63+
if (!value) return null;
64+
65+
if (typeof value === "string" && /^(\d+:)*\d+(\.\d+)?$/.test(value)) {
66+
const parts = value.split(":").map((p) => Number.parseInt(p, 10));
67+
if (parts.length < 2 || parts.some(Number.isNaN)) return null;
68+
return parts[0]! * 3600 + parts[1]! * 60 + (parts[2] || 0);
69+
}
70+
71+
try {
72+
const date = new Date(typeof value === "bigint" ? Number(value) : (value as DateValue));
73+
if (!Number.isNaN(date.getTime())) {
74+
return date.getTime() / 1000;
75+
}
76+
return null;
77+
} catch {
78+
return null;
79+
}
6580
};

0 commit comments

Comments
 (0)