@moltenship/calendar-temporal 0.0.0-stage → 1.0.0-rc.2
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/LICENSE +21 -0
- package/README.md +38 -2
- package/dist/index.d.mts +345 -0
- package/dist/index.mjs +679 -0
- package/package.json +39 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Timofey Maximov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @moltenship/calendar-temporal
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The date math of Moltenship Time: half-open ranges, month and week grids, rounding and snapping,
|
|
4
|
+
day boundaries, time slots and DST handling. Dates and times go in and out as native Temporal
|
|
5
|
+
values. Apart from `createSystemClock`, nothing reads the clock, and nothing reads the host time
|
|
6
|
+
zone.
|
|
7
|
+
|
|
8
|
+
`@moltenship/calendar` re-exports all of it, so apps that use the calendar import these functions
|
|
9
|
+
from there. The package on its own has no dependencies and no React: install it directly when you
|
|
10
|
+
only need the date and time zone helpers.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { plainDateInterval, plainDateIntervalContains } from "@moltenship/calendar-temporal";
|
|
14
|
+
|
|
15
|
+
const october = plainDateInterval(
|
|
16
|
+
Temporal.PlainDate.from("2026-10-01"),
|
|
17
|
+
Temporal.PlainDate.from("2026-11-01"),
|
|
18
|
+
);
|
|
19
|
+
plainDateIntervalContains(october, Temporal.PlainDate.from("2026-11-01")); // false
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm add @moltenship/calendar-temporal
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- A runtime with `Temporal` (Node.js 26 and later include it), or a polyfill that defines
|
|
29
|
+
`globalThis.Temporal`, loaded first.
|
|
30
|
+
- TypeScript 6 or later with `"lib": ["ESNext"]`, which includes the `Temporal` types.
|
|
31
|
+
|
|
32
|
+
## Documentation
|
|
33
|
+
|
|
34
|
+
- [Temporal helpers](https://time.molten.sh/docs/api/temporal)
|
|
35
|
+
- [Repository](https://github.com/Moltenship/time)
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
//#region src/intervals/half-open.d.ts
|
|
2
|
+
declare const validatedHalfOpenInterval: unique symbol;
|
|
3
|
+
type HalfOpenIntervalComparator<TPoint> = (left: TPoint, right: TPoint) => number;
|
|
4
|
+
type HalfOpenInterval<TPoint, TKind extends string> = Readonly<{
|
|
5
|
+
end: TPoint;
|
|
6
|
+
start: TPoint;
|
|
7
|
+
[validatedHalfOpenInterval]: TKind;
|
|
8
|
+
}>;
|
|
9
|
+
type InstantInterval = HalfOpenInterval<Temporal.Instant, "instant">;
|
|
10
|
+
type PlainDateInterval = HalfOpenInterval<Temporal.PlainDate, "plain-date">;
|
|
11
|
+
type PlainDateTimeInterval = HalfOpenInterval<Temporal.PlainDateTime, "plain-date-time">;
|
|
12
|
+
interface HalfOpenIntervalIssue<TPoint> {
|
|
13
|
+
readonly code: "start-not-before-end";
|
|
14
|
+
readonly end: TPoint;
|
|
15
|
+
readonly start: TPoint;
|
|
16
|
+
}
|
|
17
|
+
type HalfOpenIntervalValidation<TPoint, TKind extends string> = {
|
|
18
|
+
readonly interval: HalfOpenInterval<TPoint, TKind>;
|
|
19
|
+
readonly valid: true;
|
|
20
|
+
} | {
|
|
21
|
+
readonly issue: HalfOpenIntervalIssue<TPoint>;
|
|
22
|
+
readonly valid: false;
|
|
23
|
+
};
|
|
24
|
+
export declare class InvalidHalfOpenIntervalError<TPoint> extends RangeError {
|
|
25
|
+
readonly code = "start-not-before-end";
|
|
26
|
+
readonly end: TPoint;
|
|
27
|
+
readonly start: TPoint;
|
|
28
|
+
constructor(start: TPoint, end: TPoint);
|
|
29
|
+
}
|
|
30
|
+
export declare function validateHalfOpenInterval<TPoint, const TKind extends string>(kind: TKind, start: TPoint, end: TPoint, compare: HalfOpenIntervalComparator<TPoint>): HalfOpenIntervalValidation<TPoint, TKind>;
|
|
31
|
+
export declare function halfOpenInterval<TPoint, const TKind extends string>(kind: TKind, start: TPoint, end: TPoint, compare: HalfOpenIntervalComparator<TPoint>): HalfOpenInterval<TPoint, TKind>;
|
|
32
|
+
export declare function compareInstants(left: Temporal.Instant, right: Temporal.Instant): number;
|
|
33
|
+
export declare function comparePlainDates(left: Temporal.PlainDate, right: Temporal.PlainDate): number;
|
|
34
|
+
export declare function comparePlainDateTimes(left: Temporal.PlainDateTime, right: Temporal.PlainDateTime): number;
|
|
35
|
+
export declare function comparePlainTimes(left: Temporal.PlainTime, right: Temporal.PlainTime): number;
|
|
36
|
+
export declare function instantInterval(start: Temporal.Instant, end: Temporal.Instant): InstantInterval;
|
|
37
|
+
export declare function plainDateInterval(start: Temporal.PlainDate, end: Temporal.PlainDate): PlainDateInterval;
|
|
38
|
+
export declare function plainDateTimeInterval(start: Temporal.PlainDateTime, end: Temporal.PlainDateTime): PlainDateTimeInterval;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/intervals/operations.d.ts
|
|
41
|
+
type HalfOpenIntervalSplit<TInterval> = readonly [interval: TInterval] | readonly [before: TInterval, after: TInterval];
|
|
42
|
+
/** Tests whether an instant belongs to an instant interval using `[start, end)` semantics. */
|
|
43
|
+
export declare function instantIntervalContains(interval: InstantInterval, instant: Temporal.Instant): boolean;
|
|
44
|
+
/** Tests whether a date belongs to a date interval using `[start, end)` semantics. */
|
|
45
|
+
export declare function plainDateIntervalContains(interval: PlainDateInterval, date: Temporal.PlainDate): boolean;
|
|
46
|
+
/** Tests whether a date-time belongs to a date-time interval using `[start, end)` semantics. */
|
|
47
|
+
export declare function plainDateTimeIntervalContains(interval: PlainDateTimeInterval, dateTime: Temporal.PlainDateTime): boolean;
|
|
48
|
+
export declare function instantIntervalContainsInterval(outer: InstantInterval, inner: InstantInterval): boolean;
|
|
49
|
+
export declare function plainDateIntervalContainsInterval(outer: PlainDateInterval, inner: PlainDateInterval): boolean;
|
|
50
|
+
export declare function plainDateTimeIntervalContainsInterval(outer: PlainDateTimeInterval, inner: PlainDateTimeInterval): boolean;
|
|
51
|
+
export declare function instantIntervalsIntersect(left: InstantInterval, right: InstantInterval): boolean;
|
|
52
|
+
export declare function plainDateIntervalsIntersect(left: PlainDateInterval, right: PlainDateInterval): boolean;
|
|
53
|
+
export declare function plainDateTimeIntervalsIntersect(left: PlainDateTimeInterval, right: PlainDateTimeInterval): boolean;
|
|
54
|
+
export declare function intersectInstantIntervals(left: InstantInterval, right: InstantInterval): InstantInterval | null;
|
|
55
|
+
export declare function intersectPlainDateIntervals(left: PlainDateInterval, right: PlainDateInterval): PlainDateInterval | null;
|
|
56
|
+
export declare function intersectPlainDateTimeIntervals(left: PlainDateTimeInterval, right: PlainDateTimeInterval): PlainDateTimeInterval | null;
|
|
57
|
+
/** Splits only at an interior instant; endpoints and out-of-range points preserve the input. */
|
|
58
|
+
export declare function splitInstantInterval(interval: InstantInterval, splitAt: Temporal.Instant): HalfOpenIntervalSplit<InstantInterval>;
|
|
59
|
+
/** Splits only at an interior date; endpoints and out-of-range dates preserve the input. */
|
|
60
|
+
export declare function splitPlainDateInterval(interval: PlainDateInterval, splitAt: Temporal.PlainDate): HalfOpenIntervalSplit<PlainDateInterval>;
|
|
61
|
+
/** Splits only at an interior date-time; endpoints and out-of-range points preserve the input. */
|
|
62
|
+
export declare function splitPlainDateTimeInterval(interval: PlainDateTimeInterval, splitAt: Temporal.PlainDateTime): HalfOpenIntervalSplit<PlainDateTimeInterval>;
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/plain-date/iteration.d.ts
|
|
65
|
+
export declare const DEFAULT_PLAIN_DATE_ITERATION_LIMIT = 10000;
|
|
66
|
+
interface PlainDateIterationOptions {
|
|
67
|
+
/** Maximum values produced before rejecting an unexpectedly large traversal. */
|
|
68
|
+
readonly limit?: number;
|
|
69
|
+
/** Positive whole-day stride. Defaults to one day. */
|
|
70
|
+
readonly stepDays?: number;
|
|
71
|
+
}
|
|
72
|
+
/** Lazily visits dates in a half-open interval and never reads the host timezone. */
|
|
73
|
+
export declare function iteratePlainDates(range: PlainDateInterval, options?: PlainDateIterationOptions): Generator<Temporal.PlainDate, void, undefined>;
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/grids/calendar-grid.d.ts
|
|
76
|
+
type WeekStartsOn = 1 | 2 | 3 | 4 | 5 | 6 | 7;
|
|
77
|
+
interface WeekRangeOptions {
|
|
78
|
+
/** ISO day number: Monday is 1 and Sunday is 7. */
|
|
79
|
+
readonly weekStartsOn: WeekStartsOn;
|
|
80
|
+
}
|
|
81
|
+
interface MonthGridOptions extends WeekRangeOptions {
|
|
82
|
+
/** Pads the grid to six weeks instead of returning its natural four-to-six-week range. */
|
|
83
|
+
readonly fixedWeeks?: boolean;
|
|
84
|
+
}
|
|
85
|
+
interface MonthGrid {
|
|
86
|
+
/** First day of the anchored month. */
|
|
87
|
+
readonly month: Temporal.PlainDate;
|
|
88
|
+
/** Complete visible half-open range, including leading and trailing dates. */
|
|
89
|
+
readonly range: PlainDateInterval;
|
|
90
|
+
/** Consecutive seven-day half-open rows covering `range`. */
|
|
91
|
+
readonly weeks: readonly PlainDateInterval[];
|
|
92
|
+
}
|
|
93
|
+
/** Returns the seven-day half-open week containing `date`. */
|
|
94
|
+
export declare function weekRange(date: Temporal.PlainDate, options: WeekRangeOptions): PlainDateInterval;
|
|
95
|
+
/** Builds a bounded month grid without locale, timezone, or current-time globals. */
|
|
96
|
+
export declare function monthGrid(date: Temporal.PlainDate, options: MonthGridOptions): MonthGrid;
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/plain-time/rounding.d.ts
|
|
99
|
+
type PlainTimeRoundingMode = NonNullable<Temporal.RoundingOptions<Temporal.TimeUnit>["roundingMode"]>;
|
|
100
|
+
type WallTimeSnapMode = "ceil" | "floor" | "nearest";
|
|
101
|
+
interface PlainTimeRoundingOptions {
|
|
102
|
+
readonly increment?: number;
|
|
103
|
+
readonly roundingMode?: PlainTimeRoundingMode;
|
|
104
|
+
readonly smallestUnit: Temporal.TimeUnit;
|
|
105
|
+
}
|
|
106
|
+
/** A midnight-anchored wall-clock grid: `increment` must divide the next larger unit. */
|
|
107
|
+
interface WallTimeSnapOptions {
|
|
108
|
+
readonly increment: number;
|
|
109
|
+
readonly mode?: WallTimeSnapMode;
|
|
110
|
+
readonly smallestUnit: Temporal.TimeUnit;
|
|
111
|
+
}
|
|
112
|
+
interface SnappedPlainTime {
|
|
113
|
+
/** `1` when rounding up crossed midnight, so `time` is `00:00` on the following day. */
|
|
114
|
+
readonly days: 0 | 1;
|
|
115
|
+
readonly time: Temporal.PlainTime;
|
|
116
|
+
}
|
|
117
|
+
/** Rounds a wall-clock time through the native Temporal operation, wrapping at midnight. */
|
|
118
|
+
export declare function roundPlainTime(time: Temporal.PlainTime, options: PlainTimeRoundingOptions): Temporal.PlainTime;
|
|
119
|
+
/** Snaps a wall-clock time to a midnight-anchored grid and reports a carry into the next day. */
|
|
120
|
+
export declare function snapPlainTime(time: Temporal.PlainTime, options: WallTimeSnapOptions): SnappedPlainTime;
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/instant/iteration.d.ts
|
|
123
|
+
export declare const DEFAULT_INSTANT_ITERATION_LIMIT = 10000;
|
|
124
|
+
interface InstantIterationOptions {
|
|
125
|
+
/** Maximum values produced before rejecting an unexpectedly large traversal. */
|
|
126
|
+
readonly limit?: number;
|
|
127
|
+
/** Positive, time-only Temporal duration used to advance each grid point. */
|
|
128
|
+
readonly step: Temporal.DurationLike;
|
|
129
|
+
}
|
|
130
|
+
/** Lazily visits instant grid points in `[start, end)` with an explicit positive step. */
|
|
131
|
+
export declare function iterateInstants(range: InstantInterval, options: InstantIterationOptions): Generator<Temporal.Instant, void, undefined>;
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/instant/rounding.d.ts
|
|
134
|
+
type InstantRoundingMode = NonNullable<Temporal.RoundingOptions<Temporal.TimeUnit>["roundingMode"]>;
|
|
135
|
+
interface InstantRoundingOptions {
|
|
136
|
+
readonly increment?: number;
|
|
137
|
+
readonly roundingMode?: InstantRoundingMode;
|
|
138
|
+
readonly smallestUnit: Temporal.TimeUnit;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Rounds an absolute instant on the Unix-epoch grid through the native Temporal operation.
|
|
142
|
+
* Calendar grids are wall-clock grids: use `snapZonedDateTime` for those.
|
|
143
|
+
*/
|
|
144
|
+
export declare function roundInstant(instant: Temporal.Instant, options: InstantRoundingOptions): Temporal.Instant;
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/clock/calendar-clock.d.ts
|
|
147
|
+
interface CalendarClock {
|
|
148
|
+
instant(): Temporal.Instant;
|
|
149
|
+
}
|
|
150
|
+
export declare function createFixedClock(instant: Temporal.Instant): CalendarClock;
|
|
151
|
+
export declare function createSystemClock(): CalendarClock;
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/zoned/machine-keys.d.ts
|
|
154
|
+
/** Stable absolute identity for maps, caches, and render models. */
|
|
155
|
+
export declare function instantKey(instant: Temporal.Instant): string;
|
|
156
|
+
/** Stable calendar-date identity; ISO dates have no calendar annotation. */
|
|
157
|
+
export declare function plainDateKey(date: Temporal.PlainDate): string;
|
|
158
|
+
/** Stable local date-time identity with nanosecond precision. */
|
|
159
|
+
export declare function plainDateTimeKey(dateTime: Temporal.PlainDateTime): string;
|
|
160
|
+
/** Stable identity for one nominal date in one explicit timezone. */
|
|
161
|
+
export declare function zonedDayKey(date: Temporal.PlainDate, timeZone: string): string;
|
|
162
|
+
/** Stable wall-coordinate identity. It deliberately does not identify a fold occurrence. */
|
|
163
|
+
export declare function zonedWallTimeKey(dateTime: Temporal.PlainDateTime, timeZone: string): string;
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/zoned/projection.d.ts
|
|
166
|
+
type TemporalDisambiguation = NonNullable<Temporal.DisambiguationOptions["disambiguation"]>;
|
|
167
|
+
interface ZonedTimeEnvironment {
|
|
168
|
+
readonly disambiguation: TemporalDisambiguation;
|
|
169
|
+
readonly timeZone: string;
|
|
170
|
+
}
|
|
171
|
+
type ZonedWallTimeKind = "fold" | "gap" | "unique";
|
|
172
|
+
type ZonedWallTimeSelection = "earlier" | "later" | "unique";
|
|
173
|
+
interface ZonedWallTimeOccurrence {
|
|
174
|
+
readonly instant: Temporal.Instant;
|
|
175
|
+
readonly key: string;
|
|
176
|
+
readonly offset: string;
|
|
177
|
+
readonly offsetNanoseconds: number;
|
|
178
|
+
readonly wallTime: Temporal.PlainDateTime;
|
|
179
|
+
readonly zonedDateTime: Temporal.ZonedDateTime;
|
|
180
|
+
}
|
|
181
|
+
interface ZonedWallTimeAdjustment {
|
|
182
|
+
readonly direction: "earlier" | "later";
|
|
183
|
+
readonly requestedWallTime: Temporal.PlainDateTime;
|
|
184
|
+
readonly resolvedWallTime: Temporal.PlainDateTime;
|
|
185
|
+
readonly wallTimeDelta: Temporal.Duration;
|
|
186
|
+
}
|
|
187
|
+
interface ZonedWallTimeProjectionBase {
|
|
188
|
+
readonly disambiguation: TemporalDisambiguation;
|
|
189
|
+
readonly key: string;
|
|
190
|
+
readonly kind: ZonedWallTimeKind;
|
|
191
|
+
readonly occurrences: readonly ZonedWallTimeOccurrence[];
|
|
192
|
+
readonly requestedWallTime: Temporal.PlainDateTime;
|
|
193
|
+
readonly timeZone: string;
|
|
194
|
+
}
|
|
195
|
+
interface ResolvedZonedWallTimeProjection extends ZonedWallTimeProjectionBase {
|
|
196
|
+
readonly adjustment: ZonedWallTimeAdjustment | null;
|
|
197
|
+
readonly selected: ZonedWallTimeOccurrence;
|
|
198
|
+
readonly selectedBy: ZonedWallTimeSelection;
|
|
199
|
+
readonly status: "resolved";
|
|
200
|
+
}
|
|
201
|
+
interface RejectedZonedWallTimeProjection extends ZonedWallTimeProjectionBase {
|
|
202
|
+
readonly adjustment: null;
|
|
203
|
+
readonly kind: "fold" | "gap";
|
|
204
|
+
readonly selected: null;
|
|
205
|
+
readonly selectedBy: null;
|
|
206
|
+
readonly status: "rejected";
|
|
207
|
+
}
|
|
208
|
+
type ZonedWallTimeProjection = RejectedZonedWallTimeProjection | ResolvedZonedWallTimeProjection;
|
|
209
|
+
interface ZonedInstantProjection {
|
|
210
|
+
readonly instant: Temporal.Instant;
|
|
211
|
+
readonly key: string;
|
|
212
|
+
readonly offset: string;
|
|
213
|
+
readonly offsetNanoseconds: number;
|
|
214
|
+
readonly timeZone: string;
|
|
215
|
+
readonly wallTime: Temporal.PlainDateTime;
|
|
216
|
+
readonly wallTimeKey: string;
|
|
217
|
+
readonly zonedDateTime: Temporal.ZonedDateTime;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Projects a nominal wall coordinate through an explicit timezone and policy.
|
|
221
|
+
* Gaps retain the requested and shifted wall times; folds retain both instant identities.
|
|
222
|
+
*/
|
|
223
|
+
export declare function resolveZonedWallTime(requestedWallTime: Temporal.PlainDateTime, environment: ZonedTimeEnvironment): ZonedWallTimeProjection;
|
|
224
|
+
/** Projects an absolute instant while retaining the fields needed for stable rendering and editing. */
|
|
225
|
+
export declare function projectInstantToZonedTime(instant: Temporal.Instant, timeZone: string): ZonedInstantProjection;
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/zoned/rounding.d.ts
|
|
228
|
+
interface ZonedWallTimeSnapOptions extends WallTimeSnapOptions {
|
|
229
|
+
/** Occurrence chosen when the snapped wall time is repeated (fall-back). Defaults to `"earlier"`. */
|
|
230
|
+
readonly fold?: "earlier" | "later";
|
|
231
|
+
readonly timeZone: string;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Snaps a zoned value to a midnight-anchored wall-clock grid in its own timezone.
|
|
235
|
+
*
|
|
236
|
+
* A snapped wall time inside a fold keeps the input's offset, so a value in the repeated hour stays
|
|
237
|
+
* in the same occurrence. A snapped wall time inside a gap resolves to the transition instant, the
|
|
238
|
+
* only real instant at that wall position. `floor` therefore never moves later and `ceil` never
|
|
239
|
+
* moves earlier, even on DST days.
|
|
240
|
+
*/
|
|
241
|
+
export declare function snapZonedDateTime(zonedDateTime: Temporal.ZonedDateTime, options: WallTimeSnapOptions): Temporal.ZonedDateTime;
|
|
242
|
+
/**
|
|
243
|
+
* Snaps a nominal wall time, such as a pointer position, to a midnight-anchored grid and only then
|
|
244
|
+
* resolves it in `timeZone`: a repeated wall time picks the `fold` occurrence and a skipped one
|
|
245
|
+
* resolves to the transition instant.
|
|
246
|
+
*/
|
|
247
|
+
export declare function snapZonedWallTime(wallTime: Temporal.PlainDateTime, options: ZonedWallTimeSnapOptions): Temporal.ZonedDateTime;
|
|
248
|
+
//#endregion
|
|
249
|
+
//#region src/zoned/day.d.ts
|
|
250
|
+
export declare const DEFAULT_ZONED_DAY_SPLIT_LIMIT = 10000;
|
|
251
|
+
interface ZonedDayRangeBase {
|
|
252
|
+
readonly date: Temporal.PlainDate;
|
|
253
|
+
readonly duration: Temporal.Duration;
|
|
254
|
+
readonly durationNanoseconds: bigint;
|
|
255
|
+
readonly end: Temporal.Instant;
|
|
256
|
+
readonly endBoundary: Temporal.ZonedDateTime;
|
|
257
|
+
readonly key: string;
|
|
258
|
+
readonly start: Temporal.Instant;
|
|
259
|
+
readonly startBoundary: Temporal.ZonedDateTime;
|
|
260
|
+
readonly timeZone: string;
|
|
261
|
+
}
|
|
262
|
+
interface AvailableZonedDayRange extends ZonedDayRangeBase {
|
|
263
|
+
readonly interval: InstantInterval;
|
|
264
|
+
readonly kind: "available";
|
|
265
|
+
}
|
|
266
|
+
interface SkippedZonedDayRange extends ZonedDayRangeBase {
|
|
267
|
+
readonly interval: null;
|
|
268
|
+
readonly kind: "skipped";
|
|
269
|
+
}
|
|
270
|
+
type ZonedDayRange = AvailableZonedDayRange | SkippedZonedDayRange;
|
|
271
|
+
interface ZonedOffsetTransition {
|
|
272
|
+
readonly affectedWallRange: PlainDateTimeInterval;
|
|
273
|
+
readonly instant: Temporal.Instant;
|
|
274
|
+
readonly key: string;
|
|
275
|
+
readonly kind: "fold" | "gap";
|
|
276
|
+
readonly offsetAfter: string;
|
|
277
|
+
readonly offsetBefore: string;
|
|
278
|
+
readonly offsetDeltaNanoseconds: number;
|
|
279
|
+
readonly timeZone: string;
|
|
280
|
+
}
|
|
281
|
+
interface ZonedDaySplit {
|
|
282
|
+
readonly continuesAfterDay: boolean;
|
|
283
|
+
readonly continuesBeforeDay: boolean;
|
|
284
|
+
readonly date: Temporal.PlainDate;
|
|
285
|
+
readonly day: AvailableZonedDayRange;
|
|
286
|
+
readonly interval: InstantInterval;
|
|
287
|
+
readonly key: string;
|
|
288
|
+
readonly timeZone: string;
|
|
289
|
+
}
|
|
290
|
+
interface SplitInstantRangeByZonedDayOptions {
|
|
291
|
+
readonly limit?: number;
|
|
292
|
+
}
|
|
293
|
+
/** Returns the actual instant extent of one nominal date in an explicit timezone. */
|
|
294
|
+
export declare function zonedDayRange(date: Temporal.PlainDate, timeZone: string): ZonedDayRange;
|
|
295
|
+
/** Returns timezone offset transitions whose affected wall interval intersects the nominal date. */
|
|
296
|
+
export declare function zonedDayTransitions(date: Temporal.PlainDate, timeZone: string): readonly ZonedOffsetTransition[];
|
|
297
|
+
/** Splits an absolute interval at actual zoned-day boundaries without inventing skipped dates. */
|
|
298
|
+
export declare function splitInstantRangeByZonedDay(range: InstantInterval, timeZone: string, options?: SplitInstantRangeByZonedDayOptions): readonly ZonedDaySplit[];
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/zoned/slots.d.ts
|
|
301
|
+
export declare const DEFAULT_ZONED_SLOT_LIMIT = 10000;
|
|
302
|
+
interface ZonedDaySlotsOptions extends ZonedTimeEnvironment {
|
|
303
|
+
readonly limit?: number;
|
|
304
|
+
readonly slotDuration: Temporal.DurationLike;
|
|
305
|
+
}
|
|
306
|
+
interface ZonedDaySlotOccurrence {
|
|
307
|
+
readonly interval: InstantInterval;
|
|
308
|
+
readonly key: string;
|
|
309
|
+
readonly start: ZonedWallTimeOccurrence;
|
|
310
|
+
}
|
|
311
|
+
interface ZonedDaySlotSegment {
|
|
312
|
+
readonly key: string;
|
|
313
|
+
readonly kind: ZonedWallTimeKind;
|
|
314
|
+
readonly occurrences: readonly ZonedDaySlotOccurrence[];
|
|
315
|
+
readonly physicalDurationNanoseconds: bigint;
|
|
316
|
+
readonly projection: ZonedWallTimeProjection;
|
|
317
|
+
readonly wallRange: PlainDateTimeInterval;
|
|
318
|
+
}
|
|
319
|
+
interface ZonedDaySlot {
|
|
320
|
+
readonly index: number;
|
|
321
|
+
readonly key: string;
|
|
322
|
+
readonly kind: ZonedWallTimeKind | "transition";
|
|
323
|
+
readonly physicalDurationNanoseconds: bigint;
|
|
324
|
+
readonly segments: readonly ZonedDaySlotSegment[];
|
|
325
|
+
readonly wallRange: PlainDateTimeInterval;
|
|
326
|
+
}
|
|
327
|
+
interface ZonedDaySlots {
|
|
328
|
+
/** Physical pieces owned by the boundary day whose projected wall date is adjacent to `day.date`. */
|
|
329
|
+
readonly carryoverOccurrences: readonly ZonedDaySlotOccurrence[];
|
|
330
|
+
readonly day: ZonedDayRange;
|
|
331
|
+
readonly disambiguation: ZonedTimeEnvironment["disambiguation"];
|
|
332
|
+
readonly physicalDurationNanoseconds: bigint;
|
|
333
|
+
readonly slotDuration: Temporal.Duration;
|
|
334
|
+
readonly slotDurationNanoseconds: bigint;
|
|
335
|
+
readonly slots: readonly ZonedDaySlot[];
|
|
336
|
+
readonly timeZone: string;
|
|
337
|
+
readonly transitions: readonly ZonedOffsetTransition[];
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Builds a nominal wall-clock grid. Gaps have zero occurrences, folds have two, and a timezone
|
|
341
|
+
* transition inside a configured slot produces explicit sub-segments.
|
|
342
|
+
*/
|
|
343
|
+
export declare function slotsInZonedDay(date: Temporal.PlainDate, options: ZonedDaySlotsOptions): ZonedDaySlots;
|
|
344
|
+
//#endregion
|
|
345
|
+
export type { AvailableZonedDayRange, CalendarClock, HalfOpenInterval, HalfOpenIntervalComparator, HalfOpenIntervalIssue, HalfOpenIntervalSplit, HalfOpenIntervalValidation, InstantInterval, InstantIterationOptions, InstantRoundingMode, InstantRoundingOptions, MonthGrid, MonthGridOptions, PlainDateInterval, PlainDateIterationOptions, PlainDateTimeInterval, PlainTimeRoundingMode, PlainTimeRoundingOptions, RejectedZonedWallTimeProjection, ResolvedZonedWallTimeProjection, SkippedZonedDayRange, SnappedPlainTime, SplitInstantRangeByZonedDayOptions, TemporalDisambiguation, WallTimeSnapMode, WallTimeSnapOptions, WeekRangeOptions, WeekStartsOn, ZonedDayRange, ZonedDaySlot, ZonedDaySlotOccurrence, ZonedDaySlotSegment, ZonedDaySlots, ZonedDaySlotsOptions, ZonedDaySplit, ZonedInstantProjection, ZonedOffsetTransition, ZonedTimeEnvironment, ZonedWallTimeAdjustment, ZonedWallTimeKind, ZonedWallTimeOccurrence, ZonedWallTimeProjection, ZonedWallTimeSelection, ZonedWallTimeSnapOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
//#region src/intervals/half-open.ts
|
|
2
|
+
const validatedHalfOpenInterval = Symbol("validated-half-open-interval");
|
|
3
|
+
var InvalidHalfOpenIntervalError = class extends RangeError {
|
|
4
|
+
code = "start-not-before-end";
|
|
5
|
+
end;
|
|
6
|
+
start;
|
|
7
|
+
constructor(start, end) {
|
|
8
|
+
super("start-not-before-end");
|
|
9
|
+
this.name = "InvalidHalfOpenIntervalError";
|
|
10
|
+
this.start = start;
|
|
11
|
+
this.end = end;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
function validateHalfOpenInterval(kind, start, end, compare) {
|
|
15
|
+
if (!(compare(start, end) < 0)) return {
|
|
16
|
+
valid: false,
|
|
17
|
+
issue: {
|
|
18
|
+
code: "start-not-before-end",
|
|
19
|
+
start,
|
|
20
|
+
end
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
valid: true,
|
|
25
|
+
interval: Object.freeze({
|
|
26
|
+
start,
|
|
27
|
+
end,
|
|
28
|
+
[validatedHalfOpenInterval]: kind
|
|
29
|
+
})
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function halfOpenInterval(kind, start, end, compare) {
|
|
33
|
+
const validation = validateHalfOpenInterval(kind, start, end, compare);
|
|
34
|
+
if (!validation.valid) throw new InvalidHalfOpenIntervalError(start, end);
|
|
35
|
+
return validation.interval;
|
|
36
|
+
}
|
|
37
|
+
function compareInstants(left, right) {
|
|
38
|
+
return globalThis.Temporal.Instant.compare(left, right);
|
|
39
|
+
}
|
|
40
|
+
function comparePlainDates(left, right) {
|
|
41
|
+
return globalThis.Temporal.PlainDate.compare(left, right);
|
|
42
|
+
}
|
|
43
|
+
function comparePlainDateTimes(left, right) {
|
|
44
|
+
return globalThis.Temporal.PlainDateTime.compare(left, right);
|
|
45
|
+
}
|
|
46
|
+
function comparePlainTimes(left, right) {
|
|
47
|
+
return globalThis.Temporal.PlainTime.compare(left, right);
|
|
48
|
+
}
|
|
49
|
+
function instantInterval(start, end) {
|
|
50
|
+
return halfOpenInterval("instant", start, end, compareInstants);
|
|
51
|
+
}
|
|
52
|
+
function plainDateInterval(start, end) {
|
|
53
|
+
return halfOpenInterval("plain-date", start, end, comparePlainDates);
|
|
54
|
+
}
|
|
55
|
+
function plainDateTimeInterval(start, end) {
|
|
56
|
+
return halfOpenInterval("plain-date-time", start, end, comparePlainDateTimes);
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/intervals/operations.ts
|
|
60
|
+
function intervalContainsPoint(interval, point, compare) {
|
|
61
|
+
return compare(interval.start, point) <= 0 && compare(point, interval.end) < 0;
|
|
62
|
+
}
|
|
63
|
+
function intervalContainsInterval(outer, inner, compare) {
|
|
64
|
+
return compare(outer.start, inner.start) <= 0 && compare(inner.end, outer.end) <= 0;
|
|
65
|
+
}
|
|
66
|
+
function intervalsIntersect(left, right, compare) {
|
|
67
|
+
return compare(left.start, right.end) < 0 && compare(left.end, right.start) > 0;
|
|
68
|
+
}
|
|
69
|
+
function intersectIntervals(left, right, compare, construct) {
|
|
70
|
+
const start = compare(left.start, right.start) >= 0 ? left.start : right.start;
|
|
71
|
+
const end = compare(left.end, right.end) <= 0 ? left.end : right.end;
|
|
72
|
+
return compare(start, end) < 0 ? construct(start, end) : null;
|
|
73
|
+
}
|
|
74
|
+
function splitInterval(interval, splitAt, compare, construct) {
|
|
75
|
+
if (compare(splitAt, interval.start) <= 0 || compare(splitAt, interval.end) >= 0) return Object.freeze([interval]);
|
|
76
|
+
return Object.freeze([construct(interval.start, splitAt), construct(splitAt, interval.end)]);
|
|
77
|
+
}
|
|
78
|
+
/** Tests whether an instant belongs to an instant interval using `[start, end)` semantics. */
|
|
79
|
+
function instantIntervalContains(interval, instant) {
|
|
80
|
+
return intervalContainsPoint(interval, instant, compareInstants);
|
|
81
|
+
}
|
|
82
|
+
/** Tests whether a date belongs to a date interval using `[start, end)` semantics. */
|
|
83
|
+
function plainDateIntervalContains(interval, date) {
|
|
84
|
+
return intervalContainsPoint(interval, date, comparePlainDates);
|
|
85
|
+
}
|
|
86
|
+
/** Tests whether a date-time belongs to a date-time interval using `[start, end)` semantics. */
|
|
87
|
+
function plainDateTimeIntervalContains(interval, dateTime) {
|
|
88
|
+
return intervalContainsPoint(interval, dateTime, comparePlainDateTimes);
|
|
89
|
+
}
|
|
90
|
+
function instantIntervalContainsInterval(outer, inner) {
|
|
91
|
+
return intervalContainsInterval(outer, inner, compareInstants);
|
|
92
|
+
}
|
|
93
|
+
function plainDateIntervalContainsInterval(outer, inner) {
|
|
94
|
+
return intervalContainsInterval(outer, inner, comparePlainDates);
|
|
95
|
+
}
|
|
96
|
+
function plainDateTimeIntervalContainsInterval(outer, inner) {
|
|
97
|
+
return intervalContainsInterval(outer, inner, comparePlainDateTimes);
|
|
98
|
+
}
|
|
99
|
+
function instantIntervalsIntersect(left, right) {
|
|
100
|
+
return intervalsIntersect(left, right, compareInstants);
|
|
101
|
+
}
|
|
102
|
+
function plainDateIntervalsIntersect(left, right) {
|
|
103
|
+
return intervalsIntersect(left, right, comparePlainDates);
|
|
104
|
+
}
|
|
105
|
+
function plainDateTimeIntervalsIntersect(left, right) {
|
|
106
|
+
return intervalsIntersect(left, right, comparePlainDateTimes);
|
|
107
|
+
}
|
|
108
|
+
function intersectInstantIntervals(left, right) {
|
|
109
|
+
return intersectIntervals(left, right, compareInstants, instantInterval);
|
|
110
|
+
}
|
|
111
|
+
function intersectPlainDateIntervals(left, right) {
|
|
112
|
+
return intersectIntervals(left, right, comparePlainDates, plainDateInterval);
|
|
113
|
+
}
|
|
114
|
+
function intersectPlainDateTimeIntervals(left, right) {
|
|
115
|
+
return intersectIntervals(left, right, comparePlainDateTimes, plainDateTimeInterval);
|
|
116
|
+
}
|
|
117
|
+
/** Splits only at an interior instant; endpoints and out-of-range points preserve the input. */
|
|
118
|
+
function splitInstantInterval(interval, splitAt) {
|
|
119
|
+
return splitInterval(interval, splitAt, compareInstants, instantInterval);
|
|
120
|
+
}
|
|
121
|
+
/** Splits only at an interior date; endpoints and out-of-range dates preserve the input. */
|
|
122
|
+
function splitPlainDateInterval(interval, splitAt) {
|
|
123
|
+
return splitInterval(interval, splitAt, comparePlainDates, plainDateInterval);
|
|
124
|
+
}
|
|
125
|
+
/** Splits only at an interior date-time; endpoints and out-of-range points preserve the input. */
|
|
126
|
+
function splitPlainDateTimeInterval(interval, splitAt) {
|
|
127
|
+
return splitInterval(interval, splitAt, comparePlainDateTimes, plainDateTimeInterval);
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/plain-date/iteration.ts
|
|
131
|
+
const DEFAULT_PLAIN_DATE_ITERATION_LIMIT = 1e4;
|
|
132
|
+
function positiveInteger(value, code) {
|
|
133
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new RangeError(code);
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
/** Lazily visits dates in a half-open interval and never reads the host timezone. */
|
|
137
|
+
function* iteratePlainDates(range, options = {}) {
|
|
138
|
+
const limit = positiveInteger(options.limit ?? 1e4, "invalid-plain-date-iteration-limit");
|
|
139
|
+
const stepDays = positiveInteger(options.stepDays ?? 1, "invalid-plain-date-iteration-step");
|
|
140
|
+
let current = range.start;
|
|
141
|
+
let count = 0;
|
|
142
|
+
while (comparePlainDates(current, range.end) < 0) {
|
|
143
|
+
if (count >= limit) throw new RangeError("plain-date-iteration-limit-exceeded");
|
|
144
|
+
yield current;
|
|
145
|
+
count += 1;
|
|
146
|
+
if (stepDays >= current.until(range.end, { largestUnit: "day" }).days) return;
|
|
147
|
+
current = current.add({ days: stepDays });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/grids/calendar-grid.ts
|
|
152
|
+
function assertWeekStartsOn(value) {
|
|
153
|
+
if (!Number.isInteger(value) || value < 1 || value > 7) throw new RangeError("invalid-week-start");
|
|
154
|
+
}
|
|
155
|
+
function startOfWeek(date, weekStartsOn) {
|
|
156
|
+
const daysSinceWeekStart = (date.dayOfWeek - weekStartsOn + 7) % 7;
|
|
157
|
+
return date.subtract({ days: daysSinceWeekStart });
|
|
158
|
+
}
|
|
159
|
+
/** Returns the seven-day half-open week containing `date`. */
|
|
160
|
+
function weekRange(date, options) {
|
|
161
|
+
assertWeekStartsOn(options.weekStartsOn);
|
|
162
|
+
const start = startOfWeek(date, options.weekStartsOn);
|
|
163
|
+
return plainDateInterval(start, start.add({ days: 7 }));
|
|
164
|
+
}
|
|
165
|
+
/** Builds a bounded month grid without locale, timezone, or current-time globals. */
|
|
166
|
+
function monthGrid(date, options) {
|
|
167
|
+
assertWeekStartsOn(options.weekStartsOn);
|
|
168
|
+
const month = date.with({ day: 1 });
|
|
169
|
+
const start = startOfWeek(month, options.weekStartsOn);
|
|
170
|
+
const nextMonth = month.add({ months: 1 });
|
|
171
|
+
const nextMonthWeekStart = startOfWeek(nextMonth, options.weekStartsOn);
|
|
172
|
+
const naturalEnd = comparePlainDates(nextMonthWeekStart, nextMonth) === 0 ? nextMonth : nextMonthWeekStart.add({ days: 7 });
|
|
173
|
+
const end = options.fixedWeeks ? start.add({ days: 42 }) : naturalEnd;
|
|
174
|
+
if (comparePlainDates(naturalEnd, end) > 0) throw new RangeError("month-grid-exceeds-six-weeks");
|
|
175
|
+
const weeks = [];
|
|
176
|
+
for (let weekStart = start; comparePlainDates(weekStart, end) < 0;) {
|
|
177
|
+
const weekEnd = weekStart.add({ days: 7 });
|
|
178
|
+
weeks.push(plainDateInterval(weekStart, weekEnd));
|
|
179
|
+
weekStart = weekEnd;
|
|
180
|
+
}
|
|
181
|
+
return Object.freeze({
|
|
182
|
+
month,
|
|
183
|
+
range: plainDateInterval(start, end),
|
|
184
|
+
weeks: Object.freeze(weeks)
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/internal/rounding-increment.ts
|
|
189
|
+
/** Rejects increments that native Temporal would silently truncate, such as `1.5`. */
|
|
190
|
+
function assertRoundingIncrement(increment, errorCode) {
|
|
191
|
+
if (!Number.isSafeInteger(increment) || increment < 1) throw new RangeError(errorCode);
|
|
192
|
+
}
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/plain-time/rounding.ts
|
|
195
|
+
function wallTimeRoundingOptions(options, errorCode) {
|
|
196
|
+
assertRoundingIncrement(options.increment, errorCode);
|
|
197
|
+
return {
|
|
198
|
+
roundingIncrement: options.increment,
|
|
199
|
+
roundingMode: options.mode === "floor" ? "floor" : options.mode === "ceil" ? "ceil" : "halfExpand",
|
|
200
|
+
smallestUnit: options.smallestUnit
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/** Rounds a wall-clock time through the native Temporal operation, wrapping at midnight. */
|
|
204
|
+
function roundPlainTime(time, options) {
|
|
205
|
+
const increment = options.increment ?? 1;
|
|
206
|
+
assertRoundingIncrement(increment, "invalid-plain-time-rounding-increment");
|
|
207
|
+
return time.round({
|
|
208
|
+
roundingIncrement: increment,
|
|
209
|
+
roundingMode: options.roundingMode,
|
|
210
|
+
smallestUnit: options.smallestUnit
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/** Snaps a wall-clock time to a midnight-anchored grid and reports a carry into the next day. */
|
|
214
|
+
function snapPlainTime(time, options) {
|
|
215
|
+
const reference = new globalThis.Temporal.PlainDate(2e3, 1, 1);
|
|
216
|
+
const snapped = reference.toPlainDateTime(time).round(wallTimeRoundingOptions(options, "invalid-plain-time-rounding-increment"));
|
|
217
|
+
return Object.freeze({
|
|
218
|
+
days: snapped.day === reference.day ? 0 : 1,
|
|
219
|
+
time: snapped.toPlainTime()
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/internal/time-duration.ts
|
|
224
|
+
const NANOSECONDS_PER_HOUR = 3600000000000n;
|
|
225
|
+
const NANOSECONDS_PER_MINUTE = 60000000000n;
|
|
226
|
+
const NANOSECONDS_PER_SECOND = 1000000000n;
|
|
227
|
+
const NANOSECONDS_PER_MILLISECOND = 1000000n;
|
|
228
|
+
const NANOSECONDS_PER_MICROSECOND = 1000n;
|
|
229
|
+
function positiveTimeDuration(value, errorCode) {
|
|
230
|
+
const duration = globalThis.Temporal.Duration.from(value);
|
|
231
|
+
if (duration.sign !== 1 || duration.years !== 0 || duration.months !== 0 || duration.weeks !== 0 || duration.days !== 0) throw new RangeError(errorCode);
|
|
232
|
+
const nanoseconds = BigInt(duration.hours) * NANOSECONDS_PER_HOUR + BigInt(duration.minutes) * NANOSECONDS_PER_MINUTE + BigInt(duration.seconds) * NANOSECONDS_PER_SECOND + BigInt(duration.milliseconds) * NANOSECONDS_PER_MILLISECOND + BigInt(duration.microseconds) * NANOSECONDS_PER_MICROSECOND + BigInt(duration.nanoseconds);
|
|
233
|
+
if (nanoseconds <= 0n) throw new RangeError(errorCode);
|
|
234
|
+
return {
|
|
235
|
+
duration,
|
|
236
|
+
nanoseconds
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/instant/iteration.ts
|
|
241
|
+
const DEFAULT_INSTANT_ITERATION_LIMIT = 1e4;
|
|
242
|
+
function iterationLimit(value) {
|
|
243
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new RangeError("invalid-instant-iteration-limit");
|
|
244
|
+
return value;
|
|
245
|
+
}
|
|
246
|
+
/** Lazily visits instant grid points in `[start, end)` with an explicit positive step. */
|
|
247
|
+
function* iterateInstants(range, options) {
|
|
248
|
+
const limit = iterationLimit(options.limit ?? 1e4);
|
|
249
|
+
const step = positiveTimeDuration(options.step, "invalid-instant-iteration-step");
|
|
250
|
+
let current = range.start;
|
|
251
|
+
let count = 0;
|
|
252
|
+
while (compareInstants(current, range.end) < 0) {
|
|
253
|
+
if (count >= limit) throw new RangeError("instant-iteration-limit-exceeded");
|
|
254
|
+
yield current;
|
|
255
|
+
count += 1;
|
|
256
|
+
const remainingNanoseconds = range.end.epochNanoseconds - current.epochNanoseconds;
|
|
257
|
+
if (step.nanoseconds >= remainingNanoseconds) return;
|
|
258
|
+
current = current.add(step.duration);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
//#endregion
|
|
262
|
+
//#region src/instant/rounding.ts
|
|
263
|
+
/**
|
|
264
|
+
* Rounds an absolute instant on the Unix-epoch grid through the native Temporal operation.
|
|
265
|
+
* Calendar grids are wall-clock grids: use `snapZonedDateTime` for those.
|
|
266
|
+
*/
|
|
267
|
+
function roundInstant(instant, options) {
|
|
268
|
+
const increment = options.increment ?? 1;
|
|
269
|
+
assertRoundingIncrement(increment, "invalid-instant-rounding-increment");
|
|
270
|
+
return instant.round({
|
|
271
|
+
roundingIncrement: increment,
|
|
272
|
+
roundingMode: options.roundingMode,
|
|
273
|
+
smallestUnit: options.smallestUnit
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
//#endregion
|
|
277
|
+
//#region src/clock/calendar-clock.ts
|
|
278
|
+
function createFixedClock(instant) {
|
|
279
|
+
return Object.freeze({ instant: () => instant });
|
|
280
|
+
}
|
|
281
|
+
function createSystemClock() {
|
|
282
|
+
return Object.freeze({ instant: () => globalThis.Temporal.Now.instant() });
|
|
283
|
+
}
|
|
284
|
+
//#endregion
|
|
285
|
+
//#region src/zoned/machine-keys.ts
|
|
286
|
+
/** Stable absolute identity for maps, caches, and render models. */
|
|
287
|
+
function instantKey(instant) {
|
|
288
|
+
return `instant:${instant.epochNanoseconds}`;
|
|
289
|
+
}
|
|
290
|
+
/** Stable calendar-date identity; ISO dates have no calendar annotation. */
|
|
291
|
+
function plainDateKey(date) {
|
|
292
|
+
return `date:${date.toString()}`;
|
|
293
|
+
}
|
|
294
|
+
/** Stable local date-time identity with nanosecond precision. */
|
|
295
|
+
function plainDateTimeKey(dateTime) {
|
|
296
|
+
return `date-time:${dateTime.toString({ smallestUnit: "nanosecond" })}`;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Validates an identifier and normalizes its case. Aliases such as `Asia/Calcutta` and
|
|
300
|
+
* `Asia/Kolkata` deliberately stay distinct: engines disagree on which one is primary, and keys
|
|
301
|
+
* must not change between a server and a browser.
|
|
302
|
+
*/
|
|
303
|
+
function timeZoneIdentifier(timeZone) {
|
|
304
|
+
return new globalThis.Temporal.ZonedDateTime(0n, timeZone).timeZoneId;
|
|
305
|
+
}
|
|
306
|
+
function zonedDayKeyForTimeZoneId(date, timeZoneId) {
|
|
307
|
+
return `${plainDateKey(date)}|time-zone:${timeZoneId}`;
|
|
308
|
+
}
|
|
309
|
+
function zonedWallTimeKeyForTimeZoneId(dateTime, timeZoneId) {
|
|
310
|
+
return `${plainDateTimeKey(dateTime)}|time-zone:${timeZoneId}`;
|
|
311
|
+
}
|
|
312
|
+
/** Stable identity for one nominal date in one explicit timezone. */
|
|
313
|
+
function zonedDayKey(date, timeZone) {
|
|
314
|
+
return zonedDayKeyForTimeZoneId(date, timeZoneIdentifier(timeZone));
|
|
315
|
+
}
|
|
316
|
+
/** Stable wall-coordinate identity. It deliberately does not identify a fold occurrence. */
|
|
317
|
+
function zonedWallTimeKey(dateTime, timeZone) {
|
|
318
|
+
return zonedWallTimeKeyForTimeZoneId(dateTime, timeZoneIdentifier(timeZone));
|
|
319
|
+
}
|
|
320
|
+
//#endregion
|
|
321
|
+
//#region src/zoned/projection.ts
|
|
322
|
+
function occurrence(zonedDateTime) {
|
|
323
|
+
const instant = zonedDateTime.toInstant();
|
|
324
|
+
return Object.freeze({
|
|
325
|
+
instant,
|
|
326
|
+
key: instantKey(instant),
|
|
327
|
+
offset: zonedDateTime.offset,
|
|
328
|
+
offsetNanoseconds: zonedDateTime.offsetNanoseconds,
|
|
329
|
+
wallTime: zonedDateTime.toPlainDateTime(),
|
|
330
|
+
zonedDateTime
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
function adjustment(requestedWallTime, selected, direction) {
|
|
334
|
+
return Object.freeze({
|
|
335
|
+
direction,
|
|
336
|
+
requestedWallTime,
|
|
337
|
+
resolvedWallTime: selected.wallTime,
|
|
338
|
+
wallTimeDelta: requestedWallTime.until(selected.wallTime, { largestUnit: "day" })
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Projects a nominal wall coordinate through an explicit timezone and policy.
|
|
343
|
+
* Gaps retain the requested and shifted wall times; folds retain both instant identities.
|
|
344
|
+
*/
|
|
345
|
+
function resolveZonedWallTime(requestedWallTime, environment) {
|
|
346
|
+
const earlier = occurrence(requestedWallTime.toZonedDateTime(environment.timeZone, { disambiguation: "earlier" }));
|
|
347
|
+
const later = occurrence(requestedWallTime.toZonedDateTime(environment.timeZone, { disambiguation: "later" }));
|
|
348
|
+
const earlierRoundTrips = earlier.wallTime.equals(requestedWallTime);
|
|
349
|
+
const laterRoundTrips = later.wallTime.equals(requestedWallTime);
|
|
350
|
+
const timeZone = earlier.zonedDateTime.timeZoneId;
|
|
351
|
+
const key = zonedWallTimeKeyForTimeZoneId(requestedWallTime, timeZone);
|
|
352
|
+
if (!earlierRoundTrips && !laterRoundTrips) {
|
|
353
|
+
const base = {
|
|
354
|
+
disambiguation: environment.disambiguation,
|
|
355
|
+
key,
|
|
356
|
+
kind: "gap",
|
|
357
|
+
occurrences: Object.freeze([]),
|
|
358
|
+
requestedWallTime,
|
|
359
|
+
timeZone
|
|
360
|
+
};
|
|
361
|
+
if (environment.disambiguation === "reject") return Object.freeze({
|
|
362
|
+
...base,
|
|
363
|
+
adjustment: null,
|
|
364
|
+
selected: null,
|
|
365
|
+
selectedBy: null,
|
|
366
|
+
status: "rejected"
|
|
367
|
+
});
|
|
368
|
+
const selectedBy = environment.disambiguation === "earlier" ? "earlier" : "later";
|
|
369
|
+
const selected = selectedBy === "earlier" ? earlier : later;
|
|
370
|
+
return Object.freeze({
|
|
371
|
+
...base,
|
|
372
|
+
adjustment: adjustment(requestedWallTime, selected, selectedBy),
|
|
373
|
+
selected,
|
|
374
|
+
selectedBy,
|
|
375
|
+
status: "resolved"
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
if (earlier.instant.epochNanoseconds !== later.instant.epochNanoseconds) {
|
|
379
|
+
const occurrences = Object.freeze([earlier, later]);
|
|
380
|
+
const base = {
|
|
381
|
+
disambiguation: environment.disambiguation,
|
|
382
|
+
key,
|
|
383
|
+
kind: "fold",
|
|
384
|
+
occurrences,
|
|
385
|
+
requestedWallTime,
|
|
386
|
+
timeZone
|
|
387
|
+
};
|
|
388
|
+
if (environment.disambiguation === "reject") return Object.freeze({
|
|
389
|
+
...base,
|
|
390
|
+
adjustment: null,
|
|
391
|
+
selected: null,
|
|
392
|
+
selectedBy: null,
|
|
393
|
+
status: "rejected"
|
|
394
|
+
});
|
|
395
|
+
const selectedBy = environment.disambiguation === "later" ? "later" : "earlier";
|
|
396
|
+
return Object.freeze({
|
|
397
|
+
...base,
|
|
398
|
+
adjustment: null,
|
|
399
|
+
selected: selectedBy === "earlier" ? earlier : later,
|
|
400
|
+
selectedBy,
|
|
401
|
+
status: "resolved"
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
return Object.freeze({
|
|
405
|
+
adjustment: null,
|
|
406
|
+
disambiguation: environment.disambiguation,
|
|
407
|
+
key,
|
|
408
|
+
kind: "unique",
|
|
409
|
+
occurrences: Object.freeze([earlier]),
|
|
410
|
+
requestedWallTime,
|
|
411
|
+
selected: earlier,
|
|
412
|
+
selectedBy: "unique",
|
|
413
|
+
status: "resolved",
|
|
414
|
+
timeZone
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
/** Projects an absolute instant while retaining the fields needed for stable rendering and editing. */
|
|
418
|
+
function projectInstantToZonedTime(instant, timeZone) {
|
|
419
|
+
const zonedDateTime = instant.toZonedDateTimeISO(timeZone);
|
|
420
|
+
const wallTime = zonedDateTime.toPlainDateTime();
|
|
421
|
+
return Object.freeze({
|
|
422
|
+
instant,
|
|
423
|
+
key: instantKey(instant),
|
|
424
|
+
offset: zonedDateTime.offset,
|
|
425
|
+
offsetNanoseconds: zonedDateTime.offsetNanoseconds,
|
|
426
|
+
timeZone: zonedDateTime.timeZoneId,
|
|
427
|
+
wallTime,
|
|
428
|
+
wallTimeKey: zonedWallTimeKeyForTimeZoneId(wallTime, zonedDateTime.timeZoneId),
|
|
429
|
+
zonedDateTime
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region src/zoned/rounding.ts
|
|
434
|
+
const INVALID_INCREMENT = "invalid-zoned-date-time-rounding-increment";
|
|
435
|
+
/** The transition instant that ends the gap containing `wallTime`: the gap's only real instant. */
|
|
436
|
+
function gapTransition(wallTime, timeZone) {
|
|
437
|
+
return wallTime.toZonedDateTime(timeZone, { disambiguation: "earlier" }).getTimeZoneTransition("next");
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Snaps a zoned value to a midnight-anchored wall-clock grid in its own timezone.
|
|
441
|
+
*
|
|
442
|
+
* A snapped wall time inside a fold keeps the input's offset, so a value in the repeated hour stays
|
|
443
|
+
* in the same occurrence. A snapped wall time inside a gap resolves to the transition instant, the
|
|
444
|
+
* only real instant at that wall position. `floor` therefore never moves later and `ceil` never
|
|
445
|
+
* moves earlier, even on DST days.
|
|
446
|
+
*/
|
|
447
|
+
function snapZonedDateTime(zonedDateTime, options) {
|
|
448
|
+
const rounding = wallTimeRoundingOptions(options, INVALID_INCREMENT);
|
|
449
|
+
const wallTime = zonedDateTime.toPlainDateTime().round(rounding);
|
|
450
|
+
const rounded = zonedDateTime.round(rounding);
|
|
451
|
+
return rounded.toPlainDateTime().equals(wallTime) ? rounded : gapTransition(wallTime, zonedDateTime.timeZoneId);
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Snaps a nominal wall time, such as a pointer position, to a midnight-anchored grid and only then
|
|
455
|
+
* resolves it in `timeZone`: a repeated wall time picks the `fold` occurrence and a skipped one
|
|
456
|
+
* resolves to the transition instant.
|
|
457
|
+
*/
|
|
458
|
+
function snapZonedWallTime(wallTime, options) {
|
|
459
|
+
const snapped = wallTime.round(wallTimeRoundingOptions(options, INVALID_INCREMENT));
|
|
460
|
+
const resolved = snapped.toZonedDateTime(options.timeZone, { disambiguation: options.fold ?? "earlier" });
|
|
461
|
+
return resolved.toPlainDateTime().equals(snapped) ? resolved : gapTransition(snapped, options.timeZone);
|
|
462
|
+
}
|
|
463
|
+
//#endregion
|
|
464
|
+
//#region src/zoned/day.ts
|
|
465
|
+
const DEFAULT_ZONED_DAY_SPLIT_LIMIT = 1e4;
|
|
466
|
+
const MINIMUM_INSTANT_NANOSECONDS = -8640000000000000000000n;
|
|
467
|
+
const MAXIMUM_INSTANT_NANOSECONDS = 8640000000000000000000n;
|
|
468
|
+
const MAXIMUM_TIME_ZONE_OFFSET_SPAN_NANOSECONDS = 172800000000000n;
|
|
469
|
+
const MAXIMUM_ZONED_DAY_BOUNDARY_CORRECTIONS = 4;
|
|
470
|
+
function positiveLimit(value) {
|
|
471
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new RangeError("invalid-zoned-day-split-limit");
|
|
472
|
+
return value;
|
|
473
|
+
}
|
|
474
|
+
/** Returns the actual instant extent of one nominal date in an explicit timezone. */
|
|
475
|
+
function zonedDayRange(date, timeZone) {
|
|
476
|
+
const startBoundary = date.toZonedDateTime(timeZone);
|
|
477
|
+
const endBoundary = date.add({ days: 1 }).toZonedDateTime(timeZone);
|
|
478
|
+
const start = startBoundary.toInstant();
|
|
479
|
+
const end = endBoundary.toInstant();
|
|
480
|
+
const ordering = compareInstants(start, end);
|
|
481
|
+
if (ordering > 0) throw new RangeError("zoned-day-boundaries-reversed");
|
|
482
|
+
const base = {
|
|
483
|
+
date,
|
|
484
|
+
duration: start.until(end, { largestUnit: "hour" }),
|
|
485
|
+
durationNanoseconds: end.epochNanoseconds - start.epochNanoseconds,
|
|
486
|
+
end,
|
|
487
|
+
endBoundary,
|
|
488
|
+
key: zonedDayKeyForTimeZoneId(date, startBoundary.timeZoneId),
|
|
489
|
+
start,
|
|
490
|
+
startBoundary,
|
|
491
|
+
timeZone: startBoundary.timeZoneId
|
|
492
|
+
};
|
|
493
|
+
if (ordering === 0) return Object.freeze({
|
|
494
|
+
...base,
|
|
495
|
+
interval: null,
|
|
496
|
+
kind: "skipped"
|
|
497
|
+
});
|
|
498
|
+
return Object.freeze({
|
|
499
|
+
...base,
|
|
500
|
+
interval: instantInterval(start, end),
|
|
501
|
+
kind: "available"
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
function wallRangesIntersect(leftStart, leftEnd, right) {
|
|
505
|
+
return comparePlainDateTimes(leftStart, right.end) < 0 && comparePlainDateTimes(leftEnd, right.start) > 0;
|
|
506
|
+
}
|
|
507
|
+
/** Returns timezone offset transitions whose affected wall interval intersects the nominal date. */
|
|
508
|
+
function zonedDayTransitions(date, timeZone) {
|
|
509
|
+
const day = zonedDayRange(date, timeZone);
|
|
510
|
+
const wallStart = date.toPlainDateTime();
|
|
511
|
+
const wallEnd = date.add({ days: 1 }).toPlainDateTime();
|
|
512
|
+
const searchStartNanoseconds = day.start.epochNanoseconds === MINIMUM_INSTANT_NANOSECONDS ? MINIMUM_INSTANT_NANOSECONDS : day.start.epochNanoseconds - 1n;
|
|
513
|
+
const latestRelevantTransitionNanoseconds = day.end.epochNanoseconds > MAXIMUM_INSTANT_NANOSECONDS - MAXIMUM_TIME_ZONE_OFFSET_SPAN_NANOSECONDS ? MAXIMUM_INSTANT_NANOSECONDS : day.end.epochNanoseconds + MAXIMUM_TIME_ZONE_OFFSET_SPAN_NANOSECONDS;
|
|
514
|
+
let cursor = new globalThis.Temporal.ZonedDateTime(searchStartNanoseconds, day.timeZone);
|
|
515
|
+
const transitions = [];
|
|
516
|
+
for (;;) {
|
|
517
|
+
const after = cursor.getTimeZoneTransition("next");
|
|
518
|
+
if (after === null || after.epochNanoseconds > latestRelevantTransitionNanoseconds) break;
|
|
519
|
+
const instant = after.toInstant();
|
|
520
|
+
const before = instant.subtract({ nanoseconds: 1 }).toZonedDateTimeISO(day.timeZone);
|
|
521
|
+
const offsetDeltaNanoseconds = after.offsetNanoseconds - before.offsetNanoseconds;
|
|
522
|
+
if (offsetDeltaNanoseconds !== 0) {
|
|
523
|
+
const afterWallTime = after.toPlainDateTime();
|
|
524
|
+
const affectedWallRange = offsetDeltaNanoseconds > 0 ? plainDateTimeInterval(afterWallTime.subtract({ nanoseconds: offsetDeltaNanoseconds }), afterWallTime) : plainDateTimeInterval(afterWallTime, afterWallTime.add({ nanoseconds: -offsetDeltaNanoseconds }));
|
|
525
|
+
if (wallRangesIntersect(wallStart, wallEnd, affectedWallRange)) transitions.push(Object.freeze({
|
|
526
|
+
affectedWallRange,
|
|
527
|
+
instant,
|
|
528
|
+
key: instantKey(instant),
|
|
529
|
+
kind: offsetDeltaNanoseconds > 0 ? "gap" : "fold",
|
|
530
|
+
offsetAfter: after.offset,
|
|
531
|
+
offsetBefore: before.offset,
|
|
532
|
+
offsetDeltaNanoseconds,
|
|
533
|
+
timeZone: day.timeZone
|
|
534
|
+
}));
|
|
535
|
+
}
|
|
536
|
+
cursor = after;
|
|
537
|
+
}
|
|
538
|
+
return Object.freeze(transitions);
|
|
539
|
+
}
|
|
540
|
+
function zonedDayContainingInstant(instant, timeZone) {
|
|
541
|
+
let date = instant.toZonedDateTimeISO(timeZone).toPlainDate();
|
|
542
|
+
for (let correction = 0; correction < MAXIMUM_ZONED_DAY_BOUNDARY_CORRECTIONS; correction += 1) {
|
|
543
|
+
const day = zonedDayRange(date, timeZone);
|
|
544
|
+
if (day.kind === "available" && compareInstants(day.start, instant) <= 0 && compareInstants(instant, day.end) < 0) return day;
|
|
545
|
+
date = compareInstants(instant, day.start) < 0 ? date.subtract({ days: 1 }) : date.add({ days: 1 });
|
|
546
|
+
}
|
|
547
|
+
throw new RangeError("instant-not-contained-by-projected-zoned-day");
|
|
548
|
+
}
|
|
549
|
+
/** Splits an absolute interval at actual zoned-day boundaries without inventing skipped dates. */
|
|
550
|
+
function splitInstantRangeByZonedDay(range, timeZone, options = {}) {
|
|
551
|
+
const limit = positiveLimit(options.limit ?? 1e4);
|
|
552
|
+
const splits = [];
|
|
553
|
+
let current = range.start;
|
|
554
|
+
while (compareInstants(current, range.end) < 0) {
|
|
555
|
+
if (splits.length >= limit) throw new RangeError("zoned-day-split-limit-exceeded");
|
|
556
|
+
const day = zonedDayContainingInstant(current, timeZone);
|
|
557
|
+
const date = day.date;
|
|
558
|
+
const end = compareInstants(day.end, range.end) < 0 ? day.end : range.end;
|
|
559
|
+
const interval = instantInterval(current, end);
|
|
560
|
+
splits.push(Object.freeze({
|
|
561
|
+
continuesAfterDay: compareInstants(range.end, day.end) > 0,
|
|
562
|
+
continuesBeforeDay: compareInstants(range.start, day.start) < 0,
|
|
563
|
+
date,
|
|
564
|
+
day,
|
|
565
|
+
interval,
|
|
566
|
+
key: `${day.key}|${instantKey(current)}`,
|
|
567
|
+
timeZone: day.timeZone
|
|
568
|
+
}));
|
|
569
|
+
current = end;
|
|
570
|
+
}
|
|
571
|
+
return Object.freeze(splits);
|
|
572
|
+
}
|
|
573
|
+
//#endregion
|
|
574
|
+
//#region src/zoned/slots.ts
|
|
575
|
+
const NOMINAL_DAY_NANOSECONDS = 86400000000000n;
|
|
576
|
+
const DEFAULT_ZONED_SLOT_LIMIT = 1e4;
|
|
577
|
+
function carryoverOccurrence(start, end, timeZone) {
|
|
578
|
+
const projection = projectInstantToZonedTime(start, timeZone);
|
|
579
|
+
return Object.freeze({
|
|
580
|
+
interval: instantInterval(start, end),
|
|
581
|
+
key: projection.key,
|
|
582
|
+
start: projection
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
function slotLimit(value) {
|
|
586
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new RangeError("invalid-zoned-slot-limit");
|
|
587
|
+
return value;
|
|
588
|
+
}
|
|
589
|
+
function segmentBoundaries(slotRange, transitions) {
|
|
590
|
+
const candidates = [slotRange.start, slotRange.end];
|
|
591
|
+
for (const transition of transitions) for (const boundary of [transition.affectedWallRange.start, transition.affectedWallRange.end]) if (comparePlainDateTimes(boundary, slotRange.start) > 0 && comparePlainDateTimes(boundary, slotRange.end) < 0) candidates.push(boundary);
|
|
592
|
+
candidates.sort(comparePlainDateTimes);
|
|
593
|
+
return Object.freeze(candidates.filter((candidate, index) => index === 0 || !candidate.equals(candidates[index - 1])));
|
|
594
|
+
}
|
|
595
|
+
function buildSegment(wallRange, environment, day) {
|
|
596
|
+
const projection = resolveZonedWallTime(wallRange.start, environment);
|
|
597
|
+
const wallDuration = wallRange.start.until(wallRange.end, { largestUnit: "hour" });
|
|
598
|
+
const occurrences = [];
|
|
599
|
+
if (day.interval !== null) for (const start of projection.occurrences) {
|
|
600
|
+
const interval = intersectInstantIntervals(instantInterval(start.instant, start.instant.add(wallDuration)), day.interval);
|
|
601
|
+
if (interval !== null) occurrences.push(Object.freeze({
|
|
602
|
+
interval,
|
|
603
|
+
key: start.key,
|
|
604
|
+
start
|
|
605
|
+
}));
|
|
606
|
+
}
|
|
607
|
+
const physicalDurationNanoseconds = occurrences.reduce((total, item) => total + item.interval.end.epochNanoseconds - item.interval.start.epochNanoseconds, 0n);
|
|
608
|
+
return Object.freeze({
|
|
609
|
+
key: `${projection.key}|segment-end:${wallRange.end.toString({ smallestUnit: "nanosecond" })}`,
|
|
610
|
+
kind: projection.kind,
|
|
611
|
+
occurrences: Object.freeze(occurrences),
|
|
612
|
+
physicalDurationNanoseconds,
|
|
613
|
+
projection,
|
|
614
|
+
wallRange
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
function carryoverOccurrences(day, slots) {
|
|
618
|
+
if (day.interval === null) return Object.freeze([]);
|
|
619
|
+
const represented = slots.flatMap((slot) => slot.segments.flatMap((segment) => segment.occurrences)).toSorted((left, right) => compareInstants(left.interval.start, right.interval.start));
|
|
620
|
+
const carryover = [];
|
|
621
|
+
let cursor = day.start;
|
|
622
|
+
for (const occurrence of represented) {
|
|
623
|
+
if (compareInstants(occurrence.interval.end, cursor) <= 0) continue;
|
|
624
|
+
if (compareInstants(cursor, occurrence.interval.start) < 0) carryover.push(carryoverOccurrence(cursor, occurrence.interval.start, day.timeZone));
|
|
625
|
+
if (compareInstants(cursor, occurrence.interval.end) < 0) cursor = occurrence.interval.end;
|
|
626
|
+
}
|
|
627
|
+
if (compareInstants(cursor, day.end) < 0) carryover.push(carryoverOccurrence(cursor, day.end, day.timeZone));
|
|
628
|
+
return Object.freeze(carryover);
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Builds a nominal wall-clock grid. Gaps have zero occurrences, folds have two, and a timezone
|
|
632
|
+
* transition inside a configured slot produces explicit sub-segments.
|
|
633
|
+
*/
|
|
634
|
+
function slotsInZonedDay(date, options) {
|
|
635
|
+
const step = positiveTimeDuration(options.slotDuration, "invalid-zoned-slot-duration");
|
|
636
|
+
const limit = slotLimit(options.limit ?? 1e4);
|
|
637
|
+
if ((NOMINAL_DAY_NANOSECONDS + step.nanoseconds - 1n) / step.nanoseconds > BigInt(limit)) throw new RangeError("zoned-slot-limit-exceeded");
|
|
638
|
+
const day = zonedDayRange(date, options.timeZone);
|
|
639
|
+
const environment = {
|
|
640
|
+
disambiguation: options.disambiguation,
|
|
641
|
+
timeZone: day.timeZone
|
|
642
|
+
};
|
|
643
|
+
const transitions = zonedDayTransitions(date, day.timeZone);
|
|
644
|
+
const wallDayStart = date.toPlainDateTime();
|
|
645
|
+
const slots = [];
|
|
646
|
+
for (let startOffset = 0n; startOffset < NOMINAL_DAY_NANOSECONDS;) {
|
|
647
|
+
const endOffset = startOffset + step.nanoseconds < NOMINAL_DAY_NANOSECONDS ? startOffset + step.nanoseconds : NOMINAL_DAY_NANOSECONDS;
|
|
648
|
+
const wallStart = wallDayStart.add({ nanoseconds: Number(startOffset) });
|
|
649
|
+
const wallRange = plainDateTimeInterval(wallStart, wallDayStart.add({ nanoseconds: Number(endOffset) }));
|
|
650
|
+
const boundaries = segmentBoundaries(wallRange, transitions);
|
|
651
|
+
const segments = [];
|
|
652
|
+
for (let index = 1; index < boundaries.length; index += 1) segments.push(buildSegment(plainDateTimeInterval(boundaries[index - 1], boundaries[index]), environment, day));
|
|
653
|
+
const physicalDurationNanoseconds = segments.reduce((total, segment) => total + segment.physicalDurationNanoseconds, 0n);
|
|
654
|
+
slots.push(Object.freeze({
|
|
655
|
+
index: slots.length,
|
|
656
|
+
key: zonedWallTimeKeyForTimeZoneId(wallStart, day.timeZone),
|
|
657
|
+
kind: segments.length === 1 ? segments[0].kind : "transition",
|
|
658
|
+
physicalDurationNanoseconds,
|
|
659
|
+
segments: Object.freeze(segments),
|
|
660
|
+
wallRange
|
|
661
|
+
}));
|
|
662
|
+
startOffset = endOffset;
|
|
663
|
+
}
|
|
664
|
+
const frozenSlots = Object.freeze(slots);
|
|
665
|
+
const carryover = carryoverOccurrences(day, frozenSlots);
|
|
666
|
+
return Object.freeze({
|
|
667
|
+
carryoverOccurrences: carryover,
|
|
668
|
+
day,
|
|
669
|
+
disambiguation: options.disambiguation,
|
|
670
|
+
physicalDurationNanoseconds: day.durationNanoseconds,
|
|
671
|
+
slotDuration: step.duration,
|
|
672
|
+
slotDurationNanoseconds: step.nanoseconds,
|
|
673
|
+
slots: frozenSlots,
|
|
674
|
+
timeZone: day.timeZone,
|
|
675
|
+
transitions
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
//#endregion
|
|
679
|
+
export { DEFAULT_INSTANT_ITERATION_LIMIT, DEFAULT_PLAIN_DATE_ITERATION_LIMIT, DEFAULT_ZONED_DAY_SPLIT_LIMIT, DEFAULT_ZONED_SLOT_LIMIT, InvalidHalfOpenIntervalError, compareInstants, comparePlainDateTimes, comparePlainDates, comparePlainTimes, createFixedClock, createSystemClock, halfOpenInterval, instantInterval, instantIntervalContains, instantIntervalContainsInterval, instantIntervalsIntersect, instantKey, intersectInstantIntervals, intersectPlainDateIntervals, intersectPlainDateTimeIntervals, iterateInstants, iteratePlainDates, monthGrid, plainDateInterval, plainDateIntervalContains, plainDateIntervalContainsInterval, plainDateIntervalsIntersect, plainDateKey, plainDateTimeInterval, plainDateTimeIntervalContains, plainDateTimeIntervalContainsInterval, plainDateTimeIntervalsIntersect, plainDateTimeKey, projectInstantToZonedTime, resolveZonedWallTime, roundInstant, roundPlainTime, slotsInZonedDay, snapPlainTime, snapZonedDateTime, snapZonedWallTime, splitInstantInterval, splitInstantRangeByZonedDay, splitPlainDateInterval, splitPlainDateTimeInterval, validateHalfOpenInterval, weekRange, zonedDayKey, zonedDayRange, zonedDayTransitions, zonedWallTimeKey };
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moltenship/calendar-temporal",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-rc.2",
|
|
4
|
+
"description": "Framework-independent Temporal utilities for Moltenship Time.",
|
|
5
|
+
"homepage": "https://time.molten.sh/docs/api/temporal",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/Moltenship/time/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/Moltenship/time.git",
|
|
13
|
+
"directory": "packages/calendar-temporal"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"import": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"fast-check": "^4.10.2",
|
|
32
|
+
"typescript": "^7.0.2",
|
|
33
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.1.0",
|
|
34
|
+
"vite-plus": "1.1.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "vp pack",
|
|
38
|
+
"check": "vp check",
|
|
39
|
+
"dev": "vp pack --watch",
|
|
40
|
+
"test": "vp test --passWithNoTests"
|
|
41
|
+
}
|
|
6
42
|
}
|