@mark1russell7/algebra 0.8.7 → 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/README.md +17 -1
- package/build/clock-BNBDlRRA.d.ts +199 -0
- package/build/clock.d.ts +2 -2
- package/build/clock.js +127 -14
- package/build/clock.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/{run-yLPq18NQ.d.ts → run-C5U_EFvv.d.ts} +1 -1
- package/build/run.d.ts +1 -1
- package/package.json +1 -1
- package/build/clock-5u5mZpjL.d.ts +0 -115
package/README.md
CHANGED
|
@@ -127,13 +127,17 @@ An app gives `realClock` of `@mark1russell7/algebra/clock` in place of the fake
|
|
|
127
127
|
| `platformClock({ unref })` | A clock on `performance.now()` and `setTimeout`. With `unref: true`, its timers do not keep a Node process alive. |
|
|
128
128
|
| `realClock` | The clock of the platform with `unref: true`. |
|
|
129
129
|
| `fakeClock(start)` | A clock that moves only on `advance(ms)`. `block(ms)` moves it and starts no timer, as a blocked event loop does. `pending` counts the live timers. |
|
|
130
|
+
| `delay(clock, ms, signal?)` | A promise that resolves after `ms` on the clock. An abort of the signal stops the timer and rejects with the reason of the signal. |
|
|
130
131
|
|
|
131
132
|
```ts
|
|
132
|
-
import { fakeClock } from "@mark1russell7/algebra/clock";
|
|
133
|
+
import { delay, fakeClock, realClock } from "@mark1russell7/algebra/clock";
|
|
133
134
|
|
|
134
135
|
const clock = fakeClock();
|
|
135
136
|
clock.after(30_000, () => cache.sweep());
|
|
136
137
|
await clock.advance(30_000); // the sweep runs, at 30 s exactly
|
|
138
|
+
|
|
139
|
+
// a wait before the next try, which the drain stops
|
|
140
|
+
await delay(realClock, retryAfter, controller.signal);
|
|
137
141
|
```
|
|
138
142
|
|
|
139
143
|
### Civil time
|
|
@@ -143,6 +147,8 @@ The entry also gives civil time: the time on the wall of a time zone at an insta
|
|
|
143
147
|
| Export | What it does |
|
|
144
148
|
| --- | --- |
|
|
145
149
|
| `localTime(instant, zone)` | It gives the year, the month, the day, the hour, the minute, the second, the day of the week and the offset of the zone, or `undefined` for a bad zone. |
|
|
150
|
+
| `instantsOf(wall, zone)` | The inverse of `localTime`: the instants at which the clock of the zone shows the wall-clock time `wall`, in order. A gap gives none, and an hour that occurs two times gives two. The caller chooses the rule for these two cases. |
|
|
151
|
+
| `offsetChanges(from, to, zone)` | It gives the instants in `(from, to]` at which the offset of the zone changes, exact to the millisecond. |
|
|
146
152
|
| `TimeWindow` | A window as data: `{ zone, start, end, days? }`. The start and the end are local times `HH:MM`. |
|
|
147
153
|
| `inWindow(instant, window)` | It tells if the instant is in the window. A bad window gives `false`. |
|
|
148
154
|
| `nextBoundary(instant, window)` | It gives the next instant at which the window opens or closes, as `{ at, opens }`. A timer can wait for it. It looks 9 days ahead at most. It gives `undefined` for a window with no change in that time. |
|
|
@@ -155,6 +161,16 @@ These rules apply to a window:
|
|
|
155
161
|
- The window follows the time on the wall when the clock changes. A start or an end in a gap moves to the end of the gap. A local time that occurs two times is in the window two times.
|
|
156
162
|
- An example: on 2026-10-25 in Berlin, a window from 22:00 to 02:30 closes at 00:30Z. It opens again at 01:00Z and closes at 01:30Z.
|
|
157
163
|
|
|
164
|
+
A wall-clock time for `instantsOf` is in milliseconds with the arithmetic of UTC: the UTC fields of `new Date(wall)` are the fields of the clock. Thus `Date.UTC(2026, 9, 25, 2, 30)` is 02:30 on the wall.
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
import { instantsOf, offsetChanges } from "@mark1russell7/algebra/clock";
|
|
168
|
+
|
|
169
|
+
instantsOf(Date.UTC(2026, 9, 25, 2, 30), "Europe/Berlin"); // two instants: 00:30Z and 01:30Z
|
|
170
|
+
instantsOf(Date.UTC(2026, 2, 29, 2, 30), "Europe/Berlin"); // []: the clock goes from 02:00 to 03:00
|
|
171
|
+
offsetChanges(Date.UTC(2026, 0, 1), Date.UTC(2027, 0, 1), "Europe/Berlin"); // 2026-03-29T01:00Z and 2026-10-25T01:00Z
|
|
172
|
+
```
|
|
173
|
+
|
|
158
174
|
```ts
|
|
159
175
|
import { inWindow, nextBoundary, realClock } from "@mark1russell7/algebra/clock";
|
|
160
176
|
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function stops a timer that `Clock.after` started. A second use does nothing. A clock of
|
|
3
|
+
* the platform also gives `unref`: after it, the timer does not keep a Node process alive.
|
|
4
|
+
*/
|
|
5
|
+
type Cancel = (() => void) & {
|
|
6
|
+
readonly unref?: (() => void) | undefined;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* A clock gives the monotonic time in milliseconds, and it starts a function after a delay. The
|
|
10
|
+
* runtime reads time only through a clock. Thus a test can give a fake clock, and the timing of
|
|
11
|
+
* the runtime is then exact and repeatable.
|
|
12
|
+
*/
|
|
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;
|
|
19
|
+
/** This method gives the monotonic time, in milliseconds. A step of the wall clock does not move it. */
|
|
20
|
+
now(): number;
|
|
21
|
+
/** This method starts `run` after `ms` milliseconds, and gives the function that stops it. */
|
|
22
|
+
after(ms: number, run: () => void): Cancel;
|
|
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
|
+
}
|
|
38
|
+
/** These options make a clock of the platform. */
|
|
39
|
+
interface PlatformClockOptions {
|
|
40
|
+
/** True makes each timer not keep a Node process alive. The default is false, as with `setTimeout`. */
|
|
41
|
+
readonly unref?: boolean | undefined;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel
|
|
45
|
+
* that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.
|
|
46
|
+
*/
|
|
47
|
+
declare const platformClock: (options?: PlatformClockOptions) => Clock;
|
|
48
|
+
/**
|
|
49
|
+
* This clock reads `performance.now()` and uses `setTimeout`. Its timers do not keep a Node
|
|
50
|
+
* process alive, thus a timer that is not stopped cannot stop the exit of the process.
|
|
51
|
+
*/
|
|
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;
|
|
58
|
+
/**
|
|
59
|
+
* This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal
|
|
60
|
+
* aborts, the promise stops its timer and rejects with the reason of the signal. A signal that
|
|
61
|
+
* aborted before the call rejects at once, and starts no timer. Each end removes the listener from
|
|
62
|
+
* the signal. A timer of `realClock` does not keep a Node process alive: give `platformClock()` to
|
|
63
|
+
* keep the process alive until the delay ends.
|
|
64
|
+
*/
|
|
65
|
+
declare const delay: (clock: Clock, ms: number, signal?: AbortSignal) => Promise<void>;
|
|
66
|
+
/** A fake clock moves only when a test tells it to. */
|
|
67
|
+
interface FakeClock extends Clock {
|
|
68
|
+
/** This method moves the clock forward by `ms`, and it starts each timer that comes due, in sequence. */
|
|
69
|
+
advance(ms: number): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* This method moves the clock forward by `ms` and starts no timer, as a blocked event loop does.
|
|
72
|
+
* The next `advance` then starts each timer that came due during the block, late.
|
|
73
|
+
*/
|
|
74
|
+
block(ms: number): void;
|
|
75
|
+
/** This count is the number of timers that are not stopped and not called. */
|
|
76
|
+
readonly pending: number;
|
|
77
|
+
}
|
|
78
|
+
/** This function makes a fake clock. Its time starts at `start` milliseconds. */
|
|
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;
|
|
96
|
+
/**
|
|
97
|
+
* The wall-clock time of an instant in a time zone. An instant is a time in milliseconds from
|
|
98
|
+
* 1970-01-01T00:00:00Z, as `Date.now()` and `WallClock.now()` give. It is not the monotonic time of `Clock.now()`.
|
|
99
|
+
*/
|
|
100
|
+
interface LocalTime {
|
|
101
|
+
readonly year: number;
|
|
102
|
+
/** The month, from 1 (January) to 12. */
|
|
103
|
+
readonly month: number;
|
|
104
|
+
/** The day of the month, from 1. */
|
|
105
|
+
readonly day: number;
|
|
106
|
+
/** The hour, from 0 to 23. */
|
|
107
|
+
readonly hour: number;
|
|
108
|
+
readonly minute: number;
|
|
109
|
+
readonly second: number;
|
|
110
|
+
/** The day of the week, from 0 (Sunday) to 6 (Saturday). */
|
|
111
|
+
readonly weekday: number;
|
|
112
|
+
/** The offset of the zone from UTC at the instant, in minutes. It is positive east of Greenwich. */
|
|
113
|
+
readonly offset: number;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A window of local time that occurs on each day, or on some days of the week. The window is
|
|
117
|
+
* data, thus a configuration can hold it.
|
|
118
|
+
*
|
|
119
|
+
* - The start is in the window, and the end is not.
|
|
120
|
+
* - An end before the start crosses midnight. An end equal to the start gives a window of a full day.
|
|
121
|
+
* - In a change of the clock, the window follows the time on the wall. A start or an end in a gap
|
|
122
|
+
* moves to the end of the gap. Thus a window with its start and its end in one gap does not open.
|
|
123
|
+
* - A local time that occurs two times is in the window two times.
|
|
124
|
+
*/
|
|
125
|
+
interface TimeWindow {
|
|
126
|
+
/** The IANA name of the time zone, for example `Europe/Berlin`. */
|
|
127
|
+
readonly zone: string;
|
|
128
|
+
/** The start, a local time `HH:MM` from `00:00` to `23:59`. */
|
|
129
|
+
readonly start: string;
|
|
130
|
+
/** The end, a local time `HH:MM` from `00:00` to `23:59`. */
|
|
131
|
+
readonly end: string;
|
|
132
|
+
/**
|
|
133
|
+
* The days of the week on which the window starts, from 0 (Sunday) to 6. When it is absent, the
|
|
134
|
+
* window starts on each day. A window across midnight continues into the next day.
|
|
135
|
+
*/
|
|
136
|
+
readonly days?: readonly number[] | undefined;
|
|
137
|
+
}
|
|
138
|
+
/** A change of a window: the instant at which it opens or closes. */
|
|
139
|
+
interface WindowBoundary {
|
|
140
|
+
readonly at: number;
|
|
141
|
+
/** True: the window opens at `at`. False: it closes. */
|
|
142
|
+
readonly opens: boolean;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* This function gives the wall-clock time of `instant` in `zone`. It gives `undefined` for a name
|
|
146
|
+
* that is not a time zone, and for an instant that is not a date.
|
|
147
|
+
*/
|
|
148
|
+
declare function localTime(instant: number, zone: string): LocalTime | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* This function tells if `instant` is in the window. It does not throw. A bad window or a bad
|
|
151
|
+
* instant gives `false`, and `windowProblems` finds the bad window.
|
|
152
|
+
*/
|
|
153
|
+
declare function inWindow(instant: number, window: TimeWindow): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* This function gives the instants at which the wall clock of `zone` shows `wall`, in order. It is
|
|
156
|
+
* the inverse of `localTime`. The wall-clock time is in milliseconds with the arithmetic of UTC: the
|
|
157
|
+
* UTC fields of `new Date(wall)` are the fields of the wall clock.
|
|
158
|
+
*
|
|
159
|
+
* - A wall-clock time in the gap of a change forward gives no instant.
|
|
160
|
+
* - A wall-clock time in the hour of a change back gives two instants.
|
|
161
|
+
* - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.
|
|
162
|
+
*
|
|
163
|
+
* The rule for a gap or for two instants is a choice of the caller. For example, RFC 5545 takes the
|
|
164
|
+
* first of two instants, and in a gap it takes the offset before the gap. The search reads the
|
|
165
|
+
* offset one day before and one day after `wall`. Thus it finds each instant when the offset
|
|
166
|
+
* changes at most one time in these 2 days.
|
|
167
|
+
*/
|
|
168
|
+
declare function instantsOf(wall: number, zone: string): number[] | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* This function gives the instants in `(from, to]` at which the offset of `zone` changes, in order
|
|
171
|
+
* and exact to the millisecond. At each instant, the new offset starts. The search reads the offset
|
|
172
|
+
* each 6 hours of the range, thus a long range costs more reads. No zone changes its offset two
|
|
173
|
+
* times in 6 hours. A name that is not a time zone, or a bound that is not a date, gives `undefined`.
|
|
174
|
+
*/
|
|
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;
|
|
190
|
+
/**
|
|
191
|
+
* This function gives the next change of the window after `instant`: the first later instant at
|
|
192
|
+
* which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`
|
|
193
|
+
* for a bad window, and when the window does not change in the 9 days after `instant`. A window
|
|
194
|
+
* with no day and a full-day window on each day do not change.
|
|
195
|
+
*/
|
|
196
|
+
declare function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined;
|
|
197
|
+
/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */
|
|
198
|
+
declare function windowProblems(window: TimeWindow): string[];
|
|
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 { a as
|
|
2
|
-
export { Cancel, Clock, FakeClock, LocalTime, PlatformClockOptions, TimeWindow, WindowBoundary, fakeClock, inWindow, localTime, nextBoundary, 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
|
@@ -32,6 +32,37 @@ const platformClock = (options = {}) => ({
|
|
|
32
32
|
* process alive, thus a timer that is not stopped cannot stop the exit of the process.
|
|
33
33
|
*/
|
|
34
34
|
const realClock = /* @__PURE__ */ platformClock({ unref: true });
|
|
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
|
+
/**
|
|
45
|
+
* This function gives a promise that resolves after `ms` milliseconds on the clock. When the signal
|
|
46
|
+
* aborts, the promise stops its timer and rejects with the reason of the signal. A signal that
|
|
47
|
+
* aborted before the call rejects at once, and starts no timer. Each end removes the listener from
|
|
48
|
+
* the signal. A timer of `realClock` does not keep a Node process alive: give `platformClock()` to
|
|
49
|
+
* keep the process alive until the delay ends.
|
|
50
|
+
*/
|
|
51
|
+
const delay = (clock, ms, signal) => new Promise((resolve, reject) => {
|
|
52
|
+
if (signal?.aborted === true) {
|
|
53
|
+
reject(signal.reason);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const abort = () => {
|
|
57
|
+
cancel();
|
|
58
|
+
reject(signal?.reason);
|
|
59
|
+
};
|
|
60
|
+
const cancel = clock.after(ms, () => {
|
|
61
|
+
signal?.removeEventListener("abort", abort);
|
|
62
|
+
resolve();
|
|
63
|
+
});
|
|
64
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
65
|
+
});
|
|
35
66
|
/** This function lets the promises that are ready continue, before the clock moves again. */
|
|
36
67
|
const settle = async () => {
|
|
37
68
|
for (let turn = 0; turn < 64; turn += 1) await Promise.resolve();
|
|
@@ -83,6 +114,29 @@ const fakeClock = (start = 0) => {
|
|
|
83
114
|
}
|
|
84
115
|
};
|
|
85
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
|
+
};
|
|
86
140
|
const MINUTE_MS = 6e4;
|
|
87
141
|
const DAY_MS = 864e5;
|
|
88
142
|
const WEEKDAYS = {
|
|
@@ -201,32 +255,52 @@ function inWindow(instant, window) {
|
|
|
201
255
|
return valid !== void 0 && local !== void 0 && within(valid, local);
|
|
202
256
|
}
|
|
203
257
|
/**
|
|
204
|
-
*
|
|
205
|
-
*
|
|
258
|
+
* This function gives the instants at which the wall clock of `zone` shows `wall`, in order. It is
|
|
259
|
+
* the inverse of `localTime`. The wall-clock time is in milliseconds with the arithmetic of UTC: the
|
|
260
|
+
* UTC fields of `new Date(wall)` are the fields of the wall clock.
|
|
261
|
+
*
|
|
262
|
+
* - A wall-clock time in the gap of a change forward gives no instant.
|
|
263
|
+
* - A wall-clock time in the hour of a change back gives two instants.
|
|
264
|
+
* - A name that is not a time zone, or a wall-clock time that is not a date, gives `undefined`.
|
|
265
|
+
*
|
|
266
|
+
* The rule for a gap or for two instants is a choice of the caller. For example, RFC 5545 takes the
|
|
267
|
+
* first of two instants, and in a gap it takes the offset before the gap. The search reads the
|
|
268
|
+
* offset one day before and one day after `wall`. Thus it finds each instant when the offset
|
|
269
|
+
* changes at most one time in these 2 days.
|
|
206
270
|
*/
|
|
207
|
-
|
|
271
|
+
function instantsOf(wall, zone) {
|
|
272
|
+
if (zoneFormat(zone) === void 0 || !Number.isFinite(wall)) return void 0;
|
|
273
|
+
const whole = Math.floor(wall / 1e3) * 1e3;
|
|
208
274
|
const offsets = /* @__PURE__ */ new Set();
|
|
209
275
|
for (const probe of [
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
276
|
+
whole - DAY_MS,
|
|
277
|
+
whole,
|
|
278
|
+
whole + DAY_MS
|
|
213
279
|
]) {
|
|
214
280
|
const local = localTime(probe, zone);
|
|
215
281
|
if (local !== void 0) offsets.add(local.offset);
|
|
216
282
|
}
|
|
283
|
+
if (offsets.size === 0) return void 0;
|
|
217
284
|
const found = [];
|
|
218
285
|
for (const offset of offsets) {
|
|
219
|
-
const instant =
|
|
286
|
+
const instant = whole - Math.round(offset * MINUTE_MS);
|
|
220
287
|
const local = localTime(instant, zone);
|
|
221
288
|
if (local === void 0) continue;
|
|
222
|
-
if (wallOf(local.year, local.month, local.day, local.hour, local.minute, local.second) ===
|
|
289
|
+
if (wallOf(local.year, local.month, local.day, local.hour, local.minute, local.second) === whole && !found.includes(instant)) found.push(instant);
|
|
223
290
|
}
|
|
224
|
-
return found;
|
|
225
|
-
}
|
|
291
|
+
return found.sort((a, b) => a - b).map((instant) => instant + (wall - whole));
|
|
292
|
+
}
|
|
226
293
|
/**
|
|
227
|
-
*
|
|
228
|
-
* millisecond.
|
|
294
|
+
* This function gives the instants in `(from, to]` at which the offset of `zone` changes, in order
|
|
295
|
+
* and exact to the millisecond. At each instant, the new offset starts. The search reads the offset
|
|
296
|
+
* each 6 hours of the range, thus a long range costs more reads. No zone changes its offset two
|
|
297
|
+
* times in 6 hours. A name that is not a time zone, or a bound that is not a date, gives `undefined`.
|
|
229
298
|
*/
|
|
299
|
+
function offsetChanges(from, to, zone) {
|
|
300
|
+
if (localTime(from, zone) === void 0 || localTime(to, zone) === void 0) return void 0;
|
|
301
|
+
return changesOf(from, to, zone);
|
|
302
|
+
}
|
|
303
|
+
/** The instants in (from, to] at which the offset of the zone changes. `offsetChanges` tells the rule. */
|
|
230
304
|
const changesOf = (from, to, zone) => {
|
|
231
305
|
const offsetAt = (instant) => localTime(instant, zone)?.offset;
|
|
232
306
|
const found = [];
|
|
@@ -251,6 +325,45 @@ const changesOf = (from, to, zone) => {
|
|
|
251
325
|
return found;
|
|
252
326
|
};
|
|
253
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
|
+
/**
|
|
254
367
|
* This function gives the next change of the window after `instant`: the first later instant at
|
|
255
368
|
* which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`
|
|
256
369
|
* for a bad window, and when the window does not change in the 9 days after `instant`. A window
|
|
@@ -264,7 +377,7 @@ function nextBoundary(instant, window) {
|
|
|
264
377
|
const horizon = instant + 9 * DAY_MS;
|
|
265
378
|
const candidates = changesOf(instant, horizon, window.zone);
|
|
266
379
|
const midnight = wallOf(local.year, local.month, local.day, 0, 0, 0);
|
|
267
|
-
for (let day = -1; day <= 10; day += 1) for (const minutes of [valid.start, valid.end]) candidates.push(...instantsOf(midnight + day * DAY_MS + minutes * MINUTE_MS, window.zone));
|
|
380
|
+
for (let day = -1; day <= 10; day += 1) for (const minutes of [valid.start, valid.end]) candidates.push(...instantsOf(midnight + day * DAY_MS + minutes * MINUTE_MS, window.zone) ?? []);
|
|
268
381
|
candidates.sort((a, b) => a - b);
|
|
269
382
|
for (const at of candidates) {
|
|
270
383
|
if (at <= instant || at > horizon) continue;
|
|
@@ -285,6 +398,6 @@ function windowProblems(window) {
|
|
|
285
398
|
if (window.days !== void 0 && window.days.length === 0) problems.push("The window has no day, thus it does not open.");
|
|
286
399
|
return problems;
|
|
287
400
|
}
|
|
288
|
-
export { fakeClock, inWindow, localTime, nextBoundary, platformClock, realClock, windowProblems };
|
|
401
|
+
export { delay, fakeClock, fakeWallClock, firstInstantOf, inWindow, instantsOf, localTime, nextBoundary, offsetChanges, platformClock, realClock, wallClock, windowProblems };
|
|
289
402
|
|
|
290
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/** 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 * The instants at which the wall clock of the zone shows `wall`, a wall-clock time as an instant in\n * UTC. A gap gives none, and an overlap gives two.\n */\nconst instantsOf = (wall: number, zone: string): number[] => {\n const offsets = new Set<number>();\n for (const probe of [wall - DAY_MS, wall, wall + DAY_MS]) {\n const local = localTime(probe, zone);\n if (local !== undefined) offsets.add(local.offset);\n }\n const found: number[] = [];\n for (const offset of offsets) {\n const instant = wall - 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 === wall && !found.includes(instant)) found.push(instant);\n }\n return found;\n};\n\n/**\n * The instants in (from, to] at which the offset of the zone changes, each one exact to the\n * millisecond. The search reads the offset each 6 hours. No zone changes its offset two times in 6 hours.\n */\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;;AAgB7E,MAAM,SAAS,YAA2B;CACxC,KAAK,IAAI,OAAO,GAAG,OAAO,IAAI,QAAQ,GAAG,MAAM,QAAQ,QAAQ;AACjE;;AAGA,MAAa,aAAa,QAAQ,MAAiB;CACjD,IAAI,MAAM;CACV,IAAI,WAAW;CACf,MAAM,SAA+E,CAAC;CACtF,MAAM,OAAO,QAAgB;EAC3B,IAAI;EACJ,KAAK,MAAM,SAAS,QAClB,IAAI,MAAM,QAAQ,MAAM,MAAM,QAAQ,SAAS,KAAA,KAAa,MAAM,KAAK,KAAK,MAAO,MAA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,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,cA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AI,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"}
|
package/build/index.d.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",
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This function stops a timer that `Clock.after` started. A second use does nothing. A clock of
|
|
3
|
-
* the platform also gives `unref`: after it, the timer does not keep a Node process alive.
|
|
4
|
-
*/
|
|
5
|
-
type Cancel = (() => void) & {
|
|
6
|
-
readonly unref?: (() => void) | undefined;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* A clock gives the monotonic time in milliseconds, and it starts a function after a delay. The
|
|
10
|
-
* runtime reads time only through a clock. Thus a test can give a fake clock, and the timing of
|
|
11
|
-
* the runtime is then exact and repeatable.
|
|
12
|
-
*/
|
|
13
|
-
interface Clock {
|
|
14
|
-
/** This method gives the monotonic time, in milliseconds. A step of the wall clock does not move it. */
|
|
15
|
-
now(): number;
|
|
16
|
-
/** This method starts `run` after `ms` milliseconds, and gives the function that stops it. */
|
|
17
|
-
after(ms: number, run: () => void): Cancel;
|
|
18
|
-
}
|
|
19
|
-
/** These options make a clock of the platform. */
|
|
20
|
-
interface PlatformClockOptions {
|
|
21
|
-
/** True makes each timer not keep a Node process alive. The default is false, as with `setTimeout`. */
|
|
22
|
-
readonly unref?: boolean | undefined;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* This function makes a clock of the platform: `performance.now()` and `setTimeout`. Each cancel
|
|
26
|
-
* that it gives also has `unref`. A delay longer than `setTimeout` takes waits in steps.
|
|
27
|
-
*/
|
|
28
|
-
declare const platformClock: (options?: PlatformClockOptions) => Clock;
|
|
29
|
-
/**
|
|
30
|
-
* This clock reads `performance.now()` and uses `setTimeout`. Its timers do not keep a Node
|
|
31
|
-
* process alive, thus a timer that is not stopped cannot stop the exit of the process.
|
|
32
|
-
*/
|
|
33
|
-
declare const realClock: Clock;
|
|
34
|
-
/** A fake clock moves only when a test tells it to. */
|
|
35
|
-
interface FakeClock extends Clock {
|
|
36
|
-
/** This method moves the clock forward by `ms`, and it starts each timer that comes due, in sequence. */
|
|
37
|
-
advance(ms: number): Promise<void>;
|
|
38
|
-
/**
|
|
39
|
-
* This method moves the clock forward by `ms` and starts no timer, as a blocked event loop does.
|
|
40
|
-
* The next `advance` then starts each timer that came due during the block, late.
|
|
41
|
-
*/
|
|
42
|
-
block(ms: number): void;
|
|
43
|
-
/** This count is the number of timers that are not stopped and not called. */
|
|
44
|
-
readonly pending: number;
|
|
45
|
-
}
|
|
46
|
-
/** This function makes a fake clock. Its time starts at `start` milliseconds. */
|
|
47
|
-
declare const fakeClock: (start?: number) => FakeClock;
|
|
48
|
-
/**
|
|
49
|
-
* The wall-clock time of an instant in a time zone. An instant is a time in milliseconds from
|
|
50
|
-
* 1970-01-01T00:00:00Z, as `Date.now()` gives. It is not the monotonic time of `Clock.now()`.
|
|
51
|
-
*/
|
|
52
|
-
interface LocalTime {
|
|
53
|
-
readonly year: number;
|
|
54
|
-
/** The month, from 1 (January) to 12. */
|
|
55
|
-
readonly month: number;
|
|
56
|
-
/** The day of the month, from 1. */
|
|
57
|
-
readonly day: number;
|
|
58
|
-
/** The hour, from 0 to 23. */
|
|
59
|
-
readonly hour: number;
|
|
60
|
-
readonly minute: number;
|
|
61
|
-
readonly second: number;
|
|
62
|
-
/** The day of the week, from 0 (Sunday) to 6 (Saturday). */
|
|
63
|
-
readonly weekday: number;
|
|
64
|
-
/** The offset of the zone from UTC at the instant, in minutes. It is positive east of Greenwich. */
|
|
65
|
-
readonly offset: number;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* A window of local time that occurs on each day, or on some days of the week. The window is
|
|
69
|
-
* data, thus a configuration can hold it.
|
|
70
|
-
*
|
|
71
|
-
* - The start is in the window, and the end is not.
|
|
72
|
-
* - An end before the start crosses midnight. An end equal to the start gives a window of a full day.
|
|
73
|
-
* - In a change of the clock, the window follows the time on the wall. A start or an end in a gap
|
|
74
|
-
* moves to the end of the gap. Thus a window with its start and its end in one gap does not open.
|
|
75
|
-
* - A local time that occurs two times is in the window two times.
|
|
76
|
-
*/
|
|
77
|
-
interface TimeWindow {
|
|
78
|
-
/** The IANA name of the time zone, for example `Europe/Berlin`. */
|
|
79
|
-
readonly zone: string;
|
|
80
|
-
/** The start, a local time `HH:MM` from `00:00` to `23:59`. */
|
|
81
|
-
readonly start: string;
|
|
82
|
-
/** The end, a local time `HH:MM` from `00:00` to `23:59`. */
|
|
83
|
-
readonly end: string;
|
|
84
|
-
/**
|
|
85
|
-
* The days of the week on which the window starts, from 0 (Sunday) to 6. When it is absent, the
|
|
86
|
-
* window starts on each day. A window across midnight continues into the next day.
|
|
87
|
-
*/
|
|
88
|
-
readonly days?: readonly number[] | undefined;
|
|
89
|
-
}
|
|
90
|
-
/** A change of a window: the instant at which it opens or closes. */
|
|
91
|
-
interface WindowBoundary {
|
|
92
|
-
readonly at: number;
|
|
93
|
-
/** True: the window opens at `at`. False: it closes. */
|
|
94
|
-
readonly opens: boolean;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* This function gives the wall-clock time of `instant` in `zone`. It gives `undefined` for a name
|
|
98
|
-
* that is not a time zone, and for an instant that is not a date.
|
|
99
|
-
*/
|
|
100
|
-
declare function localTime(instant: number, zone: string): LocalTime | undefined;
|
|
101
|
-
/**
|
|
102
|
-
* This function tells if `instant` is in the window. It does not throw. A bad window or a bad
|
|
103
|
-
* instant gives `false`, and `windowProblems` finds the bad window.
|
|
104
|
-
*/
|
|
105
|
-
declare function inWindow(instant: number, window: TimeWindow): boolean;
|
|
106
|
-
/**
|
|
107
|
-
* This function gives the next change of the window after `instant`: the first later instant at
|
|
108
|
-
* which the window opens or closes. A timer can wait for it in closed form. It gives `undefined`
|
|
109
|
-
* for a bad window, and when the window does not change in the 9 days after `instant`. A window
|
|
110
|
-
* with no day and a full-day window on each day do not change.
|
|
111
|
-
*/
|
|
112
|
-
declare function nextBoundary(instant: number, window: TimeWindow): WindowBoundary | undefined;
|
|
113
|
-
/** This function gives the problems of a window. With a problem, `inWindow` gives `false` and `nextBoundary` gives `undefined`. */
|
|
114
|
-
declare function windowProblems(window: TimeWindow): string[];
|
|
115
|
-
export { PlatformClockOptions as a, fakeClock as c, nextBoundary as d, platformClock as f, LocalTime as i, inWindow as l, windowProblems as m, Clock as n, TimeWindow as o, realClock as p, FakeClock as r, WindowBoundary as s, Cancel as t, localTime as u };
|