Fix midnight hour format to ensure "00" instead of "24"

This commit is contained in:
2026-05-10 00:08:56 +02:00
parent 0e451bf51e
commit 85dfeb3526
2 changed files with 15 additions and 2 deletions
+13
View File
@@ -285,6 +285,19 @@ describe("hafasDateTime", () => {
expect(time).toBe("000000000");
});
it('produces "00" for midnight, not "24" (k-clock safety)', () => {
// Older ICU / Node can render midnight as "24" with hour: "numeric" + hour12: false.
// We use hour: "2-digit" which guarantees "00" through "23".
const utc = new Date(Date.UTC(2024, 0, 15, 0, 0, 0)); // Jan 15 00:00 UTC = Jan 15 01:00 CET
const { date: _date, time } = hafasDateTime(utc);
expect(time.startsWith("0")).toBe(true); // first digit is "0" from "01", never "24"
// Direct midnight in Vienna: Dec 31 23:00 UTC = Jan 1 00:00 CET
const midnightUtc = new Date(Date.UTC(2023, 11, 31, 23, 0, 0));
const { time: midnightTime } = hafasDateTime(midnightUtc);
expect(midnightTime).toBe("000000000");
});
it("handles midnight wraparound (Vienna midnight is still previous UTC day)", () => {
// Vienna Jan 1 00:00 CET = Dec 31 23:00 UTC
const utc = new Date(Date.UTC(2023, 11, 31, 23, 0, 0));
+2 -2
View File
@@ -14,7 +14,7 @@ function getTimezoneOffsetMinutes(instant: Date, tz: string): number {
const getHour = (timeZone: string) => {
const parts = new Intl.DateTimeFormat("en-US", {
timeZone,
hour: "numeric",
hour: "2-digit",
hour12: false,
}).formatToParts(instant);
return parseInt(parts.find((p) => p.type === "hour")!.value, 10);
@@ -79,7 +79,7 @@ function getDateTimeParts(instant: Date, tz: string) {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
hour: "2-digit",
minute: "numeric",
second: "numeric",
hour12: false,