diff --git a/CHANGELOG.md b/CHANGELOG.md index f2e106f8..a9d4bfdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,11 @@ written while it was being built. See [RELEASING.md](RELEASING.md). ### Fixed +- A hackathon's start and end are now a date and a time in Zurich time, and the + hackathon closes exactly then. Before, only a date could be picked, and the + hackathon closed at 01:00 on its last day: nobody could join, invite links + stopped working, and a one-day hackathon showed as finished all day. + ## [0.13.0](https://github.com/SwissDataScienceCenter/hackagon/compare/v0.12.1...v0.13.0) - 2026-11-16 ### Added diff --git a/components/frontend/src/lib/utils/eventTime.test.ts b/components/frontend/src/lib/utils/eventTime.test.ts new file mode 100644 index 00000000..baf930f9 --- /dev/null +++ b/components/frontend/src/lib/utils/eventTime.test.ts @@ -0,0 +1,97 @@ +import { describe, expect, it } from "vitest" +import { + fromEventDateTime, + readEventDateTime, + toEventDateTime, +} from "./eventTime" + +describe("fromEventDateTime", () => { + it("reads a winter time as Zurich time, UTC+1", () => { + expect(fromEventDateTime("2026-11-13T18:00")?.toISOString()).toBe( + "2026-11-13T17:00:00.000Z", + ) + }) + + it("reads a summer time as Zurich time, UTC+2", () => { + expect(fromEventDateTime("2026-07-01T09:00")?.toISOString()).toBe( + "2026-07-01T07:00:00.000Z", + ) + }) + + it("reads midnight as the start of that day in Zurich", () => { + expect(fromEventDateTime("2026-11-14T00:00")?.toISOString()).toBe( + "2026-11-13T23:00:00.000Z", + ) + }) + + it("moves a time skipped by the spring change an hour later", () => { + // 02:30 on 2026-03-29 does not exist in Zurich: clocks jump 02:00 -> 03:00. + expect(fromEventDateTime("2026-03-29T02:30")?.toISOString()).toBe( + "2026-03-29T01:30:00.000Z", + ) + }) + + it("takes the offset in force on either side of the autumn change", () => { + expect(fromEventDateTime("2026-10-25T01:30")?.toISOString()).toBe( + "2026-10-24T23:30:00.000Z", + ) + expect(fromEventDateTime("2026-10-25T03:30")?.toISOString()).toBe( + "2026-10-25T02:30:00.000Z", + ) + }) + + it("refuses what is not a date and time", () => { + expect(fromEventDateTime("2026-11-13")).toBeNull() + expect(fromEventDateTime("2026-02-30T10:00")).toBeNull() + expect(fromEventDateTime("2026-11-13T24:00")).toBeNull() + expect(fromEventDateTime("tomorrow")).toBeNull() + }) +}) + +describe("toEventDateTime", () => { + it("shows an instant on Zurich's clock", () => { + expect(toEventDateTime(new Date("2026-11-13T17:00:00Z"))).toBe( + "2026-11-13T18:00", + ) + expect(toEventDateTime(new Date("2026-07-01T07:00:00Z"))).toBe( + "2026-07-01T09:00", + ) + }) + + it("shows midnight as 00:00, not 24:00", () => { + expect(toEventDateTime(new Date("2026-11-13T23:00:00Z"))).toBe( + "2026-11-14T00:00", + ) + }) + + it("is empty for no date", () => { + expect(toEventDateTime(undefined)).toBe("") + }) + + it("round-trips through fromEventDateTime", () => { + for (const v of [ + "2026-01-05T08:15", + "2026-06-21T23:59", + "2026-12-31T00:00", + ]) { + expect(toEventDateTime(fromEventDateTime(v) ?? undefined)).toBe(v) + } + }) +}) + +describe("readEventDateTime", () => { + it("is undefined for an empty or missing field", () => { + expect(readEventDateTime(null)).toBeUndefined() + expect(readEventDateTime(" ")).toBeUndefined() + }) + + it("is null for a value that is not a date and time", () => { + expect(readEventDateTime("2026-11-13")).toBeNull() + }) + + it("parses a filled field in Zurich time", () => { + expect(readEventDateTime("2026-11-13T09:00")?.toISOString()).toBe( + "2026-11-13T08:00:00.000Z", + ) + }) +}) diff --git a/components/frontend/src/lib/utils/eventTime.ts b/components/frontend/src/lib/utils/eventTime.ts new file mode 100644 index 00000000..9dadb608 --- /dev/null +++ b/components/frontend/src/lib/utils/eventTime.ts @@ -0,0 +1,113 @@ +/** + * The time zone a hackathon's start and end are entered and edited in. + * + * Named rather than left to the runtime: the form actions that read these + * times run on the server, whose clock is UTC in the cluster, and `new Date()` + * on a bare `YYYY-MM-DDTHH:mm` reads it in that clock. A date-only value was + * worse — it parsed as 00:00 UTC, so a hackathon ended at 01:00 Zurich time on + * its own last day. Every hackathon so far is run from Zurich; a per-hackathon + * zone is a bigger change than this one. + */ +export const EVENT_TIME_ZONE = "Europe/Zurich" + +const DATE_TIME_LOCAL = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})$/ + +interface WallClock { + year: number + month: number + day: number + hour: number + minute: number + second: number +} + +/** `d`'s wall-clock fields in `timeZone`. */ +function wallClock(d: Date, timeZone: string): WallClock { + const parts = new Intl.DateTimeFormat("en-US", { + timeZone, + hourCycle: "h23", + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + }).formatToParts(d) + const part = (type: Intl.DateTimeFormatPartTypes): number => + Number(parts.find((p) => p.type === type)?.value) + + return { + year: part("year"), + month: part("month"), + day: part("day"), + hour: part("hour"), + minute: part("minute"), + second: part("second"), + } +} + +/** How far `timeZone`'s clock is ahead of UTC at `instant`, in milliseconds. */ +function offsetMs(instant: Date, timeZone: string): number { + const w = wallClock(instant, timeZone) + const wall = Date.UTC(w.year, w.month - 1, w.day, w.hour, w.minute, w.second) + + return wall - Math.floor(instant.getTime() / 1000) * 1000 +} + +/** + * A `datetime-local` value (`YYYY-MM-DDTHH:mm`) read as a time in `timeZone`, + * or `null` when it is not one. + * + * Two passes, because the offset to subtract is the one in force at the + * *result*, which the first guess can miss by an hour when a daylight-saving + * change lies between them. A time skipped in spring lands an hour later; a + * time repeated in autumn resolves to one of its two occurrences. + */ +export function fromEventDateTime( + value: string, + timeZone: string = EVENT_TIME_ZONE, +): Date | null { + const m = DATE_TIME_LOCAL.exec(value) + if (!m) return null + const [year = 0, month = 0, day = 0, hour = 0, minute = 0] = m + .slice(1) + .map(Number) + if (hour > 23 || minute > 59) return null + + const wall = Date.UTC(year, month - 1, day, hour, minute) + const asDate = new Date(wall) + if (asDate.getUTCMonth() !== month - 1 || asDate.getUTCDate() !== day) { + return null + } + + const guess = wall - offsetMs(new Date(wall), timeZone) + + return new Date(wall - offsetMs(new Date(guess), timeZone)) +} + +/** + * An instant as `datetime-local` wants it, on `timeZone`'s clock — the inverse + * of `fromEventDateTime`, so a form saved unchanged stores the same instant. + */ +export function toEventDateTime( + d: Date | undefined, + timeZone: string = EVENT_TIME_ZONE, +): string { + if (!d) return "" + const w = wallClock(new Date(d), timeZone) + const pad = (n: number) => String(n).padStart(2, "0") + + return `${w.year}-${pad(w.month)}-${pad(w.day)}T${pad(w.hour)}:${pad(w.minute)}` +} + +/** + * A form field holding a hackathon start or end: `undefined` when left empty, + * `null` when filled with something that is not a date and time. + */ +export function readEventDateTime( + raw: FormDataEntryValue | null, +): Date | undefined | null { + if (typeof raw !== "string" || raw.trim() === "") return undefined + + return fromEventDateTime(raw.trim()) +} diff --git a/components/frontend/src/routes/(app)/hackathons/create/+page.server.ts b/components/frontend/src/routes/(app)/hackathons/create/+page.server.ts index 9cff9ff8..0f257b7b 100644 --- a/components/frontend/src/routes/(app)/hackathons/create/+page.server.ts +++ b/components/frontend/src/routes/(app)/hackathons/create/+page.server.ts @@ -2,6 +2,7 @@ import type { Actions, PageServerLoad } from "./$types" import { requireGrpc } from "$lib/server/grpc/client" import { GlobalRole } from "$lib/server/grpc/generated/user/entities/global_role" import { Visibility } from "$lib/server/grpc/generated/hackathon/entities/visibility" +import { readEventDateTime } from "$lib/utils/eventTime" import { error, fail, redirect } from "@sveltejs/kit" import { ClientError, Status } from "nice-grpc-common" @@ -35,8 +36,8 @@ export const actions: Actions = { const name = form.get("name") const visibility = form.get("visibility") const description = form.get("description") - const startsAt = form.get("startsAt") - const endsAt = form.get("endsAt") + const startsAt = readEventDateTime(form.get("startsAt")) + const endsAt = readEventDateTime(form.get("endsAt")) const logo = form.get("logo") if (typeof name !== "string" || name.trim().length < 3) { @@ -46,21 +47,20 @@ export const actions: Actions = { return fail(400, { message: "Visibility is required" }) } + if (startsAt === null || endsAt === null) { + return fail(400, { + message: "Start and end must each be a date and a time", + }) + } // Status is computed server-side from both dates, so one without the other // leaves a hackathon that can never be anything but PENDING. - const hasStartsAt = typeof startsAt === "string" && startsAt !== "" - const hasEndsAt = typeof endsAt === "string" && endsAt !== "" - if (hasStartsAt !== hasEndsAt) { + if ((startsAt === undefined) !== (endsAt === undefined)) { return fail(400, { message: "Start and end date must be set together", }) } - if ( - hasStartsAt && - hasEndsAt && - new Date(endsAt as string) < new Date(startsAt as string) - ) { - return fail(400, { message: "End date must not precede the start date" }) + if (startsAt && endsAt && endsAt <= startsAt) { + return fail(400, { message: "The end must be after the start" }) } let hackathonId: string @@ -75,8 +75,8 @@ export const actions: Actions = { typeof description === "string" && description.trim() !== "" ? description : undefined, - startsAt: hasStartsAt ? new Date(startsAt as string) : undefined, - endsAt: hasEndsAt ? new Date(endsAt as string) : undefined, + startsAt, + endsAt, logo: typeof logo === "string" && logo.trim() !== "" ? logo : undefined, }) hackathonId = result.hackathonId diff --git a/components/frontend/src/routes/(app)/hackathons/create/+page.svelte b/components/frontend/src/routes/(app)/hackathons/create/+page.svelte index d01b1810..e0044a0c 100644 --- a/components/frontend/src/routes/(app)/hackathons/create/+page.svelte +++ b/components/frontend/src/routes/(app)/hackathons/create/+page.svelte @@ -60,13 +60,13 @@ diff --git a/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.server.ts b/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.server.ts index 518b86df..8d534435 100644 --- a/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.server.ts +++ b/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.server.ts @@ -1,6 +1,7 @@ import type { Actions, PageServerLoad } from "./$types" import { requireGrpc } from "$lib/server/grpc/client" import { Visibility } from "$lib/server/grpc/generated/hackathon/entities/visibility" +import { readEventDateTime } from "$lib/utils/eventTime" import { canEditHackathon } from "$lib/utils/hackathonRole" import { error, fail, redirect } from "@sveltejs/kit" import { ClientError, Status } from "nice-grpc-common" @@ -23,8 +24,8 @@ export const actions: Actions = { const name = form.get("name") const visibility = form.get("visibility") const description = form.get("description") - const startsAt = form.get("startsAt") - const endsAt = form.get("endsAt") + const startsAt = readEventDateTime(form.get("startsAt")) + const endsAt = readEventDateTime(form.get("endsAt")) const logo = form.get("logo") if (typeof name !== "string" || name.trim().length < 3) { @@ -34,21 +35,20 @@ export const actions: Actions = { return fail(400, { message: "Visibility is required" }) } + if (startsAt === null || endsAt === null) { + return fail(400, { + message: "Start and end must each be a date and a time", + }) + } // Status is computed server-side from both dates, so one without the other // leaves a hackathon that can never be anything but PENDING. - const hasStartsAt = typeof startsAt === "string" && startsAt !== "" - const hasEndsAt = typeof endsAt === "string" && endsAt !== "" - if (hasStartsAt !== hasEndsAt) { + if ((startsAt === undefined) !== (endsAt === undefined)) { return fail(400, { message: "Start and end date must be set together", }) } - if ( - hasStartsAt && - hasEndsAt && - new Date(endsAt as string) < new Date(startsAt as string) - ) { - return fail(400, { message: "End date must not precede the start date" }) + if (startsAt && endsAt && endsAt <= startsAt) { + return fail(400, { message: "The end must be after the start" }) } try { @@ -64,13 +64,13 @@ export const actions: Actions = { // clearing the field on purpose and must reach the backend as "". description: typeof description === "string" ? description : undefined, logo: typeof logo === "string" ? logo : undefined, - // TODO(backend: hackathon-edit-clear-dates): `hasStartsAt === hasEndsAt - // === false` sends both as `undefined`, which `Edit` reads as "leave + // TODO(backend: hackathon-edit-clear-dates): two empty fields send + // both as `undefined`, which `Edit` reads as "leave // unchanged" rather than "clear them" — there is no request that // returns an already-dated hackathon to dateless. Harmless here: dates // can still be *changed* freely, only full removal silently no-ops. - startsAt: hasStartsAt ? new Date(startsAt as string) : undefined, - endsAt: hasEndsAt ? new Date(endsAt as string) : undefined, + startsAt, + endsAt, }) } catch (e) { if (e instanceof ClientError && e.code === Status.INVALID_ARGUMENT) { diff --git a/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.svelte b/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.svelte index 86128670..4dc19938 100644 --- a/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.svelte +++ b/components/frontend/src/routes/(app)/my/hackathon/[id]/manage/edit/+page.svelte @@ -4,6 +4,7 @@ import MarkdownEditor from '$lib/components/forms/MarkdownEditor.svelte'; import PublicHackathonView from '$lib/components/hackathon/PublicHackathonView.svelte'; import InfoButton from '$lib/components/layout/InfoButton.svelte'; + import { fromEventDateTime, toEventDateTime } from '$lib/utils/eventTime'; import type { ActionData, PageData } from './$types'; let { data, form }: { data: PageData; form: ActionData } = $props(); @@ -12,21 +13,10 @@ // Visibility numeric values: PUBLIC=1, PRIVATE=2. const PRIVATE = 2; - function toDateInputValue(d: Date | undefined): string { - if (!d) return ''; - const date = new Date(d); - const offset = date.getTimezoneOffset(); - return new Date(date.getTime() - offset * 60_000).toISOString().slice(0, 10); - } - - // Built as a local date rather than through `new Date('2026-10-24')`, which - // parses as UTC midnight and can render as the day before in a western - // timezone. The preview would then show a date the form does not. - function fromDateInputValue(v: string): Date | undefined { - const m = v.match(/^(\d{4})-(\d{2})-(\d{2})$/); - if (!m) return undefined; - return new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3])); - } + // Start and end are entered on Zurich's clock whatever the browser's zone, + // and read back on it too, so saving the form unchanged keeps the same + // instants. The preview gets the instant the server will store. + const fromDateInputValue = (v: string): Date | undefined => fromEventDateTime(v) ?? undefined; // TODO(backend: hackathon-edit-clear-dates): once a hackathon has dates, // emptying both fields is silently ignored rather than clearing them — see @@ -42,8 +32,8 @@ let draft = $state({ name: hackathon.name, visibility: hackathon.visibility === PRIVATE ? 'private' : 'public', - startsAt: toDateInputValue(hackathon.startsAt), - endsAt: toDateInputValue(hackathon.endsAt), + startsAt: toEventDateTime(hackathon.startsAt), + endsAt: toEventDateTime(hackathon.endsAt), logo: hackathon.logo ?? '', description: hackathon.description ?? '', }); @@ -125,13 +115,23 @@ {#if hasDates}