Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions components/frontend/src/lib/utils/eventTime.test.ts
Original file line number Diff line number Diff line change
@@ -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",
)
})
})
113 changes: 113 additions & 0 deletions components/frontend/src/lib/utils/eventTime.ts
Original file line number Diff line number Diff line change
@@ -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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
</fieldset>

<label class="field-label">
Starts at (optional)
<input type="date" name="startsAt" class="field" />
Starts at, Zurich time (optional)
<input type="datetime-local" name="startsAt" class="field" />
</label>

<label class="field-label">
Ends at (optional)
<input type="date" name="endsAt" class="field" />
Ends at, Zurich time (optional)
<input type="datetime-local" name="endsAt" class="field" />
</label>

<ImageUrlField name="logo" label="Logo URL (optional)" class="sm:col-span-2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
Loading
Loading