@open-mercato/core 0.10.1-develop.7724.1.95e4e7d38e → 0.10.1-develop.7726.1.a447cc5525

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ import { createLogger } from '@open-mercato/shared/lib/logger'
2
+
3
+ const logger = createLogger('planner').child({ component: 'availability' })
4
+
5
+ const FALLBACK_TIME_ZONE = 'UTC'
6
+ const DATE_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}$/
7
+
8
+ /**
9
+ * Availability rows store `timezone` as free-form `text`, so a malformed value
10
+ * must not turn a read or a write into a 500. Unknown zones degrade to UTC,
11
+ * which is already how the expander interprets every stored instant.
12
+ */
13
+ export function resolveTimeZone(value: string | null | undefined): string {
14
+ if (!isResolvableTimeZone(value)) return FALLBACK_TIME_ZONE
15
+ return value.trim()
16
+ }
17
+
18
+ export function isResolvableTimeZone(value: string | null | undefined): value is string {
19
+ if (typeof value !== 'string' || !value.trim().length) return false
20
+ try {
21
+ new Intl.DateTimeFormat('en-US', { timeZone: value.trim() })
22
+ return true
23
+ } catch {
24
+ return false
25
+ }
26
+ }
27
+
28
+ /**
29
+ * The replace endpoints anchor `DTSTART` in the declared zone, so an
30
+ * unresolvable value is persisted as a UTC-anchored instant under a label
31
+ * nothing can resolve. Rejecting it with a 400 would narrow the replace
32
+ * schemas (a `data/validators.ts` contract surface), so for now the write is
33
+ * accepted and flagged; the 400 is announced in UPGRADE_NOTES.md.
34
+ */
35
+ export function warnOnUnresolvableTimeZone(value: string, commandId: string): void {
36
+ if (isResolvableTimeZone(value)) return
37
+ logger.warn('[deprecated] Unresolvable availability timezone anchored to UTC; this will be rejected with 400 in a future minor release', {
38
+ commandId,
39
+ timezone: value,
40
+ fallback: FALLBACK_TIME_ZONE,
41
+ })
42
+ }
43
+
44
+ function parseTimeOfDay(value: string): { hours: number; minutes: number } | null {
45
+ const [hours, minutes] = String(value).split(':').map((part) => Number(part))
46
+ if (!Number.isFinite(hours) || !Number.isFinite(minutes)) return null
47
+ if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) return null
48
+ return { hours, minutes }
49
+ }
50
+
51
+ type WallClock = { year: number; month: number; day: number; hours: number; minutes: number; seconds: number }
52
+
53
+ const MINUTE_MS = 60 * 1000
54
+ const DAY_MS = 24 * 60 * MINUTE_MS
55
+ const formatterCache = new Map<string, Intl.DateTimeFormat>()
56
+
57
+ function zonedFormatter(timeZone: string): Intl.DateTimeFormat {
58
+ let formatter = formatterCache.get(timeZone)
59
+ if (!formatter) {
60
+ formatter = new Intl.DateTimeFormat('en-US', {
61
+ timeZone,
62
+ hourCycle: 'h23',
63
+ year: 'numeric',
64
+ month: '2-digit',
65
+ day: '2-digit',
66
+ hour: '2-digit',
67
+ minute: '2-digit',
68
+ second: '2-digit',
69
+ })
70
+ formatterCache.set(timeZone, formatter)
71
+ }
72
+ return formatter
73
+ }
74
+
75
+ /**
76
+ * Wall clock of an instant in `timeZone`, read through `Intl` only. `date-fns-tz`
77
+ * seeds its offset search with host-local getters, so around DST transitions
78
+ * the same input resolved differently depending on the serving machine's `TZ`.
79
+ */
80
+ function zonedWallClock(epochMs: number, timeZone: string): WallClock {
81
+ const values: Record<string, number> = {}
82
+ for (const part of zonedFormatter(timeZone).formatToParts(new Date(epochMs))) {
83
+ if (part.type !== 'literal') values[part.type] = Number(part.value)
84
+ }
85
+ return {
86
+ year: values.year,
87
+ month: values.month,
88
+ day: values.day,
89
+ hours: values.hour,
90
+ minutes: values.minute,
91
+ seconds: values.second,
92
+ }
93
+ }
94
+
95
+ function wallClockAsUtc(clock: WallClock): number {
96
+ return Date.UTC(clock.year, clock.month - 1, clock.day, clock.hours, clock.minutes, clock.seconds)
97
+ }
98
+
99
+ function offsetAt(epochMs: number, timeZone: string): number {
100
+ const flooredToMinute = Math.floor(epochMs / MINUTE_MS) * MINUTE_MS
101
+ return wallClockAsUtc(zonedWallClock(flooredToMinute, timeZone)) - flooredToMinute
102
+ }
103
+
104
+ function parseDateKey(dateKey: string): { year: number; month: number; day: number } | null {
105
+ if (!DATE_KEY_PATTERN.test(String(dateKey))) return null
106
+ const [year, month, day] = dateKey.split('-').map((part) => Number(part))
107
+ const probe = new Date(Date.UTC(year, month - 1, day))
108
+ if (probe.getUTCMonth() !== month - 1 || probe.getUTCDate() !== day) return null
109
+ return { year, month, day }
110
+ }
111
+
112
+ function formatUtcDateKey(epochMs: number): string {
113
+ return new Date(epochMs).toISOString().slice(0, 10)
114
+ }
115
+
116
+ /**
117
+ * `2026-06-15` + `09:00` + `Europe/Warsaw` → the absolute instant that wall
118
+ * time denotes. Replaces `new Date(y, m, d, h, min)`, whose result depended on
119
+ * the host process's `TZ` rather than on the zone the caller declared (#5862).
120
+ *
121
+ * DST transitions follow the `compatible` disambiguation of RFC 5545 §3.3.5 and
122
+ * Temporal: a repeated wall time resolves to its EARLIER occurrence, and a
123
+ * skipped one moves forward by the length of the gap (`Africa/Cairo` has no
124
+ * 2026-04-24 00:00, so it becomes 01:00 — the first instant of that date).
125
+ */
126
+ export function zonedWallTimeToInstant(dateKey: string, time: string, timeZone: string): Date | null {
127
+ const date = parseDateKey(dateKey)
128
+ const parsed = parseTimeOfDay(time)
129
+ if (!date || !parsed) return null
130
+ const wall = new Date(Date.UTC(date.year, date.month - 1, date.day, parsed.hours, parsed.minutes))
131
+ return zonedWallDateToInstant(wall, timeZone)
132
+ }
133
+
134
+ /** Resolve a UTC-encoded wall clock with compatible DST disambiguation. */
135
+ export function zonedWallDateToInstant(wall: Date, timeZone: string): Date {
136
+ const zone = resolveTimeZone(timeZone)
137
+ const wallAsUtc = wall.getTime()
138
+ const offsetBefore = offsetAt(wallAsUtc - DAY_MS, zone)
139
+ const offsetAfter = offsetAt(wallAsUtc + DAY_MS, zone)
140
+ if (offsetBefore === offsetAfter) return new Date(wallAsUtc - offsetBefore)
141
+ const candidates = [wallAsUtc - offsetBefore, wallAsUtc - offsetAfter].sort((left, right) => left - right)
142
+ const matching = candidates.find((candidate) => candidate + offsetAt(candidate, zone) === wallAsUtc)
143
+ return new Date(matching ?? wallAsUtc - offsetBefore)
144
+ }
145
+
146
+ /**
147
+ * The span a full-day block covers: from the first instant of `dateKey` to the
148
+ * first instant of the following date, both in `timeZone`. That is 23 or 25
149
+ * hours on DST days, so the block neither bleeds into nor falls short of its
150
+ * neighbours.
151
+ */
152
+ export function zonedDayBounds(dateKey: string, timeZone: string): { start: Date; end: Date } | null {
153
+ const date = parseDateKey(dateKey)
154
+ if (!date) return null
155
+ const nextDateKey = formatUtcDateKey(Date.UTC(date.year, date.month - 1, date.day) + DAY_MS)
156
+ const start = zonedWallTimeToInstant(dateKey, '00:00', timeZone)
157
+ const end = zonedWallTimeToInstant(nextDateKey, '00:00', timeZone)
158
+ if (!start || !end || end <= start) return null
159
+ return { start, end }
160
+ }
161
+
162
+ /**
163
+ * The calendar date an instant falls on, as seen from `timeZone`.
164
+ *
165
+ * Which day an existing rule belongs to has to be read in that rule's own zone,
166
+ * the same zone its `DTSTART` was anchored in. Derived from the host clock
167
+ * instead, replace-by-date selected a different set of rows — and the 403 gate
168
+ * in `api/availability-date-specific.ts` admitted or refused the same request —
169
+ * depending on which machine served it.
170
+ */
171
+ export function zonedDateKey(instant: Date, timeZone: string): string {
172
+ const clock = zonedWallClock(instant.getTime(), resolveTimeZone(timeZone))
173
+ return formatUtcDateKey(Date.UTC(clock.year, clock.month - 1, clock.day))
174
+ }
175
+
176
+ /** `Date.getDay()` index (0 = Sunday) of an instant, as seen from `timeZone`. */
177
+ export function zonedWeekday(instant: Date, timeZone: string): number {
178
+ const clock = zonedWallClock(instant.getTime(), resolveTimeZone(timeZone))
179
+ return new Date(Date.UTC(clock.year, clock.month - 1, clock.day)).getUTCDay()
180
+ }
181
+
182
+ /**
183
+ * The next occurrence of `weekday` (0 = Sunday) at or after `from`, as a
184
+ * calendar date key in `timeZone`. The zone matters: at 12:00 UTC on a Monday
185
+ * it is already Tuesday in `Pacific/Auckland`, which shifted the whole series
186
+ * by a week before this was zone-aware.
187
+ */
188
+ export function nextWeekdayDateKey(weekday: number, timeZone: string, from: Date = new Date()): string {
189
+ const clock = zonedWallClock(from.getTime(), resolveTimeZone(timeZone))
190
+ const localMidnight = Date.UTC(clock.year, clock.month - 1, clock.day)
191
+ const diff = (weekday - new Date(localMidnight).getUTCDay() + 7) % 7
192
+ return formatUtcDateKey(localMidnight + diff * DAY_MS)
193
+ }
194
+
195
+ /**
196
+ * The start and end instants of the occurrence a weekly window is anchored on.
197
+ *
198
+ * The anchor's exact length becomes the rule's `DURATION`, which every reader
199
+ * applies to every week. An anchor that spans a DST transition (Europe/Warsaw
200
+ * Sunday 00:00–08:00 on 2026-10-25 lasts 9 hours) or starts in a gap would
201
+ * therefore misstate the window for all ordinary weeks, so the upcoming
202
+ * occurrence is used only when both local endpoints and its exact length match
203
+ * the request; otherwise the previous week's occurrence anchors the series. Taking
204
+ * the earlier week keeps the upcoming occurrence inside the series for readers
205
+ * that expand forward from `DTSTART`.
206
+ *
207
+ * `from` is threaded in rather than defaulted per call: resolving a window's
208
+ * start and end against two separate `new Date()` reads can straddle midnight
209
+ * in the declared zone, which lands the anchors seven days apart.
210
+ */
211
+ export function zonedWeeklyAnchor(
212
+ weekday: number,
213
+ startTime: string,
214
+ endTime: string,
215
+ timeZone: string,
216
+ from: Date,
217
+ ): { start: Date; end: Date } | null {
218
+ const startClock = parseTimeOfDay(startTime)
219
+ const endClock = parseTimeOfDay(endTime)
220
+ if (!startClock || !endClock) return null
221
+ const zone = resolveTimeZone(timeZone)
222
+ const startMinutes = startClock.hours * 60 + startClock.minutes
223
+ const endMinutes = endClock.hours * 60 + endClock.minutes
224
+ const localLengthMs = (endMinutes - startMinutes) * MINUTE_MS
225
+ const upcoming = parseDateKey(nextWeekdayDateKey(weekday, timeZone, from))
226
+ if (!upcoming) return null
227
+ const upcomingMidnight = Date.UTC(upcoming.year, upcoming.month - 1, upcoming.day)
228
+ let fallback: { start: Date; end: Date } | null = null
229
+ for (const weekOffset of [0, -1]) {
230
+ const midnight = upcomingMidnight + weekOffset * 7 * DAY_MS
231
+ const dateKey = formatUtcDateKey(midnight)
232
+ const start = zonedWallTimeToInstant(dateKey, startTime, timeZone)
233
+ const end = zonedWallTimeToInstant(dateKey, endTime, timeZone)
234
+ if (!start || !end) return null
235
+ const startWall = wallClockAsUtc(zonedWallClock(start.getTime(), zone))
236
+ const endWall = wallClockAsUtc(zonedWallClock(end.getTime(), zone))
237
+ if (startWall !== midnight + startMinutes * MINUTE_MS || endWall !== midnight + endMinutes * MINUTE_MS) continue
238
+ if (end.getTime() - start.getTime() === localLengthMs) return { start, end }
239
+ fallback ??= { start, end }
240
+ }
241
+ return fallback
242
+ }