Repository navigation
Migrate timezone/date utilities to Temporal (bundled polyfill) #1321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
9026d8b
b880f8e
8ba59c1
13eece6
4ce3466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 */ | ||
|
|
@@ -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(); | ||
|
|
||
| /** | ||
| * 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" }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For submissions that include a numeric offset or bracketed zone, such as Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — confirmed and fixed in b880f8e. Generated by Claude Code |
||
| .toInstant() | ||
| .toString({ fractionalSecondDigits: 3 }); | ||
| } catch { | ||
| throw new Error(`Invalid datetime: ${naive}`); | ||
| } | ||
|
|
@@ -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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When date-dependent code is exercised under
@std/testing/time'sFakeTime, this now reads the real wall clock becauseFakeTimeoverridesDate/timers whileTemporal.Nowuses the runtime clock directly. The previous@internationalized/datetoday()path was backed byDate.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 fromDate.now()before converting to the requested timezone to keep this helper controllable.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
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
FakeTimefrozen at 2030-01-15,Temporal.Now.plainDateISOreturned the real date while aDate.now()-derived instant returned the frozen one.todayInTznow derives "today" fromDate.now()(via a sharedmsToZonedhelper thatutcToZonedalso uses), restoring the freezable behaviour the oldtoday()had. Added aFakeTimetest that locks it in.server-qr-book/renewals(FakeTime + todayInTz) still pass via the harness.Generated by Claude Code