@hogsend/core 0.0.1 → 0.1.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hogsend/core",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -16,6 +16,7 @@
16
16
  "./types": "./src/types/index.ts",
17
17
  "./registry": "./src/registry/index.ts",
18
18
  "./conditions": "./src/conditions/index.ts",
19
+ "./schedule": "./src/schedule/index.ts",
19
20
  "./schemas": "./src/schemas/index.ts"
20
21
  },
21
22
  "files": [
@@ -26,15 +27,20 @@
26
27
  "access": "public"
27
28
  },
28
29
  "dependencies": {
30
+ "@js-temporal/polyfill": "^0.5.1",
29
31
  "drizzle-orm": "^0.45.2",
32
+ "iana-db-timezones": "^0.3.0",
30
33
  "zod": "^4.4.3",
31
- "@hogsend/db": "^0.0.1"
34
+ "@hogsend/db": "^0.1.0"
32
35
  },
33
36
  "devDependencies": {
34
37
  "@types/node": "latest",
38
+ "vitest": "^4.1.8",
35
39
  "@repo/typescript-config": "0.0.0"
36
40
  },
37
41
  "scripts": {
38
- "check-types": "tsc --noEmit"
42
+ "check-types": "tsc --noEmit",
43
+ "test": "vitest run",
44
+ "test:watch": "vitest"
39
45
  }
40
46
  }
package/src/index.ts CHANGED
@@ -12,5 +12,6 @@ export {
12
12
  minutes,
13
13
  } from "./duration.js";
14
14
  export { JourneyRegistry } from "./registry/index.js";
15
+ export * from "./schedule/index.js";
15
16
  export { journeyMetaSchema } from "./schemas/index.js";
16
17
  export * from "./types/index.js";
@@ -0,0 +1,10 @@
1
+ export {
2
+ resolveAfter,
3
+ resolveNextLocalTime,
4
+ resolveNextWeekday,
5
+ resolveTomorrow,
6
+ type ScheduleOptions,
7
+ } from "./resolvers.js";
8
+ export { parseTimeOfDay, weekdayToIso } from "./time.js";
9
+ export { isValidTimeZone, type TimeZone } from "./tz.js";
10
+ export { clampToWindow, type SendWindow } from "./window.js";
@@ -0,0 +1,136 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import type { DurationObject } from "../duration.js";
3
+ import { durationToMs } from "../duration.js";
4
+ import type { IfPast, Weekday } from "../types/journey-context.js";
5
+ import { parseTimeOfDay, weekdayToIso } from "./time.js";
6
+ import { clampToWindow, type SendWindow } from "./window.js";
7
+
8
+ export interface ScheduleOptions {
9
+ /** Resolved IANA timezone, e.g. "America/New_York". */
10
+ timezone: string;
11
+ /** Explicit "current" instant — no ambient `Date.now()`. */
12
+ now: Date;
13
+ /** Optional quiet-hours window in the same tz. */
14
+ window?: SendWindow;
15
+ /** How to treat a naive resolved instant that is already <= now. */
16
+ ifPast?: IfPast;
17
+ }
18
+
19
+ /**
20
+ * Convert a `ZonedDateTime` to a `Date`, applying the `ifPast` policy and the
21
+ * optional send window. This is the shared tail of every resolver:
22
+ *
23
+ * 1. `ifPast` — `"next"` (default) rolls the instant forward by `rollDays` when
24
+ * it is at/before `now` (never fewer than 1 day, so a snapped past instant
25
+ * always lands in the future); `"now"` clamps it to the current instant.
26
+ * 2. `clampToWindow` — if a window is configured, the final instant is mapped
27
+ * into the open window (the LAST step, after `ifPast`).
28
+ */
29
+ function finalize(
30
+ candidate: Temporal.ZonedDateTime,
31
+ opts: ScheduleOptions,
32
+ rollDays: number,
33
+ ): Date {
34
+ const nowMs = opts.now.getTime();
35
+ let resolved = candidate;
36
+
37
+ if (resolved.epochMilliseconds <= nowMs) {
38
+ if ((opts.ifPast ?? "next") === "now") {
39
+ resolved = Temporal.Instant.fromEpochMilliseconds(
40
+ nowMs,
41
+ ).toZonedDateTimeISO(opts.timezone);
42
+ } else {
43
+ // ifPast "next": always roll forward. Resolvers that snap to an
44
+ // arbitrary HH:mm (resolveAfter / resolveTomorrow) pass rollDays 0, so a
45
+ // snapped time landing at/before now must still advance at least one day
46
+ // — otherwise a past instant would be returned and fire immediately.
47
+ resolved = resolved.add({ days: Math.max(rollDays, 1) });
48
+ }
49
+ }
50
+
51
+ const instant = new Date(resolved.epochMilliseconds);
52
+ return opts.window
53
+ ? clampToWindow(instant, opts.window, opts.timezone)
54
+ : instant;
55
+ }
56
+
57
+ /**
58
+ * Build the wall-clock `HH:mm` instant on `date` in `opts.timezone`.
59
+ *
60
+ * Disambiguation `"compatible"` (the JS-`Date` rule) is the documented choice:
61
+ * on a spring-forward gap it yields the post-gap instant ("first valid instant
62
+ * going forward"); on a fall-back overlap it yields the earlier occurrence.
63
+ */
64
+ function atTime(
65
+ date: Temporal.PlainDate,
66
+ time: string,
67
+ timezone: string,
68
+ ): Temporal.ZonedDateTime {
69
+ const { hour, minute } = parseTimeOfDay(time);
70
+ return date
71
+ .toPlainDateTime(Temporal.PlainTime.from({ hour, minute }))
72
+ .toZonedDateTime(timezone, { disambiguation: "compatible" });
73
+ }
74
+
75
+ function nowZoned(opts: ScheduleOptions): Temporal.ZonedDateTime {
76
+ return Temporal.Instant.fromEpochMilliseconds(
77
+ opts.now.getTime(),
78
+ ).toZonedDateTimeISO(opts.timezone);
79
+ }
80
+
81
+ /**
82
+ * Next occurrence of `HH:mm` local: today if still in the future (per
83
+ * `ifPast`), otherwise tomorrow.
84
+ */
85
+ export function resolveNextLocalTime(
86
+ time: string,
87
+ opts: ScheduleOptions,
88
+ ): Date {
89
+ const candidate = atTime(nowZoned(opts).toPlainDate(), time, opts.timezone);
90
+ return finalize(candidate, opts, 1);
91
+ }
92
+
93
+ /**
94
+ * Upcoming `weekday` at `HH:mm` local. If today IS the weekday and the time is
95
+ * still in the future, returns today; otherwise the next matching weekday
96
+ * (1..7 days ahead).
97
+ */
98
+ export function resolveNextWeekday(
99
+ weekday: Weekday,
100
+ time: string,
101
+ opts: ScheduleOptions,
102
+ ): Date {
103
+ const targetIso = weekdayToIso(weekday);
104
+ const today = nowZoned(opts).toPlainDate();
105
+ const delta = (targetIso - today.dayOfWeek + 7) % 7;
106
+ const candidate = atTime(today.add({ days: delta }), time, opts.timezone);
107
+ // If the chosen day is today (delta === 0) but the time already passed, roll
108
+ // a full week forward rather than a single day.
109
+ return finalize(candidate, opts, 7);
110
+ }
111
+
112
+ /** Tomorrow (now + 1 calendar day in tz) at `HH:mm` local. */
113
+ export function resolveTomorrow(time: string, opts: ScheduleOptions): Date {
114
+ const tomorrow = nowZoned(opts).toPlainDate().add({ days: 1 });
115
+ const candidate = atTime(tomorrow, time, opts.timezone);
116
+ // Tomorrow at HH:mm is already in the future; rollDays 0 means finalize only
117
+ // intervenes for ifPast="now" or the defensive 1-day roll if it ever lands
118
+ // at/before now.
119
+ return finalize(candidate, opts, 0);
120
+ }
121
+
122
+ /**
123
+ * `now` + `duration`, then snapped to `HH:mm` local on the resulting calendar
124
+ * day.
125
+ */
126
+ export function resolveAfter(
127
+ duration: DurationObject,
128
+ time: string,
129
+ opts: ScheduleOptions,
130
+ ): Date {
131
+ const shifted = Temporal.Instant.fromEpochMilliseconds(
132
+ opts.now.getTime() + durationToMs(duration),
133
+ ).toZonedDateTimeISO(opts.timezone);
134
+ const candidate = atTime(shifted.toPlainDate(), time, opts.timezone);
135
+ return finalize(candidate, opts, 0);
136
+ }
@@ -0,0 +1,283 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, expect, it } from "vitest";
3
+ import { days, hours } from "../duration.js";
4
+ import {
5
+ clampToWindow,
6
+ isValidTimeZone,
7
+ parseTimeOfDay,
8
+ resolveAfter,
9
+ resolveNextLocalTime,
10
+ resolveNextWeekday,
11
+ resolveTomorrow,
12
+ weekdayToIso,
13
+ } from "./index.js";
14
+
15
+ const NY = "America/New_York";
16
+
17
+ /** Build a Date from a wall-clock time in a tz (compatible disambiguation). */
18
+ function at(
19
+ tz: string,
20
+ y: number,
21
+ m: number,
22
+ d: number,
23
+ h: number,
24
+ min: number,
25
+ ): Date {
26
+ const zdt = Temporal.PlainDateTime.from({
27
+ year: y,
28
+ month: m,
29
+ day: d,
30
+ hour: h,
31
+ minute: min,
32
+ }).toZonedDateTime(tz, { disambiguation: "compatible" });
33
+ return new Date(zdt.epochMilliseconds);
34
+ }
35
+
36
+ /** UTC offset string ("-04:00") of a Date in a tz. */
37
+ function offsetOf(date: Date, tz: string): string {
38
+ return Temporal.Instant.fromEpochMilliseconds(
39
+ date.getTime(),
40
+ ).toZonedDateTimeISO(tz).offset;
41
+ }
42
+
43
+ /** Local wall-clock "HH:mm" of a Date in a tz. */
44
+ function wall(date: Date, tz: string): string {
45
+ const zdt = Temporal.Instant.fromEpochMilliseconds(
46
+ date.getTime(),
47
+ ).toZonedDateTimeISO(tz);
48
+ return `${String(zdt.hour).padStart(2, "0")}:${String(zdt.minute).padStart(
49
+ 2,
50
+ "0",
51
+ )}`;
52
+ }
53
+
54
+ describe("parseTimeOfDay", () => {
55
+ it("parses HH:mm", () => {
56
+ expect(parseTimeOfDay("08:30")).toEqual({ hour: 8, minute: 30 });
57
+ expect(parseTimeOfDay("23:59")).toEqual({ hour: 23, minute: 59 });
58
+ });
59
+ it("throws on malformed / out-of-range", () => {
60
+ expect(() => parseTimeOfDay("8h")).toThrow();
61
+ expect(() => parseTimeOfDay("24:00")).toThrow();
62
+ expect(() => parseTimeOfDay("12:60")).toThrow();
63
+ });
64
+ });
65
+
66
+ describe("weekdayToIso", () => {
67
+ it("maps short and full names to the same ISO weekday", () => {
68
+ expect(weekdayToIso("tue")).toBe(2);
69
+ expect(weekdayToIso("tuesday")).toBe(2);
70
+ expect(weekdayToIso("sun")).toBe(7);
71
+ expect(weekdayToIso("monday")).toBe(1);
72
+ });
73
+ });
74
+
75
+ describe("resolveNextLocalTime", () => {
76
+ it("returns same-day when time is still future", () => {
77
+ const now = at(NY, 2026, 6, 1, 10, 0);
78
+ const got = resolveNextLocalTime("14:00", { timezone: NY, now });
79
+ expect(got).toEqual(at(NY, 2026, 6, 1, 14, 0));
80
+ });
81
+
82
+ it("rolls to next day when past (ifPast default 'next')", () => {
83
+ const now = at(NY, 2026, 6, 1, 15, 0);
84
+ const got = resolveNextLocalTime("14:00", { timezone: NY, now });
85
+ expect(got).toEqual(at(NY, 2026, 6, 2, 14, 0));
86
+ });
87
+
88
+ it("ifPast 'now' clamps to the current instant", () => {
89
+ const now = at(NY, 2026, 6, 1, 15, 0);
90
+ const got = resolveNextLocalTime("14:00", {
91
+ timezone: NY,
92
+ now,
93
+ ifPast: "now",
94
+ });
95
+ expect(got.getTime()).toBe(now.getTime());
96
+ });
97
+
98
+ it("exact equality (now == target) rolls forward under 'next'", () => {
99
+ const now = at(NY, 2026, 6, 1, 14, 0);
100
+ const got = resolveNextLocalTime("14:00", { timezone: NY, now });
101
+ expect(got).toEqual(at(NY, 2026, 6, 2, 14, 0));
102
+ });
103
+ });
104
+
105
+ describe("resolveNextWeekday", () => {
106
+ it("short/full names produce identical instants", () => {
107
+ const now = at(NY, 2026, 6, 1, 9, 0); // Monday
108
+ const a = resolveNextWeekday("tue", "08:00", { timezone: NY, now });
109
+ const b = resolveNextWeekday("tuesday", "08:00", { timezone: NY, now });
110
+ expect(a).toEqual(b);
111
+ });
112
+
113
+ it("Mon → next Tuesday is tomorrow", () => {
114
+ const now = at(NY, 2026, 6, 1, 9, 0); // Monday 2026-06-01
115
+ const got = resolveNextWeekday("tuesday", "08:00", { timezone: NY, now });
116
+ expect(got).toEqual(at(NY, 2026, 6, 2, 8, 0));
117
+ });
118
+
119
+ it("Tue 06:00 → today 08:00 (same-day future)", () => {
120
+ const now = at(NY, 2026, 6, 2, 6, 0); // Tuesday
121
+ const got = resolveNextWeekday("tuesday", "08:00", { timezone: NY, now });
122
+ expect(got).toEqual(at(NY, 2026, 6, 2, 8, 0));
123
+ });
124
+
125
+ it("Tue 09:00 → next Tuesday (7 days)", () => {
126
+ const now = at(NY, 2026, 6, 2, 9, 0); // Tuesday
127
+ const got = resolveNextWeekday("tuesday", "08:00", { timezone: NY, now });
128
+ expect(got).toEqual(at(NY, 2026, 6, 9, 8, 0));
129
+ });
130
+
131
+ it("Sunday → next Monday is tomorrow (week wrap)", () => {
132
+ const now = at(NY, 2026, 6, 7, 12, 0); // Sunday 2026-06-07
133
+ const got = resolveNextWeekday("monday", "08:00", { timezone: NY, now });
134
+ expect(got).toEqual(at(NY, 2026, 6, 8, 8, 0));
135
+ });
136
+ });
137
+
138
+ describe("resolveTomorrow", () => {
139
+ it("returns tomorrow at HH:mm", () => {
140
+ const now = at(NY, 2026, 6, 1, 10, 0);
141
+ const got = resolveTomorrow("08:00", { timezone: NY, now });
142
+ expect(got).toEqual(at(NY, 2026, 6, 2, 8, 0));
143
+ });
144
+ });
145
+
146
+ describe("resolveAfter", () => {
147
+ it("now + 2 days snapped to 08:00 local", () => {
148
+ const now = at(NY, 2026, 6, 1, 10, 0);
149
+ const got = resolveAfter(days(2), "08:00", { timezone: NY, now });
150
+ expect(got).toEqual(at(NY, 2026, 6, 3, 8, 0));
151
+ });
152
+
153
+ it("in 1 hour crossing midnight snaps to next day's HH:mm", () => {
154
+ const now = at(NY, 2026, 6, 1, 23, 30);
155
+ const got = resolveAfter(hours(1), "08:00", { timezone: NY, now });
156
+ // now + 1h => 2026-06-02 00:30 local => snapped to 08:00 that day.
157
+ expect(got).toEqual(at(NY, 2026, 6, 2, 8, 0));
158
+ });
159
+
160
+ it("snapped HH:mm earlier than now on the same day rolls forward (not past)", () => {
161
+ // now 09:00, +1h => 10:00 same day, but .at('08:00') pulls back to 08:00
162
+ // which is already in the past. Must roll to tomorrow's 08:00, never < now.
163
+ const now = at(NY, 2026, 6, 1, 9, 0);
164
+ const got = resolveAfter(hours(1), "08:00", { timezone: NY, now });
165
+ expect(got.getTime()).toBeGreaterThan(now.getTime());
166
+ expect(got).toEqual(at(NY, 2026, 6, 2, 8, 0));
167
+ });
168
+
169
+ it("snapped HH:mm earlier than now with ifPast 'now' clamps to current instant", () => {
170
+ const now = at(NY, 2026, 6, 1, 9, 0);
171
+ const got = resolveAfter(hours(1), "08:00", {
172
+ timezone: NY,
173
+ now,
174
+ ifPast: "now",
175
+ });
176
+ expect(got.getTime()).toBe(now.getTime());
177
+ });
178
+ });
179
+
180
+ describe("DST handling (America/New_York)", () => {
181
+ it("spring-forward gap (2026-03-08 02:30) picks post-gap EDT instant", () => {
182
+ // Clocks jump 02:00 -> 03:00; 02:30 does not exist.
183
+ const now = at(NY, 2026, 3, 8, 0, 0);
184
+ const got = resolveNextLocalTime("02:30", { timezone: NY, now });
185
+ // "compatible" => later instant; resulting offset is EDT (-04:00).
186
+ expect(offsetOf(got, NY)).toBe("-04:00");
187
+ expect(wall(got, NY)).toBe("03:30");
188
+ });
189
+
190
+ it("fall-back overlap (2026-11-01 01:30) picks earlier (EDT) instant", () => {
191
+ const now = at(NY, 2026, 11, 1, 0, 0);
192
+ const got = resolveNextLocalTime("01:30", { timezone: NY, now });
193
+ // The earlier of the two 01:30s is still EDT (-04:00).
194
+ expect(offsetOf(got, NY)).toBe("-04:00");
195
+ expect(wall(got, NY)).toBe("01:30");
196
+ });
197
+
198
+ it("resolveTomorrow across spring-forward uses calendar add (not +24h)", () => {
199
+ const now = at(NY, 2026, 3, 7, 23, 0); // EST
200
+ const got = resolveTomorrow("08:00", { timezone: NY, now });
201
+ expect(wall(got, NY)).toBe("08:00");
202
+ expect(offsetOf(got, NY)).toBe("-04:00"); // 2026-03-08 is EDT
203
+ });
204
+ });
205
+
206
+ describe("clampToWindow", () => {
207
+ const win = { start: "09:00", end: "17:00" };
208
+
209
+ it("inside window is unchanged", () => {
210
+ const inst = at(NY, 2026, 6, 1, 12, 0);
211
+ expect(clampToWindow(inst, win, NY)).toEqual(inst);
212
+ });
213
+
214
+ it("before open snaps to today's open", () => {
215
+ const inst = at(NY, 2026, 6, 1, 7, 0);
216
+ expect(clampToWindow(inst, win, NY)).toEqual(at(NY, 2026, 6, 1, 9, 0));
217
+ });
218
+
219
+ it("after close snaps to tomorrow's open", () => {
220
+ const inst = at(NY, 2026, 6, 1, 19, 0);
221
+ expect(clampToWindow(inst, win, NY)).toEqual(at(NY, 2026, 6, 2, 9, 0));
222
+ });
223
+
224
+ it("exactly at close (17:00) is treated as after-close", () => {
225
+ const inst = at(NY, 2026, 6, 1, 17, 0);
226
+ expect(clampToWindow(inst, win, NY)).toEqual(at(NY, 2026, 6, 2, 9, 0));
227
+ });
228
+
229
+ it("exactly at open (09:00) is unchanged (inclusive)", () => {
230
+ const inst = at(NY, 2026, 6, 1, 9, 0);
231
+ expect(clampToWindow(inst, win, NY)).toEqual(inst);
232
+ });
233
+
234
+ it("start === end is always open", () => {
235
+ const inst = at(NY, 2026, 6, 1, 3, 0);
236
+ const allDay = { start: "00:00", end: "00:00" };
237
+ expect(clampToWindow(inst, allDay, NY)).toEqual(inst);
238
+ });
239
+
240
+ describe("overnight window 22:00-06:00", () => {
241
+ const overnight = { start: "22:00", end: "06:00" };
242
+ it("23:00 is in the open tail", () => {
243
+ const inst = at(NY, 2026, 6, 1, 23, 0);
244
+ expect(clampToWindow(inst, overnight, NY)).toEqual(inst);
245
+ });
246
+ it("03:00 is in the early-morning open", () => {
247
+ const inst = at(NY, 2026, 6, 1, 3, 0);
248
+ expect(clampToWindow(inst, overnight, NY)).toEqual(inst);
249
+ });
250
+ it("12:00 quiet gap snaps forward to tonight's open", () => {
251
+ const inst = at(NY, 2026, 6, 1, 12, 0);
252
+ expect(clampToWindow(inst, overnight, NY)).toEqual(
253
+ at(NY, 2026, 6, 1, 22, 0),
254
+ );
255
+ });
256
+ });
257
+
258
+ it("clamp into next-open via resolver window option", () => {
259
+ // 19:00 local resolved with a 09:00-17:00 window => next day 09:00.
260
+ const now = at(NY, 2026, 6, 1, 10, 0);
261
+ const got = resolveNextLocalTime("19:00", {
262
+ timezone: NY,
263
+ now,
264
+ window: win,
265
+ });
266
+ expect(got).toEqual(at(NY, 2026, 6, 2, 9, 0));
267
+ });
268
+ });
269
+
270
+ describe("isValidTimeZone", () => {
271
+ it("accepts valid IANA zones", () => {
272
+ expect(isValidTimeZone("America/New_York")).toBe(true);
273
+ expect(isValidTimeZone("UTC")).toBe(true);
274
+ expect(isValidTimeZone("Europe/London")).toBe(true);
275
+ });
276
+ it("rejects invalid / empty without throwing", () => {
277
+ expect(isValidTimeZone("Not/AZone")).toBe(false);
278
+ expect(isValidTimeZone("")).toBe(false);
279
+ expect(isValidTimeZone("garbage")).toBe(false);
280
+ // @ts-expect-error testing non-string robustness
281
+ expect(isValidTimeZone(null)).toBe(false);
282
+ });
283
+ });
@@ -0,0 +1,53 @@
1
+ import type { Weekday } from "../types/journey-context.js";
2
+
3
+ export type { IfPast, Weekday } from "../types/journey-context.js";
4
+
5
+ /**
6
+ * Map of accepted weekday names (short + full, lowercase) to ISO weekday
7
+ * numbers (Monday = 1 … Sunday = 7), matching `Temporal.PlainDate#dayOfWeek`.
8
+ */
9
+ const WEEKDAY_TO_ISO: Record<Weekday, number> = {
10
+ mon: 1,
11
+ monday: 1,
12
+ tue: 2,
13
+ tuesday: 2,
14
+ wed: 3,
15
+ wednesday: 3,
16
+ thu: 4,
17
+ thursday: 4,
18
+ fri: 5,
19
+ friday: 5,
20
+ sat: 6,
21
+ saturday: 6,
22
+ sun: 7,
23
+ sunday: 7,
24
+ };
25
+
26
+ /**
27
+ * Resolve a {@link Weekday} name to its ISO weekday number (1..7). Throws on an
28
+ * unknown name — this is author error, not a runtime fall-through case.
29
+ */
30
+ export function weekdayToIso(weekday: Weekday): number {
31
+ const iso = WEEKDAY_TO_ISO[weekday.toLowerCase() as Weekday];
32
+ if (iso === undefined) {
33
+ throw new TypeError(`Unknown weekday: "${weekday}"`);
34
+ }
35
+ return iso;
36
+ }
37
+
38
+ /**
39
+ * Parse a `"HH:mm"` string into `{ hour, minute }`. Throws on malformed input
40
+ * (author error — the scheduling API takes a literal time-of-day string).
41
+ */
42
+ export function parseTimeOfDay(time: string): { hour: number; minute: number } {
43
+ const match = /^(\d{1,2}):(\d{2})$/.exec(time.trim());
44
+ if (!match) {
45
+ throw new TypeError(`Invalid time-of-day (expected "HH:mm"): "${time}"`);
46
+ }
47
+ const hour = Number(match[1]);
48
+ const minute = Number(match[2]);
49
+ if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
50
+ throw new TypeError(`Time-of-day out of range: "${time}"`);
51
+ }
52
+ return { hour, minute };
53
+ }
@@ -0,0 +1,31 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import type { TimezoneCode } from "iana-db-timezones";
3
+
4
+ /**
5
+ * A valid IANA timezone identifier (e.g. `"America/New_York"`, `"Europe/London"`)
6
+ * — the full canonical + alias set, as a string-literal union. Sourced from
7
+ * `iana-db-timezones` (type-only import, erased at build). Use this for
8
+ * author-supplied timezones (`ctx.when.tz(...)`, client `defaults.timezone`) to
9
+ * get autocomplete and compile-time typo-catching. Timezones that arrive as
10
+ * runtime *data* (PostHog props, contact rows) stay plain `string` and are
11
+ * validated via {@link isValidTimeZone}.
12
+ */
13
+ export type TimeZone = TimezoneCode;
14
+
15
+ /**
16
+ * True if `tz` is a usable IANA timezone identifier. Probes the zone via
17
+ * Temporal inside a try/catch — it never throws, even on garbage input, so it
18
+ * is safe to use as a validity gate in a precedence chain.
19
+ */
20
+ export function isValidTimeZone(tz: string): boolean {
21
+ if (typeof tz !== "string" || tz.length === 0) {
22
+ return false;
23
+ }
24
+ try {
25
+ // `Temporal.Now.zonedDateTimeISO(tz)` throws RangeError on an unknown zone.
26
+ Temporal.Now.zonedDateTimeISO(tz);
27
+ return true;
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
@@ -0,0 +1,91 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { parseTimeOfDay } from "./time.js";
3
+
4
+ /** A quiet-hours / send window as wall-clock `"HH:mm"` edges in some tz. */
5
+ export interface SendWindow {
6
+ start: string;
7
+ end: string;
8
+ }
9
+
10
+ /**
11
+ * Build the absolute instant for a `PlainDate` at `HH:mm` in `timezone`.
12
+ *
13
+ * All wall-clock → instant conversions use disambiguation `"compatible"` (the
14
+ * JS-`Date`-equivalent rule): on a spring-forward gap it picks the later (post-
15
+ * gap) instant; on a fall-back overlap it picks the earlier occurrence. This
16
+ * keeps window edges DST-safe.
17
+ */
18
+ function instantAt(
19
+ date: Temporal.PlainDate,
20
+ hour: number,
21
+ minute: number,
22
+ timezone: string,
23
+ ): Temporal.ZonedDateTime {
24
+ return date
25
+ .toPlainDateTime(Temporal.PlainTime.from({ hour, minute }))
26
+ .toZonedDateTime(timezone, { disambiguation: "compatible" });
27
+ }
28
+
29
+ /**
30
+ * Map an instant into the open send window in `timezone`. No-op if the instant
31
+ * is already inside the window.
32
+ *
33
+ * - Normal window (`start < end`, e.g. `09:00`–`17:00`): open means
34
+ * `open <= instant < close`. Before open → today's open; at/after close →
35
+ * tomorrow's open.
36
+ * - Overnight window (`start > end`, e.g. `22:00`–`06:00`): wraps midnight.
37
+ * Open means `instant >= open` OR `instant < close`. In the quiet daytime
38
+ * gap → tonight's open.
39
+ * - `start === end` → always open (no clamp).
40
+ *
41
+ * "Next day" uses Temporal calendar arithmetic (`add({ days: 1 })`), so a 23/25-
42
+ * hour DST day still lands on the correct `HH:mm` wall-clock.
43
+ */
44
+ export function clampToWindow(
45
+ instant: Date,
46
+ window: SendWindow,
47
+ timezone: string,
48
+ ): Date {
49
+ const { hour: openH, minute: openM } = parseTimeOfDay(window.start);
50
+ const { hour: closeH, minute: closeM } = parseTimeOfDay(window.end);
51
+
52
+ // start === end → always open.
53
+ if (openH === closeH && openM === closeM) {
54
+ return instant;
55
+ }
56
+
57
+ const zdt = Temporal.Instant.fromEpochMilliseconds(
58
+ instant.getTime(),
59
+ ).toZonedDateTimeISO(timezone);
60
+ const date = zdt.toPlainDate();
61
+
62
+ const todayOpen = instantAt(date, openH, openM, timezone);
63
+ const todayClose = instantAt(date, closeH, closeM, timezone);
64
+ const normal = openH < closeH || (openH === closeH && openM < closeM);
65
+
66
+ if (normal) {
67
+ if (Temporal.ZonedDateTime.compare(zdt, todayOpen) < 0) {
68
+ return new Date(todayOpen.epochMilliseconds);
69
+ }
70
+ if (Temporal.ZonedDateTime.compare(zdt, todayClose) >= 0) {
71
+ const tomorrowOpen = instantAt(
72
+ date.add({ days: 1 }),
73
+ openH,
74
+ openM,
75
+ timezone,
76
+ );
77
+ return new Date(tomorrowOpen.epochMilliseconds);
78
+ }
79
+ return instant;
80
+ }
81
+
82
+ // Overnight window (start > end): close is in the morning, open in the evening.
83
+ if (Temporal.ZonedDateTime.compare(zdt, todayClose) < 0) {
84
+ return instant; // early-morning open tail
85
+ }
86
+ if (Temporal.ZonedDateTime.compare(zdt, todayOpen) >= 0) {
87
+ return instant; // late-evening open head
88
+ }
89
+ // Quiet daytime gap [close, open) → snap forward to tonight's open.
90
+ return new Date(todayOpen.epochMilliseconds);
91
+ }
@@ -1,4 +1,5 @@
1
1
  import type { DurationObject } from "../duration.js";
2
+ import type { TimeZone } from "../schedule/tz.js";
2
3
 
3
4
  export interface SleepOptions {
4
5
  duration: DurationObject;
@@ -10,6 +11,51 @@ export interface SleepResult {
10
11
  resumedAt: string;
11
12
  }
12
13
 
14
+ export interface SleepUntilOptions {
15
+ label?: string;
16
+ }
17
+
18
+ export type Weekday =
19
+ | "mon"
20
+ | "tue"
21
+ | "wed"
22
+ | "thu"
23
+ | "fri"
24
+ | "sat"
25
+ | "sun"
26
+ | "monday"
27
+ | "tuesday"
28
+ | "wednesday"
29
+ | "thursday"
30
+ | "friday"
31
+ | "saturday"
32
+ | "sunday";
33
+
34
+ /** How to treat a resolved instant that is already in the past. */
35
+ export type IfPast = "next" | "now";
36
+
37
+ export interface TimeOfDayBuilder {
38
+ /** Resolve to an absolute instant at `time` ("HH:mm") in the bound tz. */
39
+ at(time: string): Date;
40
+ }
41
+
42
+ export interface WhenBuilder {
43
+ /** Upcoming named weekday; chain `.at("HH:mm")`. */
44
+ next(weekday: Weekday): TimeOfDayBuilder;
45
+ /** Next occurrence of `time` local (today if future, else tomorrow). */
46
+ nextLocal(time: string): Date;
47
+ /** Tomorrow in the bound tz; chain `.at("HH:mm")`. */
48
+ tomorrow(): TimeOfDayBuilder;
49
+ /** `duration` from now, snapped to `.at("HH:mm")` on that day. */
50
+ in(duration: DurationObject): TimeOfDayBuilder;
51
+ /** Override the resolved user tz for this chain only. Returns a new builder. */
52
+ tz(timezone: TimeZone): WhenBuilder;
53
+ /** Override the default send window for this chain. Returns a new builder. */
54
+ window(start: string, end: string): WhenBuilder;
55
+ /** How to treat an already-past resolved time. Default "next". */
56
+ ifPast(strategy: IfPast): WhenBuilder;
57
+ }
58
+
13
59
  export interface TriggerOptions {
14
60
  event: string;
15
61
  userId: string;
@@ -52,6 +98,13 @@ export interface EmailHistoryResult {
52
98
 
53
99
  export interface JourneyContext {
54
100
  sleep(opts: SleepOptions): Promise<SleepResult>;
101
+
102
+ /** Durable sleep until an absolute instant (`Date` or ISO string). */
103
+ sleepUntil(at: Date | string, opts?: SleepUntilOptions): Promise<SleepResult>;
104
+
105
+ /** Timezone-bound fluent scheduler. Always terminates in a `Date`. */
106
+ when: WhenBuilder;
107
+
55
108
  checkpoint(label: string): Promise<void>;
56
109
  trigger(opts: TriggerOptions): Promise<void>;
57
110
  identify(properties: Record<string, unknown>): void;