diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-26 09:03:12 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-26 09:03:12 +0200 |
| commit | f07c98fb888a5b56ba15d307a3afbdaf4209eaa3 (patch) | |
| tree | e5957d6f4aff03ecc63f9a654998ac9776e28ef5 /src/lib/shared/temporal.ts | |
| parent | a89dd5935ba80d83c1c4802c8dd2d2ddec38f345 (diff) | |
Allow backdating a routine completion
Diffstat (limited to 'src/lib/shared/temporal.ts')
| -rw-r--r-- | src/lib/shared/temporal.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lib/shared/temporal.ts b/src/lib/shared/temporal.ts index 103f209..6fd085d 100644 --- a/src/lib/shared/temporal.ts +++ b/src/lib/shared/temporal.ts @@ -24,3 +24,29 @@ export function formatDayMonthYear(iso: string): string { year: 'numeric', }); } + +const ONE_DAY_MS = 86_400_000; + +/** + * Stamp to store for a `YYYY-MM-DD` form day: noon UTC — a midnight stamp can + * read back as the wrong day in a timezone ahead of UTC. Clamped to `now` when + * the day is still ahead of the clock, and null for a malformed day or one + * more than a day in the future (timezones ahead of UTC make "today" fuzzy). + */ +export function instantFromDay( + day: string, + now: Temporal.Instant, +): string | null { + let midnight: Temporal.Instant; + try { + midnight = Temporal.PlainDate.from(day.trim()) + .toZonedDateTime('UTC') + .toInstant(); + } catch { + return null; + } + const noon = midnight.add({ hours: 12 }); + if (noon.epochMilliseconds - now.epochMilliseconds > ONE_DAY_MS) return null; + const stamp = Temporal.Instant.compare(noon, now) > 0 ? now : noon; + return stamp.toString({ smallestUnit: 'millisecond' }); +} |
