@mark1russell7/algebra 0.8.8 → 0.8.9
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/build/{clock-DOwB2Lj_.d.ts → clock-BNBDlRRA.d.ts} +56 -2
- package/build/clock.d.ts +2 -2
- package/build/clock.js +72 -1
- package/build/clock.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/{run-BAJjDj98.d.ts → run-C5U_EFvv.d.ts} +1 -1
- package/build/run.d.ts +1 -1
- package/package.json +1 -1
|
@@ -11,11 +11,30 @@ type Cancel = (() => void) & {
|
|
|
11
11
|
* the runtime is then exact and repeatable.
|
|
12
12
|
*/
|
|
13
13
|
interface Clock {
|
|
14
|
+
/**
|
|
15
|
+
* This mark tells the time of `now()`. A clock gives the monotonic time, thus the mark is
|
|
16
|
+
* `"monotonic"` or absent. A `WallClock` has the mark `"wall"`, thus it is not a `Clock`.
|
|
17
|
+
*/
|
|
18
|
+
readonly time?: "monotonic" | undefined;
|
|
14
19
|
/** This method gives the monotonic time, in milliseconds. A step of the wall clock does not move it. */
|
|
15
20
|
now(): number;
|
|
16
21
|
/** This method starts `run` after `ms` milliseconds, and gives the function that stops it. */
|
|
17
22
|
after(ms: number, run: () => void): Cancel;
|
|
18
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* A wall clock gives the instant: the time in milliseconds from 1970-01-01T00:00:00Z, as `Date.now()`
|
|
26
|
+
* gives it. Civil time, a calendar and a time that another computer sends need the instant. A step
|
|
27
|
+
* of the wall clock moves `now()`, also back. Each timer waits on the monotonic time, thus a step
|
|
28
|
+
* does not move a timer. The mark `"wall"` keeps the two clocks apart: a function that needs the
|
|
29
|
+
* monotonic time does not get a wall clock, and the reverse.
|
|
30
|
+
*/
|
|
31
|
+
interface WallClock {
|
|
32
|
+
readonly time: "wall";
|
|
33
|
+
/** This method gives the instant, in milliseconds from the epoch. A step of the wall clock moves it. */
|
|
34
|
+
now(): number;
|
|
35
|
+
/** This method starts `run` after `ms` milliseconds of monotonic time, and gives the function that stops it. */
|
|
36
|
+
after(ms: number, run: () => void): Cancel;
|
|
37
|
+
}
|
|
19
38
|
/** These options make a clock of the platform. */
|
|
20
39
|
interface PlatformClockOptions {
|
|
21
40
|
/** True makes each timer not keep a Node process alive. The default is false, as with `setTimeout`. */
|
|
@@ -31,6 +50,11 @@ declare const platformClock: (options?: PlatformClockOptions) => Clock;
|
|
|
31
50
|
* process alive, thus a timer that is not stopped cannot stop the exit of the process.
|
|
32
51
|
*/
|
|
33
52
|
declare const realClock: Clock;
|
|
53
|
+
/**
|
|
54
|
+
* This function makes the wall clock of the platform: `Date.now()`, and the timers of
|
|
55
|
+
* `platformClock`. The options are the options of `platformClock`.
|
|
56
|
+
*/
|
|
57
|
+
declare const wallClock: (options?: PlatformClockOptions) => WallClock;
|
|
34
58
|
/**
|
|
35
59
|
* This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal
|
|
36
60
|
* aborts, the promise stops its timer and rejects with the reason of the signal. A signal that
|
|
@@ -53,9 +77,25 @@ interface FakeClock extends Clock {
|
|
|
53
77
|
}
|
|
54
78
|
/** This function makes a fake clock. Its time starts at `start` milliseconds. */
|
|
55
79
|
declare const fakeClock: (start?: number) => FakeClock;
|
|
80
|
+
/** A fake wall clock moves only when a test tells it to. A test can also step its instant, as a wall clock steps. */
|
|
81
|
+
interface FakeWallClock extends WallClock {
|
|
82
|
+
/** This method moves the instant and the monotonic time forward by `ms`, and it starts each timer that comes due, in sequence. */
|
|
83
|
+
advance(ms: number): Promise<void>;
|
|
84
|
+
/** This method moves the instant and the monotonic time forward by `ms`, and starts no timer, as a blocked event loop does. */
|
|
85
|
+
block(ms: number): void;
|
|
86
|
+
/** This method steps the instant to `instant`, forward or back. The monotonic time does not move, thus no timer comes due. */
|
|
87
|
+
set(instant: number): void;
|
|
88
|
+
/** This count is the number of timers that are not stopped and not called. */
|
|
89
|
+
readonly pending: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* This function makes a fake wall clock. Its instant starts at `start`, in milliseconds from the
|
|
93
|
+
* epoch. Its timers are the timers of a `fakeClock`, thus a step of the instant moves no timer.
|
|
94
|
+
*/
|
|
95
|
+
declare const fakeWallClock: (start: number) => FakeWallClock;
|
|
56
96
|
/**
|
|
57
97
|
* The wall-clock time of an instant in a time zone. An instant is a time in milliseconds from
|
|
58
|
-
* 1970-01-01T00:00:00Z, as `Date.now()`
|
|
98
|
+
* 1970-01-01T00:00:00Z, as `Date.now()` and `WallClock.now()` give. It is not the monotonic time of `Clock.now()`.
|
|
59
99
|
*/
|
|
60
100
|
interface LocalTime {
|
|
61
101
|
readonly year: number;
|
|
@@ -133,6 +173,20 @@ declare function instantsOf(wall: number, zone: string): number[] | undefined;
|
|
|
133
173
|
* times in 6 hours. A name that is not a time zone, or a bound that is not a date, gives `undefined`.
|
|
134
174
|
*/
|
|
135
175
|
declare function offsetChanges(from: number, to: number, zone: string): number[] | undefined;
|
|
176
|
+
/**
|
|
177
|
+
* This function gives the first instant at which the wall clock of `zone` shows `wall` or a later
|
|
178
|
+
* wall-clock time. The wall-clock time is in milliseconds with the arithmetic of UTC, as for
|
|
179
|
+
* `instantsOf`. Thus a time on the wall, for example the start of a day, always has an instant.
|
|
180
|
+
*
|
|
181
|
+
* - A wall-clock time that occurs one time gives its instant.
|
|
182
|
+
* - A wall-clock time in the hour of a change back gives the first of its two instants.
|
|
183
|
+
* - A wall-clock time in the gap of a change forward gives the instant of the change: the end of the gap.
|
|
184
|
+
* - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.
|
|
185
|
+
*
|
|
186
|
+
* The search reads the offset one day before and one day after `wall`, as `instantsOf` does. A gap
|
|
187
|
+
* that is not in these 2 days also gives `undefined`.
|
|
188
|
+
*/
|
|
189
|
+
declare function firstInstantOf(wall: number, zone: string): number | undefined;
|
|
136
190
|
/**
|
|
137
191
|
* This function gives the next change of the window after `instant`: the first later instant at
|
|
138
192
|
* which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`
|
|
@@ -142,4 +196,4 @@ declare function offsetChanges(from: number, to: number, zone: string): number[]
|
|
|
142
196
|
declare function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined;
|
|
143
197
|
/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */
|
|
144
198
|
declare function windowProblems(window: TimeWindow): string[];
|
|
145
|
-
export { windowProblems as _,
|
|
199
|
+
export { windowProblems as S, nextBoundary as _, LocalTime as a, realClock as b, WallClock as c, fakeClock as d, fakeWallClock as f, localTime as g, instantsOf as h, FakeWallClock as i, WindowBoundary as l, inWindow as m, Clock as n, PlatformClockOptions as o, firstInstantOf as p, FakeClock as r, TimeWindow as s, Cancel as t, delay as u, offsetChanges as v, wallClock as x, platformClock as y };
|
package/build/clock.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { Cancel, Clock, FakeClock, LocalTime, PlatformClockOptions, TimeWindow, WindowBoundary, delay, fakeClock, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, windowProblems };
|
|
1
|
+
import { S as windowProblems, _ as nextBoundary, a as LocalTime, b as realClock, c as WallClock, d as fakeClock, f as fakeWallClock, g as localTime, h as instantsOf, i as FakeWallClock, l as WindowBoundary, m as inWindow, n as Clock, o as PlatformClockOptions, p as firstInstantOf, r as FakeClock, s as TimeWindow, t as Cancel, u as delay, v as offsetChanges, x as wallClock, y as platformClock } from "./clock-BNBDlRRA.js";
|
|
2
|
+
export { Cancel, Clock, FakeClock, FakeWallClock, LocalTime, PlatformClockOptions, TimeWindow, WallClock, WindowBoundary, delay, fakeClock, fakeWallClock, firstInstantOf, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, wallClock, windowProblems };
|
package/build/clock.js
CHANGED
|
@@ -33,6 +33,15 @@ const platformClock = (options = {}) => ({
|
|
|
33
33
|
*/
|
|
34
34
|
const realClock = /* @__PURE__ */ platformClock({ unref: true });
|
|
35
35
|
/**
|
|
36
|
+
* This function makes the wall clock of the platform: `Date.now()`, and the timers of
|
|
37
|
+
* `platformClock`. The options are the options of `platformClock`.
|
|
38
|
+
*/
|
|
39
|
+
const wallClock = (options = {}) => ({
|
|
40
|
+
time: "wall",
|
|
41
|
+
now: () => Date.now(),
|
|
42
|
+
after: platformClock(options).after
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
36
45
|
* This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal
|
|
37
46
|
* aborts, the promise stops its timer and rejects with the reason of the signal. A signal that
|
|
38
47
|
* aborted before the call rejects at once, and starts no timer. Each end removes the listener from
|
|
@@ -105,6 +114,29 @@ const fakeClock = (start = 0) => {
|
|
|
105
114
|
}
|
|
106
115
|
};
|
|
107
116
|
};
|
|
117
|
+
/**
|
|
118
|
+
* This function makes a fake wall clock. Its instant starts at `start`, in milliseconds from the
|
|
119
|
+
* epoch. Its timers are the timers of a `fakeClock`, thus a step of the instant moves no timer.
|
|
120
|
+
*/
|
|
121
|
+
const fakeWallClock = (start) => {
|
|
122
|
+
const monotonicTime = fakeClock(0);
|
|
123
|
+
let offset = start;
|
|
124
|
+
return {
|
|
125
|
+
time: "wall",
|
|
126
|
+
now: () => monotonicTime.now() + offset,
|
|
127
|
+
after: (ms, run) => monotonicTime.after(ms, run),
|
|
128
|
+
advance: (ms) => monotonicTime.advance(ms),
|
|
129
|
+
block(ms) {
|
|
130
|
+
monotonicTime.block(ms);
|
|
131
|
+
},
|
|
132
|
+
set(instant) {
|
|
133
|
+
offset = instant - monotonicTime.now();
|
|
134
|
+
},
|
|
135
|
+
get pending() {
|
|
136
|
+
return monotonicTime.pending;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
};
|
|
108
140
|
const MINUTE_MS = 6e4;
|
|
109
141
|
const DAY_MS = 864e5;
|
|
110
142
|
const WEEKDAYS = {
|
|
@@ -293,6 +325,45 @@ const changesOf = (from, to, zone) => {
|
|
|
293
325
|
return found;
|
|
294
326
|
};
|
|
295
327
|
/**
|
|
328
|
+
* This function gives the first instant at which the wall clock of `zone` shows `wall` or a later
|
|
329
|
+
* wall-clock time. The wall-clock time is in milliseconds with the arithmetic of UTC, as for
|
|
330
|
+
* `instantsOf`. Thus a time on the wall, for example the start of a day, always has an instant.
|
|
331
|
+
*
|
|
332
|
+
* - A wall-clock time that occurs one time gives its instant.
|
|
333
|
+
* - A wall-clock time in the hour of a change back gives the first of its two instants.
|
|
334
|
+
* - A wall-clock time in the gap of a change forward gives the instant of the change: the end of the gap.
|
|
335
|
+
* - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.
|
|
336
|
+
*
|
|
337
|
+
* The search reads the offset one day before and one day after `wall`, as `instantsOf` does. A gap
|
|
338
|
+
* that is not in these 2 days also gives `undefined`.
|
|
339
|
+
*/
|
|
340
|
+
function firstInstantOf(wall, zone) {
|
|
341
|
+
const instants = instantsOf(wall, zone);
|
|
342
|
+
if (instants === void 0) return void 0;
|
|
343
|
+
if (instants.length > 0) return instants[0];
|
|
344
|
+
const whole = Math.floor(wall / 1e3) * 1e3;
|
|
345
|
+
const offsets = [];
|
|
346
|
+
for (const probe of [
|
|
347
|
+
whole - DAY_MS,
|
|
348
|
+
whole,
|
|
349
|
+
whole + DAY_MS
|
|
350
|
+
]) {
|
|
351
|
+
const local = localTime(probe, zone);
|
|
352
|
+
if (local !== void 0) offsets.push(Math.round(local.offset * MINUTE_MS));
|
|
353
|
+
}
|
|
354
|
+
const from = wall - Math.max(...offsets);
|
|
355
|
+
const to = wall - Math.min(...offsets);
|
|
356
|
+
const offsetAt = (instant) => {
|
|
357
|
+
const local = localTime(instant, zone);
|
|
358
|
+
return local === void 0 ? void 0 : Math.round(local.offset * MINUTE_MS);
|
|
359
|
+
};
|
|
360
|
+
for (const change of changesOf(from, to, zone)) {
|
|
361
|
+
const before = offsetAt(change - 1);
|
|
362
|
+
const after = offsetAt(change);
|
|
363
|
+
if (before !== void 0 && after !== void 0 && change + before <= wall && wall < change + after) return change;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
296
367
|
* This function gives the next change of the window after `instant`: the first later instant at
|
|
297
368
|
* which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`
|
|
298
369
|
* for a bad window, and when the window does not change in the 9 days after `instant`. A window
|
|
@@ -327,6 +398,6 @@ function windowProblems(window) {
|
|
|
327
398
|
if (window.days !== void 0 && window.days.length === 0) problems.push("The window has no day, thus it does not open.");
|
|
328
399
|
return problems;
|
|
329
400
|
}
|
|
330
|
-
export { delay, fakeClock, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, windowProblems };
|
|
401
|
+
export { delay, fakeClock, fakeWallClock, firstInstantOf, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, wallClock, windowProblems };
|
|
331
402
|
|
|
332
403
|
//# sourceMappingURL=clock.js.map
|
package/build/clock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clock.js","names":[],"sources":["../src/clock.ts"],"sourcesContent":["// @mark1russell7/algebra/clock: the clock of a runtime, a fake clock for tests, and civil time. It imports\n// nothing: no `node:` module, no zod and no other package. Thus a client in a browser can use it.\n// The service gives this clock again as @mark1russell7/service/clock.\n\n/**\n * This function stops a timer that `Clock.after` started. A second use does nothing. A clock of\n * the platform also gives `unref`: after it, the timer does not keep a Node process alive.\n */\nexport type Cancel = (() => void) & { readonly unref?: (() => void) | undefined };\n\n/**\n * A clock gives the monotonic time in milliseconds, and it starts a function after a delay. The\n * runtime reads time only through a clock. Thus a test can give a fake clock, and the timing of\n * the runtime is then exact and repeatable.\n */\nexport interface Clock {\n /** This method gives the monotonic time, in milliseconds. A step of the wall clock does not move it. */\n now(): number;\n /** This method starts `run` after `ms` milliseconds, and gives the function that stops it. */\n after(ms: number, run: () => void): Cancel;\n}\n\n/** These options make a clock of the platform. */\nexport interface PlatformClockOptions {\n /** True makes each timer not keep a Node process alive. The default is false, as with `setTimeout`. */\n readonly unref?: boolean | undefined;\n}\n\n/** This function gives the monotonic time: `performance.now()`, or `Date.now()` on a platform without `performance`. */\nconst monotonic = (): number => {\n const performance = (globalThis as { performance?: { now(): number } }).performance;\n return performance === undefined ? Date.now() : performance.now();\n};\n\n/** This delay is the longest that `setTimeout` takes, in milliseconds. A longer delay starts the function after 1 ms. */\nconst LONGEST_DELAY = 0x7fffffff;\n\n/**\n * This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel\n * that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.\n */\nexport const platformClock = (options: PlatformClockOptions = {}): Clock => ({\n now: monotonic,\n after(ms, run) {\n let unrefed = options.unref === true;\n const arm = (left: number): ReturnType<typeof setTimeout> => {\n const step =\n left > LONGEST_DELAY ? setTimeout(() => (timer = arm(left - LONGEST_DELAY)), LONGEST_DELAY) : setTimeout(run, Math.max(0, left));\n if (unrefed) (step as { unref?: () => void }).unref?.();\n return step;\n };\n let timer = arm(ms);\n const unref = (): void => {\n unrefed = true;\n (timer as { unref?: () => void }).unref?.();\n };\n return Object.assign(() => clearTimeout(timer), { unref });\n },\n});\n\n/**\n * This clock reads `performance.now()` and uses `setTimeout`. Its timers do not keep a Node\n * process alive, thus a timer that is not stopped cannot stop the exit of the process.\n */\nexport const realClock: Clock = /* @__PURE__ */ platformClock({ unref: true });\n\n/**\n * This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal\n * aborts, the promise stops its timer and rejects with the reason of the signal. A signal that\n * aborted before the call rejects at once, and starts no timer. Each end removes the listener from\n * the signal. A timer of `realClock` does not keep a Node process alive: give `platformClock()` to\n * keep the process alive until the delay ends.\n */\nexport const delay = (clock: Clock, ms: number, signal?: AbortSignal): Promise<void> =>\n new Promise<void>((resolve, reject) => {\n if (signal?.aborted === true) {\n reject(signal.reason);\n return;\n }\n const abort = (): void => {\n cancel();\n reject(signal?.reason);\n };\n const cancel = clock.after(ms, () => {\n signal?.removeEventListener(\"abort\", abort);\n resolve();\n });\n signal?.addEventListener(\"abort\", abort, { once: true });\n });\n\n/** A fake clock moves only when a test tells it to. */\nexport interface FakeClock extends Clock {\n /** This method moves the clock forward by `ms`, and it starts each timer that comes due, in sequence. */\n advance(ms: number): Promise<void>;\n /**\n * This method moves the clock forward by `ms` and starts no timer, as a blocked event loop does.\n * The next `advance` then starts each timer that came due during the block, late.\n */\n block(ms: number): void;\n /** This count is the number of timers that are not stopped and not called. */\n readonly pending: number;\n}\n\n/** This function lets the promises that are ready continue, before the clock moves again. */\nconst settle = async (): Promise<void> => {\n for (let turn = 0; turn < 64; turn += 1) await Promise.resolve();\n};\n\n/** This function makes a fake clock. Its time starts at `start` milliseconds. */\nexport const fakeClock = (start = 0): FakeClock => {\n let now = start;\n let sequence = 0;\n const timers: Array<{ at: number; order: number; run: () => void; live: boolean }> = [];\n const due = (end: number) => {\n let best: (typeof timers)[number] | undefined;\n for (const timer of timers)\n if (timer.live && timer.at <= end && (best === undefined || timer.at < best.at || (timer.at === best.at && timer.order < best.order)))\n best = timer;\n return best;\n };\n return {\n now: () => now,\n after(ms, run): Cancel {\n const timer = { at: now + Math.max(0, ms), order: sequence++, run, live: true };\n timers.push(timer);\n return () => {\n timer.live = false;\n };\n },\n async advance(ms) {\n // a monotonic clock does not go back\n const end = now + Math.max(0, ms);\n await settle();\n for (;;) {\n const timer = due(end);\n if (timer === undefined) break;\n timer.live = false;\n // a timer that came due during a block runs late, at the time of the clock\n now = Math.max(now, timer.at);\n timer.run();\n await settle();\n }\n now = Math.max(now, end);\n for (let at = timers.length - 1; at >= 0; at -= 1) if (!timers[at]!.live) timers.splice(at, 1);\n await settle();\n },\n block(ms) {\n now += Math.max(0, ms);\n },\n get pending() {\n return timers.filter((timer) => timer.live).length;\n },\n };\n};\n\n// Civil time: the wall-clock time of an instant in a time zone, and windows of local time. A clock\n// gives instants, and civil time maps an instant to the time on the wall of a zone. It uses only\n// `Intl.DateTimeFormat`, thus this entry still imports nothing.\n\n/**\n * The wall-clock time of an instant in a time zone. An instant is a time in milliseconds from\n * 1970-01-01T00:00:00Z, as `Date.now()` gives. It is not the monotonic time of `Clock.now()`.\n */\nexport interface LocalTime {\n readonly year: number;\n /** The month, from 1 (January) to 12. */\n readonly month: number;\n /** The day of the month, from 1. */\n readonly day: number;\n /** The hour, from 0 to 23. */\n readonly hour: number;\n readonly minute: number;\n readonly second: number;\n /** The day of the week, from 0 (Sunday) to 6 (Saturday). */\n readonly weekday: number;\n /** The offset of the zone from UTC at the instant, in minutes. It is positive east of Greenwich. */\n readonly offset: number;\n}\n\n/**\n * A window of local time that occurs on each day, or on some days of the week. The window is\n * data, thus a configuration can hold it.\n *\n * - The start is in the window, and the end is not.\n * - An end before the start crosses midnight. An end equal to the start gives a window of a full day.\n * - In a change of the clock, the window follows the time on the wall. A start or an end in a gap\n * moves to the end of the gap. Thus a window with its start and its end in one gap does not open.\n * - A local time that occurs two times is in the window two times.\n */\nexport interface TimeWindow {\n /** The IANA name of the time zone, for example `Europe/Berlin`. */\n readonly zone: string;\n /** The start, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly start: string;\n /** The end, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly end: string;\n /**\n * The days of the week on which the window starts, from 0 (Sunday) to 6. When it is absent, the\n * window starts on each day. A window across midnight continues into the next day.\n */\n readonly days?: readonly number[] | undefined;\n}\n\n/** A change of a window: the instant at which it opens or closes. */\nexport interface WindowBoundary {\n readonly at: number;\n /** True: the window opens at `at`. False: it closes. */\n readonly opens: boolean;\n}\n\nconst MINUTE_MS = 60_000;\nconst DAY_MS = 86_400_000;\nconst WEEKDAYS: Readonly<Record<string, number>> = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };\nconst LOCAL_TIME = /^([01]\\d|2[0-3]):([0-5]\\d)$/;\n\n/**\n * The cache has one format for each good zone name. It keeps no bad name, and it starts again at\n * `ZONE_CACHE_MAX` names. Thus names from configuration input cannot make it grow without a limit.\n */\nconst ZONE_FORMATS = new Map<string, Intl.DateTimeFormat>();\nconst ZONE_CACHE_MAX = 512;\n/** The longest IANA name has 32 characters. A longer name than this limit is not a zone. */\nconst ZONE_NAME_MAX = 100;\n\nconst zoneFormat = (zone: string): Intl.DateTimeFormat | undefined => {\n let format = ZONE_FORMATS.get(zone);\n if (format === undefined) {\n if (typeof zone !== \"string\" || zone.length > ZONE_NAME_MAX) return undefined;\n try {\n format = new Intl.DateTimeFormat(\"en-US\", {\n timeZone: zone,\n hourCycle: \"h23\",\n era: \"short\",\n year: \"numeric\",\n month: \"numeric\",\n day: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n second: \"numeric\",\n weekday: \"short\",\n });\n } catch {\n return undefined;\n }\n if (ZONE_FORMATS.size >= ZONE_CACHE_MAX) ZONE_FORMATS.clear();\n ZONE_FORMATS.set(zone, format);\n }\n return format;\n};\n\n/**\n * This function gives the wall-clock time of `instant` in `zone`. It gives `undefined` for a name\n * that is not a time zone, and for an instant that is not a date.\n */\nexport function localTime(instant: number, zone: string): LocalTime | undefined {\n const format = zoneFormat(zone);\n if (format === undefined || !Number.isFinite(instant)) return undefined;\n let parts: Intl.DateTimeFormatPart[];\n try {\n parts = format.formatToParts(instant);\n } catch {\n // an instant out of the range of a date\n return undefined;\n }\n const field: Record<string, string> = {};\n for (const part of parts) field[part.type] = part.value;\n const before = field[\"era\"] === \"BC\" || field[\"era\"] === \"B\";\n const year = before ? 1 - Number(field[\"year\"]) : Number(field[\"year\"]);\n const month = Number(field[\"month\"]);\n const day = Number(field[\"day\"]);\n const hour = Number(field[\"hour\"]) % 24;\n const minute = Number(field[\"minute\"]);\n const second = Number(field[\"second\"]);\n const whole = Math.floor(instant / 1000) * 1000;\n return { year, month, day, hour, minute, second, weekday: WEEKDAYS[field[\"weekday\"] ?? \"\"] ?? 0, offset: (wallOf(year, month, day, hour, minute, second) - whole) / MINUTE_MS };\n}\n\n/** The wall-clock time as an instant in UTC. `Date.UTC` reads a year from 0 to 99 as 1900 to 1999, thus this function sets the year by itself. */\nconst wallOf = (year: number, month: number, day: number, hour: number, minute: number, second: number): number => {\n const wall = new Date(0);\n wall.setUTCFullYear(year, month - 1, day);\n wall.setUTCHours(hour, minute, second, 0);\n return wall.getTime();\n};\n\n/** The minutes from midnight of a local time `HH:MM`. */\nconst minutesOf = (time: string): number | undefined => {\n const match = LOCAL_TIME.exec(time);\n return match === null ? undefined : Number(match[1]) * 60 + Number(match[2]);\n};\n\ninterface CheckedWindow {\n readonly start: number;\n readonly end: number;\n readonly days: ReadonlySet<number> | undefined;\n}\n\nconst isDay = (day: number): boolean => Number.isInteger(day) && day >= 0 && day <= 6;\n\nconst checked = (window: TimeWindow): CheckedWindow | undefined => {\n const start = minutesOf(window.start);\n const end = minutesOf(window.end);\n if (start === undefined || end === undefined || zoneFormat(window.zone) === undefined) return undefined;\n if (window.days !== undefined && !window.days.every(isDay)) return undefined;\n return { start, end, days: window.days === undefined ? undefined : new Set(window.days) };\n};\n\nconst within = (window: CheckedWindow, local: LocalTime): boolean => {\n const minute = local.hour * 60 + local.minute;\n const startsOn = (weekday: number): boolean => window.days === undefined || window.days.has(weekday);\n if (window.start < window.end) return window.start <= minute && minute < window.end && startsOn(local.weekday);\n // across midnight, or a full day: the part from the start, then the part before the end on the next day\n return (minute >= window.start && startsOn(local.weekday)) || (minute < window.end && startsOn((local.weekday + 6) % 7));\n};\n\n/**\n * This function tells if `instant` is in the window. It does not throw. A bad window or a bad\n * instant gives `false`, and `windowProblems` finds the bad window.\n */\nexport function inWindow(instant: number, window: TimeWindow): boolean {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n return valid !== undefined && local !== undefined && within(valid, local);\n}\n\n/**\n * This function gives the instants at which the wall clock of `zone` shows `wall`, in order. It is\n * the inverse of `localTime`. The wall-clock time is in milliseconds with the arithmetic of UTC: the\n * UTC fields of `new Date(wall)` are the fields of the wall clock.\n *\n * - A wall-clock time in the gap of a change forward gives no instant.\n * - A wall-clock time in the hour of a change back gives two instants.\n * - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.\n *\n * The rule for a gap or for two instants is a choice of the caller. For example, RFC 5545 takes the\n * first of two instants, and in a gap it takes the offset before the gap. The search reads the\n * offset one day before and one day after `wall`. Thus it finds each instant when the offset\n * changes at most one time in these 2 days.\n */\nexport function instantsOf(wall: number, zone: string): number[] | undefined {\n if (zoneFormat(zone) === undefined || !Number.isFinite(wall)) return undefined;\n // localTime gives whole seconds: the search uses the whole second, and adds the milliseconds at the end\n const whole = Math.floor(wall / 1000) * 1000;\n const offsets = new Set<number>();\n for (const probe of [whole - DAY_MS, whole, whole + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.add(local.offset);\n }\n // no offset: the wall-clock time is out of the range of a date\n if (offsets.size === 0) return undefined;\n const found: number[] = [];\n for (const offset of offsets) {\n // an old offset of local mean time has seconds, thus the product is rounded to the millisecond\n const instant = whole - Math.round(offset * MINUTE_MS);\n const local = localTime(instant, zone);\n if (local === undefined) continue;\n const back = wallOf(local.year, local.month, local.day, local.hour, local.minute, local.second);\n if (back === whole && !found.includes(instant)) found.push(instant);\n }\n return found.sort((a, b) => a - b).map((instant) => instant + (wall - whole));\n}\n\n/**\n * This function gives the instants in `(from, to]` at which the offset of `zone` changes, in order\n * and exact to the millisecond. At each instant, the new offset starts. The search reads the offset\n * each 6 hours of the range, thus a long range costs more reads. No zone changes its offset two\n * times in 6 hours. A name that is not a time zone, or a bound that is not a date, gives `undefined`.\n */\nexport function offsetChanges(from: number, to: number, zone: string): number[] | undefined {\n if (localTime(from, zone) === undefined || localTime(to, zone) === undefined) return undefined;\n return changesOf(from, to, zone);\n}\n\n/** The instants in (from, to] at which the offset of the zone changes. `offsetChanges` tells the rule. */\nconst changesOf = (from: number, to: number, zone: string): number[] => {\n const offsetAt = (instant: number): number | undefined => localTime(instant, zone)?.offset;\n const found: number[] = [];\n let low = from;\n let before = offsetAt(low);\n while (low < to) {\n const high = Math.min(low + DAY_MS / 4, to);\n const after = offsetAt(high);\n if (before !== undefined && after !== undefined && after !== before) {\n // the first instant with the new offset\n let left = low;\n let right = high;\n while (right - left > 1) {\n const middle = Math.floor((left + right) / 2);\n if (offsetAt(middle) === before) left = middle;\n else right = middle;\n }\n found.push(right);\n }\n low = high;\n before = after;\n }\n return found;\n};\n\n/**\n * This function gives the next change of the window after `instant`: the first later instant at\n * which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`\n * for a bad window, and when the window does not change in the 9 days after `instant`. A window\n * with no day and a full-day window on each day do not change.\n */\nexport function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n if (valid === undefined || local === undefined) return undefined;\n const inside = within(valid, local);\n const horizon = instant + 9 * DAY_MS;\n // The state changes only where the wall clock shows the start or the end, or where the offset changes.\n const candidates = changesOf(instant, horizon, window.zone);\n const midnight = wallOf(local.year, local.month, local.day, 0, 0, 0);\n for (let day = -1; day <= 10; day += 1) {\n for (const minutes of [valid.start, valid.end]) candidates.push(...(instantsOf(midnight + day * DAY_MS + minutes * MINUTE_MS, window.zone) ?? []));\n }\n candidates.sort((a, b) => a - b);\n for (const at of candidates) {\n if (at <= instant || at > horizon) continue;\n const there = localTime(at, window.zone);\n if (there !== undefined && within(valid, there) !== inside) return { at, opens: !inside };\n }\n return undefined;\n}\n\n/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */\nexport function windowProblems(window: TimeWindow): string[] {\n const problems: string[] = [];\n if (zoneFormat(window.zone) === undefined) problems.push(`\"${window.zone}\" is not the IANA name of a time zone.`);\n if (minutesOf(window.start) === undefined) problems.push(`The start \"${window.start}\" is not a local time HH:MM from 00:00 to 23:59.`);\n if (minutesOf(window.end) === undefined) problems.push(`The end \"${window.end}\" is not a local time HH:MM from 00:00 to 23:59.`);\n for (const day of window.days ?? []) {\n if (!isDay(day)) problems.push(`The day ${day} is not a day of the week from 0 (Sunday) to 6.`);\n }\n if (window.days !== undefined && window.days.length === 0) problems.push(\"The window has no day, thus it does not open.\");\n return problems;\n}\n"],"mappings":";;AA6BA,MAAM,kBAA0B;CAC9B,MAAM,cAAe,WAAmD;CACxE,OAAO,gBAAgB,KAAA,IAAY,KAAK,IAAI,IAAI,YAAY,IAAI;AAClE;;AAGA,MAAM,gBAAgB;;;;;AAMtB,MAAa,iBAAiB,UAAgC,CAAC,OAAc;CAC3E,KAAK;CACL,MAAM,IAAI,KAAK;EACb,IAAI,UAAU,QAAQ,UAAU;EAChC,MAAM,OAAO,SAAgD;GAC3D,MAAM,OACJ,OAAO,gBAAgB,iBAAkB,QAAQ,IAAI,OAAO,aAAa,GAAI,aAAa,IAAI,WAAW,KAAK,KAAK,IAAI,GAAG,IAAI,CAAC;GACjI,IAAI,SAAS,KAAiC,QAAQ;GACtD,OAAO;EACT;EACA,IAAI,QAAQ,IAAI,EAAE;EAClB,MAAM,cAAoB;GACxB,UAAU;GACV,MAAkC,QAAQ;EAC5C;EACA,OAAO,OAAO,aAAa,aAAa,KAAK,GAAG,EAAE,MAAM,CAAC;CAC3D;AACF;;;;;AAMA,MAAa,YAAmC,8BAAc,EAAE,OAAO,KAAK,CAAC;;;;;;;;AAS7E,MAAa,SAAS,OAAc,IAAY,WAC9C,IAAI,SAAe,SAAS,WAAW;CACrC,IAAI,QAAQ,YAAY,MAAM;EAC5B,OAAO,OAAO,MAAM;EACpB;CACF;CACA,MAAM,cAAoB;EACxB,OAAO;EACP,OAAO,QAAQ,MAAM;CACvB;CACA,MAAM,SAAS,MAAM,MAAM,UAAU;EACnC,QAAQ,oBAAoB,SAAS,KAAK;EAC1C,QAAQ;CACV,CAAC;CACD,QAAQ,iBAAiB,SAAS,OAAO,EAAE,MAAM,KAAK,CAAC;AACzD,CAAC;;AAgBH,MAAM,SAAS,YAA2B;CACxC,KAAK,IAAI,OAAO,GAAG,OAAO,IAAI,QAAQ,GAAG,MAAM,QA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M,OAAO,MAAM,GAAG,GAAG,WAAW,KAAK,GAAI,WAAW,WAAW,MAAM,SAAS,UAAU,WAAW,OAAO,IAAI,KAAK,CAAC,CAAE;CAEnJ,WAAW,MAAM,GAAG,MAAM,IAAI,CAAC;CAC/B,KAAK,MAAM,MAAM,YAAY;EAC3B,IAAI,MAAM,WAAW,KAAK,SAAS;EACnC,MAAM,QAAQ,UAAU,IAAI,OAAO,IAAI;EACvC,IAAI,UAAU,KAAA,KAAa,OAAO,OAAO,KAAK,MAAM,QAAQ,OAAO;GAAE;GAAI,OAAO,CAAC;EAAO;CAC1F;AAEF;;AAGA,SAAgB,eAAe,QAA8B;CAC3D,MAAM,WAAqB,CAAC;CAC5B,IAAI,WAAW,OAAO,IAAI,MAAM,KAAA,GAAW,SAAS,KAAK,IAAI,OAAO,KAAK,uCAAuC;CAChH,IAAI,UAAU,OAAO,KAAK,MAAM,KAAA,GAAW,SAAS,KAAK,cAAc,OAAO,MAAM,iDAAiD;CACrI,IAAI,UAAU,OAAO,GAAG,MAAM,KAAA,GAAW,SAAS,KAAK,YAAY,OAAO,IAAI,iDAAiD;CAC/H,KAAK,MAAM,OAAO,OAAO,QAAQ,CAAC,GAChC,IAAI,CAAC,MAAM,GAAG,GAAG,SAAS,KAAK,WAAW,IAAI,gDAAgD;CAEhG,IAAI,OAAO,SAAS,KAAA,KAAa,OAAO,KAAK,WAAW,GAAG,SAAS,KAAK,+CAA+C;CACxH,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"clock.js","names":[],"sources":["../src/clock.ts"],"sourcesContent":["// @mark1russell7/algebra/clock: the clock of a runtime, a fake clock for tests, and civil time. It imports\n// nothing: no `node:` module, no zod and no other package. Thus a client in a browser can use it.\n// The service gives this clock again as @mark1russell7/service/clock.\n\n/**\n * This function stops a timer that `Clock.after` started. A second use does nothing. A clock of\n * the platform also gives `unref`: after it, the timer does not keep a Node process alive.\n */\nexport type Cancel = (() => void) & { readonly unref?: (() => void) | undefined };\n\n/**\n * A clock gives the monotonic time in milliseconds, and it starts a function after a delay. The\n * runtime reads time only through a clock. Thus a test can give a fake clock, and the timing of\n * the runtime is then exact and repeatable.\n */\nexport interface Clock {\n /**\n * This mark tells the time of `now()`. A clock gives the monotonic time, thus the mark is\n * `\"monotonic\"` or absent. A `WallClock` has the mark `\"wall\"`, thus it is not a `Clock`.\n */\n readonly time?: \"monotonic\" | undefined;\n /** This method gives the monotonic time, in milliseconds. A step of the wall clock does not move it. */\n now(): number;\n /** This method starts `run` after `ms` milliseconds, and gives the function that stops it. */\n after(ms: number, run: () => void): Cancel;\n}\n\n/**\n * A wall clock gives the instant: the time in milliseconds from 1970-01-01T00:00:00Z, as `Date.now()`\n * gives it. Civil time, a calendar and a time that another computer sends need the instant. A step\n * of the wall clock moves `now()`, also back. Each timer waits on the monotonic time, thus a step\n * does not move a timer. The mark `\"wall\"` keeps the two clocks apart: a function that needs the\n * monotonic time does not get a wall clock, and the reverse.\n */\nexport interface WallClock {\n readonly time: \"wall\";\n /** This method gives the instant, in milliseconds from the epoch. A step of the wall clock moves it. */\n now(): number;\n /** This method starts `run` after `ms` milliseconds of monotonic time, and gives the function that stops it. */\n after(ms: number, run: () => void): Cancel;\n}\n\n/** These options make a clock of the platform. */\nexport interface PlatformClockOptions {\n /** True makes each timer not keep a Node process alive. The default is false, as with `setTimeout`. */\n readonly unref?: boolean | undefined;\n}\n\n/** This function gives the monotonic time: `performance.now()`, or `Date.now()` on a platform without `performance`. */\nconst monotonic = (): number => {\n const performance = (globalThis as { performance?: { now(): number } }).performance;\n return performance === undefined ? Date.now() : performance.now();\n};\n\n/** This delay is the longest that `setTimeout` takes, in milliseconds. A longer delay starts the function after 1 ms. */\nconst LONGEST_DELAY = 0x7fffffff;\n\n/**\n * This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel\n * that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.\n */\nexport const platformClock = (options: PlatformClockOptions = {}): Clock => ({\n now: monotonic,\n after(ms, run) {\n let unrefed = options.unref === true;\n const arm = (left: number): ReturnType<typeof setTimeout> => {\n const step =\n left > LONGEST_DELAY ? setTimeout(() => (timer = arm(left - LONGEST_DELAY)), LONGEST_DELAY) : setTimeout(run, Math.max(0, left));\n if (unrefed) (step as { unref?: () => void }).unref?.();\n return step;\n };\n let timer = arm(ms);\n const unref = (): void => {\n unrefed = true;\n (timer as { unref?: () => void }).unref?.();\n };\n return Object.assign(() => clearTimeout(timer), { unref });\n },\n});\n\n/**\n * This clock reads `performance.now()` and uses `setTimeout`. Its timers do not keep a Node\n * process alive, thus a timer that is not stopped cannot stop the exit of the process.\n */\nexport const realClock: Clock = /* @__PURE__ */ platformClock({ unref: true });\n\n/**\n * This function makes the wall clock of the platform: `Date.now()`, and the timers of\n * `platformClock`. The options are the options of `platformClock`.\n */\nexport const wallClock = (options: PlatformClockOptions = {}): WallClock => ({\n time: \"wall\",\n now: () => Date.now(),\n after: platformClock(options).after,\n});\n\n/**\n * This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal\n * aborts, the promise stops its timer and rejects with the reason of the signal. A signal that\n * aborted before the call rejects at once, and starts no timer. Each end removes the listener from\n * the signal. A timer of `realClock` does not keep a Node process alive: give `platformClock()` to\n * keep the process alive until the delay ends.\n */\nexport const delay = (clock: Clock, ms: number, signal?: AbortSignal): Promise<void> =>\n new Promise<void>((resolve, reject) => {\n if (signal?.aborted === true) {\n reject(signal.reason);\n return;\n }\n const abort = (): void => {\n cancel();\n reject(signal?.reason);\n };\n const cancel = clock.after(ms, () => {\n signal?.removeEventListener(\"abort\", abort);\n resolve();\n });\n signal?.addEventListener(\"abort\", abort, { once: true });\n });\n\n/** A fake clock moves only when a test tells it to. */\nexport interface FakeClock extends Clock {\n /** This method moves the clock forward by `ms`, and it starts each timer that comes due, in sequence. */\n advance(ms: number): Promise<void>;\n /**\n * This method moves the clock forward by `ms` and starts no timer, as a blocked event loop does.\n * The next `advance` then starts each timer that came due during the block, late.\n */\n block(ms: number): void;\n /** This count is the number of timers that are not stopped and not called. */\n readonly pending: number;\n}\n\n/** This function lets the promises that are ready continue, before the clock moves again. */\nconst settle = async (): Promise<void> => {\n for (let turn = 0; turn < 64; turn += 1) await Promise.resolve();\n};\n\n/** This function makes a fake clock. Its time starts at `start` milliseconds. */\nexport const fakeClock = (start = 0): FakeClock => {\n let now = start;\n let sequence = 0;\n const timers: Array<{ at: number; order: number; run: () => void; live: boolean }> = [];\n const due = (end: number) => {\n let best: (typeof timers)[number] | undefined;\n for (const timer of timers)\n if (timer.live && timer.at <= end && (best === undefined || timer.at < best.at || (timer.at === best.at && timer.order < best.order)))\n best = timer;\n return best;\n };\n return {\n now: () => now,\n after(ms, run): Cancel {\n const timer = { at: now + Math.max(0, ms), order: sequence++, run, live: true };\n timers.push(timer);\n return () => {\n timer.live = false;\n };\n },\n async advance(ms) {\n // a monotonic clock does not go back\n const end = now + Math.max(0, ms);\n await settle();\n for (;;) {\n const timer = due(end);\n if (timer === undefined) break;\n timer.live = false;\n // a timer that came due during a block runs late, at the time of the clock\n now = Math.max(now, timer.at);\n timer.run();\n await settle();\n }\n now = Math.max(now, end);\n for (let at = timers.length - 1; at >= 0; at -= 1) if (!timers[at]!.live) timers.splice(at, 1);\n await settle();\n },\n block(ms) {\n now += Math.max(0, ms);\n },\n get pending() {\n return timers.filter((timer) => timer.live).length;\n },\n };\n};\n\n/** A fake wall clock moves only when a test tells it to. A test can also step its instant, as a wall clock steps. */\nexport interface FakeWallClock extends WallClock {\n /** This method moves the instant and the monotonic time forward by `ms`, and it starts each timer that comes due, in sequence. */\n advance(ms: number): Promise<void>;\n /** This method moves the instant and the monotonic time forward by `ms`, and starts no timer, as a blocked event loop does. */\n block(ms: number): void;\n /** This method steps the instant to `instant`, forward or back. The monotonic time does not move, thus no timer comes due. */\n set(instant: number): void;\n /** This count is the number of timers that are not stopped and not called. */\n readonly pending: number;\n}\n\n/**\n * This function makes a fake wall clock. Its instant starts at `start`, in milliseconds from the\n * epoch. Its timers are the timers of a `fakeClock`, thus a step of the instant moves no timer.\n */\nexport const fakeWallClock = (start: number): FakeWallClock => {\n const monotonicTime = fakeClock(0);\n let offset = start;\n return {\n time: \"wall\",\n now: () => monotonicTime.now() + offset,\n after: (ms, run) => monotonicTime.after(ms, run),\n advance: (ms) => monotonicTime.advance(ms),\n block(ms) {\n monotonicTime.block(ms);\n },\n set(instant) {\n offset = instant - monotonicTime.now();\n },\n get pending() {\n return monotonicTime.pending;\n },\n };\n};\n\n// Civil time: the wall-clock time of an instant in a time zone, and windows of local time. A clock\n// gives instants, and civil time maps an instant to the time on the wall of a zone. It uses only\n// `Intl.DateTimeFormat`, thus this entry still imports nothing.\n\n/**\n * The wall-clock time of an instant in a time zone. An instant is a time in milliseconds from\n * 1970-01-01T00:00:00Z, as `Date.now()` and `WallClock.now()` give. It is not the monotonic time of `Clock.now()`.\n */\nexport interface LocalTime {\n readonly year: number;\n /** The month, from 1 (January) to 12. */\n readonly month: number;\n /** The day of the month, from 1. */\n readonly day: number;\n /** The hour, from 0 to 23. */\n readonly hour: number;\n readonly minute: number;\n readonly second: number;\n /** The day of the week, from 0 (Sunday) to 6 (Saturday). */\n readonly weekday: number;\n /** The offset of the zone from UTC at the instant, in minutes. It is positive east of Greenwich. */\n readonly offset: number;\n}\n\n/**\n * A window of local time that occurs on each day, or on some days of the week. The window is\n * data, thus a configuration can hold it.\n *\n * - The start is in the window, and the end is not.\n * - An end before the start crosses midnight. An end equal to the start gives a window of a full day.\n * - In a change of the clock, the window follows the time on the wall. A start or an end in a gap\n * moves to the end of the gap. Thus a window with its start and its end in one gap does not open.\n * - A local time that occurs two times is in the window two times.\n */\nexport interface TimeWindow {\n /** The IANA name of the time zone, for example `Europe/Berlin`. */\n readonly zone: string;\n /** The start, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly start: string;\n /** The end, a local time `HH:MM` from `00:00` to `23:59`. */\n readonly end: string;\n /**\n * The days of the week on which the window starts, from 0 (Sunday) to 6. When it is absent, the\n * window starts on each day. A window across midnight continues into the next day.\n */\n readonly days?: readonly number[] | undefined;\n}\n\n/** A change of a window: the instant at which it opens or closes. */\nexport interface WindowBoundary {\n readonly at: number;\n /** True: the window opens at `at`. False: it closes. */\n readonly opens: boolean;\n}\n\nconst MINUTE_MS = 60_000;\nconst DAY_MS = 86_400_000;\nconst WEEKDAYS: Readonly<Record<string, number>> = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };\nconst LOCAL_TIME = /^([01]\\d|2[0-3]):([0-5]\\d)$/;\n\n/**\n * The cache has one format for each good zone name. It keeps no bad name, and it starts again at\n * `ZONE_CACHE_MAX` names. Thus names from configuration input cannot make it grow without a limit.\n */\nconst ZONE_FORMATS = new Map<string, Intl.DateTimeFormat>();\nconst ZONE_CACHE_MAX = 512;\n/** The longest IANA name has 32 characters. A longer name than this limit is not a zone. */\nconst ZONE_NAME_MAX = 100;\n\nconst zoneFormat = (zone: string): Intl.DateTimeFormat | undefined => {\n let format = ZONE_FORMATS.get(zone);\n if (format === undefined) {\n if (typeof zone !== \"string\" || zone.length > ZONE_NAME_MAX) return undefined;\n try {\n format = new Intl.DateTimeFormat(\"en-US\", {\n timeZone: zone,\n hourCycle: \"h23\",\n era: \"short\",\n year: \"numeric\",\n month: \"numeric\",\n day: \"numeric\",\n hour: \"numeric\",\n minute: \"numeric\",\n second: \"numeric\",\n weekday: \"short\",\n });\n } catch {\n return undefined;\n }\n if (ZONE_FORMATS.size >= ZONE_CACHE_MAX) ZONE_FORMATS.clear();\n ZONE_FORMATS.set(zone, format);\n }\n return format;\n};\n\n/**\n * This function gives the wall-clock time of `instant` in `zone`. It gives `undefined` for a name\n * that is not a time zone, and for an instant that is not a date.\n */\nexport function localTime(instant: number, zone: string): LocalTime | undefined {\n const format = zoneFormat(zone);\n if (format === undefined || !Number.isFinite(instant)) return undefined;\n let parts: Intl.DateTimeFormatPart[];\n try {\n parts = format.formatToParts(instant);\n } catch {\n // an instant out of the range of a date\n return undefined;\n }\n const field: Record<string, string> = {};\n for (const part of parts) field[part.type] = part.value;\n const before = field[\"era\"] === \"BC\" || field[\"era\"] === \"B\";\n const year = before ? 1 - Number(field[\"year\"]) : Number(field[\"year\"]);\n const month = Number(field[\"month\"]);\n const day = Number(field[\"day\"]);\n const hour = Number(field[\"hour\"]) % 24;\n const minute = Number(field[\"minute\"]);\n const second = Number(field[\"second\"]);\n const whole = Math.floor(instant / 1000) * 1000;\n return { year, month, day, hour, minute, second, weekday: WEEKDAYS[field[\"weekday\"] ?? \"\"] ?? 0, offset: (wallOf(year, month, day, hour, minute, second) - whole) / MINUTE_MS };\n}\n\n/** The wall-clock time as an instant in UTC. `Date.UTC` reads a year from 0 to 99 as 1900 to 1999, thus this function sets the year by itself. */\nconst wallOf = (year: number, month: number, day: number, hour: number, minute: number, second: number): number => {\n const wall = new Date(0);\n wall.setUTCFullYear(year, month - 1, day);\n wall.setUTCHours(hour, minute, second, 0);\n return wall.getTime();\n};\n\n/** The minutes from midnight of a local time `HH:MM`. */\nconst minutesOf = (time: string): number | undefined => {\n const match = LOCAL_TIME.exec(time);\n return match === null ? undefined : Number(match[1]) * 60 + Number(match[2]);\n};\n\ninterface CheckedWindow {\n readonly start: number;\n readonly end: number;\n readonly days: ReadonlySet<number> | undefined;\n}\n\nconst isDay = (day: number): boolean => Number.isInteger(day) && day >= 0 && day <= 6;\n\nconst checked = (window: TimeWindow): CheckedWindow | undefined => {\n const start = minutesOf(window.start);\n const end = minutesOf(window.end);\n if (start === undefined || end === undefined || zoneFormat(window.zone) === undefined) return undefined;\n if (window.days !== undefined && !window.days.every(isDay)) return undefined;\n return { start, end, days: window.days === undefined ? undefined : new Set(window.days) };\n};\n\nconst within = (window: CheckedWindow, local: LocalTime): boolean => {\n const minute = local.hour * 60 + local.minute;\n const startsOn = (weekday: number): boolean => window.days === undefined || window.days.has(weekday);\n if (window.start < window.end) return window.start <= minute && minute < window.end && startsOn(local.weekday);\n // across midnight, or a full day: the part from the start, then the part before the end on the next day\n return (minute >= window.start && startsOn(local.weekday)) || (minute < window.end && startsOn((local.weekday + 6) % 7));\n};\n\n/**\n * This function tells if `instant` is in the window. It does not throw. A bad window or a bad\n * instant gives `false`, and `windowProblems` finds the bad window.\n */\nexport function inWindow(instant: number, window: TimeWindow): boolean {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n return valid !== undefined && local !== undefined && within(valid, local);\n}\n\n/**\n * This function gives the instants at which the wall clock of `zone` shows `wall`, in order. It is\n * the inverse of `localTime`. The wall-clock time is in milliseconds with the arithmetic of UTC: the\n * UTC fields of `new Date(wall)` are the fields of the wall clock.\n *\n * - A wall-clock time in the gap of a change forward gives no instant.\n * - A wall-clock time in the hour of a change back gives two instants.\n * - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.\n *\n * The rule for a gap or for two instants is a choice of the caller. For example, RFC 5545 takes the\n * first of two instants, and in a gap it takes the offset before the gap. The search reads the\n * offset one day before and one day after `wall`. Thus it finds each instant when the offset\n * changes at most one time in these 2 days.\n */\nexport function instantsOf(wall: number, zone: string): number[] | undefined {\n if (zoneFormat(zone) === undefined || !Number.isFinite(wall)) return undefined;\n // localTime gives whole seconds: the search uses the whole second, and adds the milliseconds at the end\n const whole = Math.floor(wall / 1000) * 1000;\n const offsets = new Set<number>();\n for (const probe of [whole - DAY_MS, whole, whole + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.add(local.offset);\n }\n // no offset: the wall-clock time is out of the range of a date\n if (offsets.size === 0) return undefined;\n const found: number[] = [];\n for (const offset of offsets) {\n // an old offset of local mean time has seconds, thus the product is rounded to the millisecond\n const instant = whole - Math.round(offset * MINUTE_MS);\n const local = localTime(instant, zone);\n if (local === undefined) continue;\n const back = wallOf(local.year, local.month, local.day, local.hour, local.minute, local.second);\n if (back === whole && !found.includes(instant)) found.push(instant);\n }\n return found.sort((a, b) => a - b).map((instant) => instant + (wall - whole));\n}\n\n/**\n * This function gives the instants in `(from, to]` at which the offset of `zone` changes, in order\n * and exact to the millisecond. At each instant, the new offset starts. The search reads the offset\n * each 6 hours of the range, thus a long range costs more reads. No zone changes its offset two\n * times in 6 hours. A name that is not a time zone, or a bound that is not a date, gives `undefined`.\n */\nexport function offsetChanges(from: number, to: number, zone: string): number[] | undefined {\n if (localTime(from, zone) === undefined || localTime(to, zone) === undefined) return undefined;\n return changesOf(from, to, zone);\n}\n\n/** The instants in (from, to] at which the offset of the zone changes. `offsetChanges` tells the rule. */\nconst changesOf = (from: number, to: number, zone: string): number[] => {\n const offsetAt = (instant: number): number | undefined => localTime(instant, zone)?.offset;\n const found: number[] = [];\n let low = from;\n let before = offsetAt(low);\n while (low < to) {\n const high = Math.min(low + DAY_MS / 4, to);\n const after = offsetAt(high);\n if (before !== undefined && after !== undefined && after !== before) {\n // the first instant with the new offset\n let left = low;\n let right = high;\n while (right - left > 1) {\n const middle = Math.floor((left + right) / 2);\n if (offsetAt(middle) === before) left = middle;\n else right = middle;\n }\n found.push(right);\n }\n low = high;\n before = after;\n }\n return found;\n};\n\n/**\n * This function gives the first instant at which the wall clock of `zone` shows `wall` or a later\n * wall-clock time. The wall-clock time is in milliseconds with the arithmetic of UTC, as for\n * `instantsOf`. Thus a time on the wall, for example the start of a day, always has an instant.\n *\n * - A wall-clock time that occurs one time gives its instant.\n * - A wall-clock time in the hour of a change back gives the first of its two instants.\n * - A wall-clock time in the gap of a change forward gives the instant of the change: the end of the gap.\n * - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.\n *\n * The search reads the offset one day before and one day after `wall`, as `instantsOf` does. A gap\n * that is not in these 2 days also gives `undefined`.\n */\nexport function firstInstantOf(wall: number, zone: string): number | undefined {\n const instants = instantsOf(wall, zone);\n if (instants === undefined) return undefined;\n if (instants.length > 0) return instants[0];\n const whole = Math.floor(wall / 1000) * 1000;\n const offsets: number[] = [];\n for (const probe of [whole - DAY_MS, whole, whole + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.push(Math.round(local.offset * MINUTE_MS));\n }\n // A change at the instant c, from the offset o1 to the larger offset o2, skips the wall-clock times\n // from c + o1 to c + o2, without the end. Thus c is after wall - o2, and at or before wall - o1.\n const from = wall - Math.max(...offsets);\n const to = wall - Math.min(...offsets);\n const offsetAt = (instant: number): number | undefined => {\n const local = localTime(instant, zone);\n return local === undefined ? undefined : Math.round(local.offset * MINUTE_MS);\n };\n for (const change of changesOf(from, to, zone)) {\n const before = offsetAt(change - 1);\n const after = offsetAt(change);\n if (before !== undefined && after !== undefined && change + before <= wall && wall < change + after) return change;\n }\n return undefined;\n}\n\n/**\n * This function gives the next change of the window after `instant`: the first later instant at\n * which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`\n * for a bad window, and when the window does not change in the 9 days after `instant`. A window\n * with no day and a full-day window on each day do not change.\n */\nexport function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined {\n const valid = checked(window);\n const local = valid === undefined ? undefined : localTime(instant, window.zone);\n if (valid === undefined || local === undefined) return undefined;\n const inside = within(valid, local);\n const horizon = instant + 9 * DAY_MS;\n // The state changes only where the wall clock shows the start or the end, or where the offset changes.\n const candidates = changesOf(instant, horizon, window.zone);\n const midnight = wallOf(local.year, local.month, local.day, 0, 0, 0);\n for (let day = -1; day <= 10; day += 1) {\n for (const minutes of [valid.start, valid.end]) candidates.push(...(instantsOf(midnight + day * DAY_MS + minutes * MINUTE_MS, window.zone) ?? []));\n }\n candidates.sort((a, b) => a - b);\n for (const at of candidates) {\n if (at <= instant || at > horizon) continue;\n const there = localTime(at, window.zone);\n if (there !== undefined && within(valid, there) !== inside) return { at, opens: !inside };\n }\n return undefined;\n}\n\n/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */\nexport function windowProblems(window: TimeWindow): string[] {\n const problems: string[] = [];\n if (zoneFormat(window.zone) === undefined) problems.push(`\"${window.zone}\" is not the IANA name of a time zone.`);\n if (minutesOf(window.start) === undefined) problems.push(`The start \"${window.start}\" is not a local time HH:MM from 00:00 to 23:59.`);\n if (minutesOf(window.end) === undefined) problems.push(`The end \"${window.end}\" is not a local time HH:MM from 00:00 to 23:59.`);\n for (const day of window.days ?? []) {\n if (!isDay(day)) problems.push(`The day ${day} is not a day of the week from 0 (Sunday) to 6.`);\n }\n if (window.days !== undefined && window.days.length === 0) problems.push(\"The window has no day, thus it does not open.\");\n return problems;\n}\n"],"mappings":";;AAiDA,MAAM,kBAA0B;CAC9B,MAAM,cAAe,WAAmD;CACxE,OAAO,gBAAgB,KAAA,IAAY,KAAK,IAAI,IAAI,YAAY,IAAI;AAClE;;AAGA,MAAM,gBAAgB;;;;;AAMtB,MAAa,iBAAiB,UAAgC,CAAC,OAAc;CAC3E,KAAK;CACL,MAAM,IAAI,KAAK;EACb,IAAI,UAAU,QAAQ,UAAU;EAChC,MAAM,OAAO,SAAgD;GAC3D,MAAM,OACJ,OAAO,gBAAgB,iBAAkB,QAAQ,IAAI,OAAO,aAAa,GAAI,aAAa,IAAI,WAAW,KAAK,KAAK,IAAI,GAAG,IAAI,CAAC;GACjI,IAAI,SAAS,KAAiC,QAAQ;GACtD,OAAO;EACT;EACA,IAAI,QAAQ,IAAI,EAAE;EAClB,MAAM,cAAoB;GACxB,UAAU;GACV,MAAkC,QAAQ;EAC5C;EACA,OAAO,OAAO,aAAa,aAAa,KAAK,GAAG,EAAE,MAAM,CAAC;CAC3D;AACF;;;;;AAMA,MAAa,YAAmC,8BAAc,EAAE,OAAO,KAAK,CAAC;;;;;AAM7E,MAAa,aAAa,UAAgC,CAAC,OAAkB;CAC3E,MAAM;CACN,WAAW,KAAK,IAAI;CACpB,OAAO,cAAc,OAAO,CAAC,CAAC;AAChC;;;;;;;;AASA,MAAa,SAAS,OAAc,IAAY,WAC9C,IAAI,SAAe,SAAS,WAAW;CACrC,IAAI,QAAQ,YAAY,MAAM;EAC5B,OAAO,OAAO,MAAM;EACpB;CACF;CACA,MAAM,cAAoB;EACxB,OAAO;EACP,OAAO,QAAQ,M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ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { a as Realization, c as Unit, d as machineProblems, f as readsFired, i as Over, l as Vantage, n as FIRED, o as Timer, r as Machine, s as TransitionDef, t as Expr, u as exprKnobs } from "./machine-yGpVFyfh.js";
|
|
2
2
|
import { i as replay, n as Replayable, r as Transition, t as ReplayOptions } from "./replay-DRTZc7Pw.js";
|
|
3
|
-
import { A as timerBand, C as parseExpr, D as recovery, E as realizationOf, M as timerUnder, N as transitionOn, O as stackedDetection, S as overValues, T as realizationKnobs, _ as detection, a as Detection, b as firings, c as KnobValues, d as Recovery, f as ScheduleOptions, g as algebraProblems, h as TimerBounds, i as Band, j as timerOf, k as synGiveUpSends, l as Manifestation, m as Situation, n as RunMachineOptions, o as Detector, p as Scheduled, r as runMachine, s as Firing, t as MachineRun, u as Node, v as errnoOf, w as probeTrain, x as modelSource, y as evaluate } from "./run-
|
|
3
|
+
import { A as timerBand, C as parseExpr, D as recovery, E as realizationOf, M as timerUnder, N as transitionOn, O as stackedDetection, S as overValues, T as realizationKnobs, _ as detection, a as Detection, b as firings, c as KnobValues, d as Recovery, f as ScheduleOptions, g as algebraProblems, h as TimerBounds, i as Band, j as timerOf, k as synGiveUpSends, l as Manifestation, m as Situation, n as RunMachineOptions, o as Detector, p as Scheduled, r as runMachine, s as Firing, t as MachineRun, u as Node, v as errnoOf, w as probeTrain, x as modelSource, y as evaluate } from "./run-C5U_EFvv.js";
|
|
4
4
|
/** This function gives one value or many values as an array. */
|
|
5
5
|
export declare const arr: <T>(value: T | readonly T[]) => readonly T[];
|
|
6
6
|
/** This function gives the states that a transition leaves. The state "*" is each state that is not final. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { r as Machine, s as TransitionDef, t as Expr } from "./machine-yGpVFyfh.js";
|
|
2
|
-
import { n as Clock } from "./clock-
|
|
2
|
+
import { n as Clock } from "./clock-BNBDlRRA.js";
|
|
3
3
|
import { r as Transition } from "./replay-DRTZc7Pw.js";
|
|
4
4
|
import { Extent, Quantity, Rational, Unknown } from "@mark1russell7/quantity";
|
|
5
5
|
/**
|
package/build/run.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as RunMachineOptions, r as runMachine, t as MachineRun } from "./run-
|
|
1
|
+
import { n as RunMachineOptions, r as runMachine, t as MachineRun } from "./run-C5U_EFvv.js";
|
|
2
2
|
export { MachineRun, RunMachineOptions, runMachine };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/algebra",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.9",
|
|
5
5
|
"description": "State machines as data, and the timer algebra over them: closed forms for a machine's timers in exact rationals, detection as the first live timer, recovery after a stall, and the replay of a trace.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"state-machine",
|