Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"esbuild": "npm:esbuild@^0.28.0",
"sass": "npm:sass@^1.101.0",
"@bunny.net/edgescript-sdk": "npm:@bunny.net/edgescript-sdk@^0.12.1",
"@internationalized/date": "npm:@internationalized/date@^3.12.0",
"@bunny.net/storage-sdk": "npm:@bunny.net/storage-sdk@^0.3.1",
"@botpoison/browser": "npm:@botpoison/browser@^0.1.30",
"jsqr": "npm:jsqr@^1.4.0",
Expand Down
14 changes: 0 additions & 14 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions src/shared/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* Date computation for daily listings
*/

import { fromAbsolute } from "@internationalized/date";
import { filter } from "#fp";
import { settings } from "#shared/db/settings.ts";
import {
formatDatetimeInTz,
formatDatetimeShortInTz,
localToUtc,
todayInTz,
utcToZoned,
} from "#shared/timezone.ts";
import {
type Holiday,
Expand Down Expand Up @@ -449,14 +449,9 @@ export const formatTimeAgo = (
* Used by the calendar view to map standard listing dates to calendar days.
*/
export const listingDateToCalendarDate = (utcIso: string): string | null => {
const tz = settings.timezone;
if (!utcIso) return null;
try {
const ms = new Date(utcIso).getTime();
if (Number.isNaN(ms)) return null;
const zoned = fromAbsolute(ms, tz);
const pad = (n: number) => String(n).padStart(2, "0");
return `${zoned.year}-${pad(zoned.month)}-${pad(zoned.day)}`;
return utcToZoned(utcIso, settings.timezone).toPlainDate().toString();
} catch {
return null;
}
Expand Down
36 changes: 19 additions & 17 deletions src/shared/timezone.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
/**
* Timezone conversion utilities using @internationalized/date.
* Timezone conversion utilities built on the standard Temporal API.
*
* Wraps the library to provide simple string-in/string-out functions
* for the rest of the codebase, with correct DST handling and
* explicit disambiguation.
* Provides simple string-in/string-out functions for the rest of the
* codebase, with correct DST handling and explicit disambiguation.
*/

import {
fromAbsolute,
today as libToday,
parseDateTime,
toZoned,
} from "@internationalized/date";
import { formatIsoForPreview } from "#shared/bulk-replace.ts";

/** Default timezone when none is configured */
Expand All @@ -21,26 +14,35 @@ export const DEFAULT_TIMEZONE = "Europe/London";
const pad2 = (n: number): string => String(n).padStart(2, "0");

/** Parse a UTC ISO string into a ZonedDateTime in the given timezone */
const utcToZoned = (utcIso: string, tz: string) =>
fromAbsolute(new Date(utcIso).getTime(), tz);
export const utcToZoned = (
utcIso: string,
tz: string,
): Temporal.ZonedDateTime =>
Temporal.Instant.fromEpochMilliseconds(
new Date(utcIso).getTime(),
).toZonedDateTimeISO(tz);

/**
* Get today's date as YYYY-MM-DD in the given timezone.
*/
export const todayInTz = (tz: string): string => libToday(tz).toString();
export const todayInTz = (tz: string): string =>
Temporal.Now.plainDateISO(tz).toString();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the fakeable clock for todayInTz

When date-dependent code is exercised under @std/testing/time's FakeTime, this now reads the real wall clock because FakeTime overrides Date/timers while Temporal.Now uses the runtime clock directly. The previous @internationalized/date today() path was backed by Date.now(), so fake-time tests for booking windows, holiday cutoffs, or delivery/calendar pages can now calculate “today” from the actual date instead of the frozen date; derive the Temporal instant from Date.now() before converting to the requested timezone to keep this helper controllable.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and fixed in 8ba59c1. Verified empirically: under FakeTime frozen at 2030-01-15, Temporal.Now.plainDateISO returned the real date while a Date.now()-derived instant returned the frozen one. todayInTz now derives "today" from Date.now() (via a shared msToZoned helper that utcToZoned also uses), restoring the freezable behaviour the old today() had. Added a FakeTime test that locks it in. server-qr-book/renewals (FakeTime + todayInTz) still pass via the harness.


Generated by Claude Code


/**
* Convert a naive datetime-local value (YYYY-MM-DDTHH:MM) to a UTC ISO string,
* interpreting the value as local time in the given timezone.
*
* Uses 'compatible' disambiguation: spring-forward gaps resolve to the later
* (post-transition) time; fall-back overlaps resolve to the earlier occurrence.
* `overflow: "reject"` rejects impossible calendar dates (e.g. 2026-02-30)
* rather than silently clamping them.
*/
export const localToUtc = (naive: string, tz: string): string => {
try {
const dt = parseDateTime(naive);
const zoned = toZoned(dt, tz, "compatible");
return zoned.toAbsoluteString();
return Temporal.PlainDateTime.from(naive, { overflow: "reject" })
.toZonedDateTime(tz, { disambiguation: "compatible" })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject offset-bearing datetime inputs

For submissions that include a numeric offset or bracketed zone, such as 2026-06-15T14:30+09:00 or 2026-06-15T14:30[Asia/Tokyo], Temporal.PlainDateTime.from accepts the string but ignores that offset/zone before converting with settings.timezone. The previous parseDateTime path rejected these non-naive values, so this now silently stores a different instant instead of failing validation; keep the datetime-local parser strict before calling toZonedDateTime.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — confirmed and fixed in b880f8e. Temporal.PlainDateTime.from does silently drop a numeric offset or bracketed zone, where the old parseDateTime rejected them. Both localToUtc and isValidDatetime now go through a shared parseNaiveDateTime helper that requires the strict naive datetime-local shape (no Z/offset/zone) before delegating calendar-validity to overflow: "reject". Added tests for the offset, bracketed-zone, and Z cases.


Generated by Claude Code

.toInstant()
.toString({ fractionalSecondDigits: 3 });
} catch {
throw new Error(`Invalid datetime: ${naive}`);
}
Expand Down Expand Up @@ -107,7 +109,7 @@ export const isValidTimezone = (tz: string): boolean => {
*/
export const isValidDatetime = (value: string): boolean => {
try {
parseDateTime(value);
Temporal.PlainDateTime.from(value, { overflow: "reject" });
return true;
} catch {
return false;
Expand Down