@klinking/colander 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/formats/date.cjs +49 -0
- package/dist/formats/date.d.cts +45 -0
- package/dist/formats/date.d.mts +45 -0
- package/dist/formats/date.mjs +2 -0
- package/dist/formats/object.cjs +49 -0
- package/dist/formats/object.d.cts +45 -0
- package/dist/formats/object.d.mts +45 -0
- package/dist/formats/object.mjs +2 -0
- package/dist/formats/plain-date-time.cjs +49 -0
- package/dist/formats/plain-date-time.d.cts +45 -0
- package/dist/formats/plain-date-time.d.mts +45 -0
- package/dist/formats/plain-date-time.mjs +2 -0
- package/dist/formats/plain-date.cjs +49 -0
- package/dist/formats/plain-date.d.cts +45 -0
- package/dist/formats/plain-date.d.mts +45 -0
- package/dist/formats/plain-date.mjs +2 -0
- package/dist/formats/plain-month-day.cjs +49 -0
- package/dist/formats/plain-month-day.d.cts +45 -0
- package/dist/formats/plain-month-day.d.mts +45 -0
- package/dist/formats/plain-month-day.mjs +2 -0
- package/dist/formats/plain-year-month.cjs +49 -0
- package/dist/formats/plain-year-month.d.cts +45 -0
- package/dist/formats/plain-year-month.d.mts +45 -0
- package/dist/formats/plain-year-month.mjs +2 -0
- package/dist/formats/zoned-date-time.cjs +49 -0
- package/dist/formats/zoned-date-time.d.cts +45 -0
- package/dist/formats/zoned-date-time.d.mts +45 -0
- package/dist/formats/zoned-date-time.mjs +2 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/temporal-polyfill-DTx0VIX5.mjs +4556 -0
- package/dist/temporal-polyfill-DU8VfleL.cjs +4860 -0
- package/dist/weeks-view-context-BN6qR-83.d.mts +2022 -0
- package/dist/weeks-view-context-aHGgYkpt.d.cts +2022 -0
- package/package.json +89 -0
- package/src/calendar-context.ts +22 -0
- package/src/calendar-provider.test.tsx +123 -0
- package/src/calendar-provider.tsx +538 -0
- package/src/calendar-types.ts +212 -0
- package/src/compute-weeks-in-window.test.ts +83 -0
- package/src/compute-weeks-in-window.ts +40 -0
- package/src/context.ts +34 -0
- package/src/day-cell.tsx +635 -0
- package/src/drag-handle.tsx +137 -0
- package/src/formats/date.ts +110 -0
- package/src/formats/formats.test.ts +18 -0
- package/src/formats/object.ts +110 -0
- package/src/formats/plain-date-time.ts +110 -0
- package/src/formats/plain-date.ts +110 -0
- package/src/formats/plain-month-day.ts +110 -0
- package/src/formats/plain-year-month.ts +110 -0
- package/src/formats/zoned-date-time.ts +110 -0
- package/src/grid-header.tsx +141 -0
- package/src/grid.event-perf.test.tsx +282 -0
- package/src/grid.perf.test.tsx +467 -0
- package/src/grid.tsx +608 -0
- package/src/index.ts +116 -0
- package/src/keyboard.test.ts +498 -0
- package/src/keyboard.ts +142 -0
- package/src/month-separator.test.tsx +183 -0
- package/src/month-separator.tsx +238 -0
- package/src/month-view-context.ts +26 -0
- package/src/month-view-types.ts +89 -0
- package/src/month-view.test.tsx +1433 -0
- package/src/month-view.tsx +373 -0
- package/src/navigation.tsx +350 -0
- package/src/overflow.test.ts +124 -0
- package/src/overflow.ts +233 -0
- package/src/range-preview.tsx +90 -0
- package/src/resolve-first-week.test.ts +132 -0
- package/src/resolve-first-week.ts +145 -0
- package/src/root-selection.ts +621 -0
- package/src/selected-range.tsx +163 -0
- package/src/temporal-polyfill.test.ts +317 -0
- package/src/temporal-polyfill.ts +566 -0
- package/src/test-temporal.ts +17 -0
- package/src/types.ts +814 -0
- package/src/utils.bench.ts +183 -0
- package/src/utils.test.ts +983 -0
- package/src/utils.ts +493 -0
- package/src/view-context.ts +28 -0
- package/src/week-number.tsx +118 -0
- package/src/weeks-grid.tsx +265 -0
- package/src/weeks-keyboard.test.ts +222 -0
- package/src/weeks-keyboard.ts +156 -0
- package/src/weeks-navigation.tsx +209 -0
- package/src/weeks-view-context.ts +26 -0
- package/src/weeks-view-state.ts +421 -0
- package/src/weeks-view-types.ts +102 -0
- package/src/weeks-view.test.tsx +479 -0
- package/src/weeks-view.tsx +127 -0
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
import type { TemporalNamespace } from "./types";
|
|
2
|
+
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// Helpers (private)
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
function daysInMonth(year: number, month: number): number {
|
|
8
|
+
return new Date(year, month, 0).getDate();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function toUTC(y: number, m: number, d: number): number {
|
|
12
|
+
return Date.UTC(y, m - 1, d);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isoWeekday(utcDate: Date): number {
|
|
16
|
+
const dow = utcDate.getUTCDay();
|
|
17
|
+
return dow === 0 ? 7 : dow;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function pad2(n: number): string {
|
|
21
|
+
return n < 10 ? `0${n}` : `${n}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function clamp(val: number, min: number, max: number): number {
|
|
25
|
+
return val < min ? min : val > max ? max : val;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseDateISO(str: string): {
|
|
29
|
+
year: number;
|
|
30
|
+
month: number;
|
|
31
|
+
day: number;
|
|
32
|
+
} {
|
|
33
|
+
const [datePart] = str.split("T");
|
|
34
|
+
const [y, m, d] = datePart.split("-").map(Number);
|
|
35
|
+
return { year: y, month: m, day: d };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function parseDateTimeISO(str: string): {
|
|
39
|
+
year: number;
|
|
40
|
+
month: number;
|
|
41
|
+
day: number;
|
|
42
|
+
hour: number;
|
|
43
|
+
minute: number;
|
|
44
|
+
second: number;
|
|
45
|
+
} {
|
|
46
|
+
const [datePart, timePart] = str.split("T");
|
|
47
|
+
const [y, m, d] = datePart.split("-").map(Number);
|
|
48
|
+
if (!timePart) return { year: y, month: m, day: d, hour: 0, minute: 0, second: 0 };
|
|
49
|
+
const [h, min, s] = timePart.split(":").map(Number);
|
|
50
|
+
return {
|
|
51
|
+
year: y,
|
|
52
|
+
month: m,
|
|
53
|
+
day: d,
|
|
54
|
+
hour: h || 0,
|
|
55
|
+
minute: min || 0,
|
|
56
|
+
second: s || 0,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Cache Intl.DateTimeFormat instances per timezone
|
|
61
|
+
const dtfCache = new Map<string, Intl.DateTimeFormat>();
|
|
62
|
+
|
|
63
|
+
function getDTF(tz: string): Intl.DateTimeFormat {
|
|
64
|
+
let dtf = dtfCache.get(tz);
|
|
65
|
+
if (!dtf) {
|
|
66
|
+
dtf = new Intl.DateTimeFormat("en-US", {
|
|
67
|
+
timeZone: tz,
|
|
68
|
+
year: "numeric",
|
|
69
|
+
month: "numeric",
|
|
70
|
+
day: "numeric",
|
|
71
|
+
hour: "numeric",
|
|
72
|
+
minute: "numeric",
|
|
73
|
+
second: "numeric",
|
|
74
|
+
hour12: false,
|
|
75
|
+
});
|
|
76
|
+
dtfCache.set(tz, dtf);
|
|
77
|
+
}
|
|
78
|
+
return dtf;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface TzParts {
|
|
82
|
+
year: number;
|
|
83
|
+
month: number;
|
|
84
|
+
day: number;
|
|
85
|
+
hour: number;
|
|
86
|
+
minute: number;
|
|
87
|
+
second: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function extractTzParts(epochMs: number, tz: string): TzParts {
|
|
91
|
+
const dtf = getDTF(tz);
|
|
92
|
+
const parts = dtf.formatToParts(new Date(epochMs));
|
|
93
|
+
const vals: Record<string, number> = {};
|
|
94
|
+
for (const p of parts) {
|
|
95
|
+
if (p.type !== "literal") {
|
|
96
|
+
vals[p.type] = Number(p.value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
year: vals.year,
|
|
101
|
+
month: vals.month,
|
|
102
|
+
day: vals.day,
|
|
103
|
+
hour: vals.hour === 24 ? 0 : vals.hour,
|
|
104
|
+
minute: vals.minute,
|
|
105
|
+
second: vals.second,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function wallToEpoch(
|
|
110
|
+
y: number,
|
|
111
|
+
m: number,
|
|
112
|
+
d: number,
|
|
113
|
+
h: number,
|
|
114
|
+
min: number,
|
|
115
|
+
s: number,
|
|
116
|
+
tz: string,
|
|
117
|
+
): number {
|
|
118
|
+
const utcGuess = Date.UTC(y, m - 1, d, h, min, s);
|
|
119
|
+
const inTz = extractTzParts(utcGuess, tz);
|
|
120
|
+
const wallOfGuess = Date.UTC(
|
|
121
|
+
inTz.year,
|
|
122
|
+
inTz.month - 1,
|
|
123
|
+
inTz.day,
|
|
124
|
+
inTz.hour,
|
|
125
|
+
inTz.minute,
|
|
126
|
+
inTz.second,
|
|
127
|
+
);
|
|
128
|
+
return utcGuess - (wallOfGuess - utcGuess);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
// MiniPlainDate
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
class MiniPlainDate {
|
|
136
|
+
readonly year: number;
|
|
137
|
+
readonly month: number;
|
|
138
|
+
readonly day: number;
|
|
139
|
+
|
|
140
|
+
constructor(year: number, month: number, day: number) {
|
|
141
|
+
this.year = year;
|
|
142
|
+
this.month = month;
|
|
143
|
+
this.day = day;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
get dayOfWeek(): number {
|
|
147
|
+
return isoWeekday(new Date(toUTC(this.year, this.month, this.day)));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
get daysInWeek(): number {
|
|
151
|
+
return 7;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
get daysInMonth(): number {
|
|
155
|
+
return daysInMonth(this.year, this.month);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get monthCode(): string {
|
|
159
|
+
return `M${pad2(this.month)}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
add(dur: { days?: number; weeks?: number; months?: number; years?: number }): MiniPlainDate {
|
|
163
|
+
let y = this.year + (dur.years || 0);
|
|
164
|
+
let m = this.month + (dur.months || 0);
|
|
165
|
+
|
|
166
|
+
// Resolve month overflow/underflow
|
|
167
|
+
while (m > 12) {
|
|
168
|
+
m -= 12;
|
|
169
|
+
y += 1;
|
|
170
|
+
}
|
|
171
|
+
while (m < 1) {
|
|
172
|
+
m += 12;
|
|
173
|
+
y -= 1;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Clamp day to valid range for new month
|
|
177
|
+
const d = Math.min(this.day, daysInMonth(y, m));
|
|
178
|
+
|
|
179
|
+
// Add days and weeks via epoch arithmetic
|
|
180
|
+
const totalDays = (dur.days || 0) + (dur.weeks || 0) * 7;
|
|
181
|
+
if (totalDays === 0) return new MiniPlainDate(y, m, d);
|
|
182
|
+
|
|
183
|
+
const epoch = toUTC(y, m, d) + totalDays * 86400000;
|
|
184
|
+
const ud = new Date(epoch);
|
|
185
|
+
return new MiniPlainDate(ud.getUTCFullYear(), ud.getUTCMonth() + 1, ud.getUTCDate());
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
subtract(dur: { days?: number; weeks?: number; months?: number; years?: number }): MiniPlainDate {
|
|
189
|
+
return this.add({
|
|
190
|
+
days: dur.days ? -dur.days : undefined,
|
|
191
|
+
weeks: dur.weeks ? -dur.weeks : undefined,
|
|
192
|
+
months: dur.months ? -dur.months : undefined,
|
|
193
|
+
years: dur.years ? -dur.years : undefined,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
since(other: MiniPlainDate, _opts?: any): { days: number } {
|
|
198
|
+
const thisUTC = toUTC(this.year, this.month, this.day);
|
|
199
|
+
const otherUTC = toUTC(other.year, other.month, other.day);
|
|
200
|
+
return { days: Math.round((thisUTC - otherUTC) / 86400000) };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
until(other: MiniPlainDate, _opts?: any): { days: number } {
|
|
204
|
+
const thisUTC = toUTC(this.year, this.month, this.day);
|
|
205
|
+
const otherUTC = toUTC(other.year, other.month, other.day);
|
|
206
|
+
return { days: Math.round((otherUTC - thisUTC) / 86400000) };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
equals(other: MiniPlainDate): boolean {
|
|
210
|
+
return this.year === other.year && this.month === other.month && this.day === other.day;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
toString(): string {
|
|
214
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}-${pad2(this.day)}`;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
toZonedDateTime(tz: string | { timeZone: string }): MiniZonedDateTime {
|
|
218
|
+
const tzId = typeof tz === "string" ? tz : tz.timeZone;
|
|
219
|
+
return this.toPlainDateTime().toZonedDateTime(tzId);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
toPlainDateTime(timeLike?: {
|
|
223
|
+
hour?: number;
|
|
224
|
+
minute?: number;
|
|
225
|
+
second?: number;
|
|
226
|
+
}): MiniPlainDateTime {
|
|
227
|
+
return new MiniPlainDateTime(
|
|
228
|
+
this.year,
|
|
229
|
+
this.month,
|
|
230
|
+
this.day,
|
|
231
|
+
timeLike?.hour ?? 0,
|
|
232
|
+
timeLike?.minute ?? 0,
|
|
233
|
+
timeLike?.second ?? 0,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
toPlainMonthDay(): MiniPlainMonthDay {
|
|
238
|
+
return new MiniPlainMonthDay(this.month, this.day);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
toPlainYearMonth(): MiniPlainYearMonth {
|
|
242
|
+
return new MiniPlainYearMonth(this.year, this.month);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
toLocaleString(locale?: string | string[], options?: Intl.DateTimeFormatOptions): string {
|
|
246
|
+
return new Date(toUTC(this.year, this.month, this.day)).toLocaleDateString(locale, {
|
|
247
|
+
timeZone: "UTC",
|
|
248
|
+
...options,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
// MiniPlainDateTime
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
|
|
257
|
+
class MiniPlainDateTime {
|
|
258
|
+
readonly year: number;
|
|
259
|
+
readonly month: number;
|
|
260
|
+
readonly day: number;
|
|
261
|
+
readonly hour: number;
|
|
262
|
+
readonly minute: number;
|
|
263
|
+
readonly second: number;
|
|
264
|
+
|
|
265
|
+
constructor(year: number, month: number, day: number, hour = 0, minute = 0, second = 0) {
|
|
266
|
+
this.year = year;
|
|
267
|
+
this.month = month;
|
|
268
|
+
this.day = day;
|
|
269
|
+
this.hour = hour;
|
|
270
|
+
this.minute = minute;
|
|
271
|
+
this.second = second;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
get dayOfWeek(): number {
|
|
275
|
+
return isoWeekday(new Date(toUTC(this.year, this.month, this.day)));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
get daysInMonth(): number {
|
|
279
|
+
return daysInMonth(this.year, this.month);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
toZonedDateTime(tz: string | { timeZone: string }): MiniZonedDateTime {
|
|
283
|
+
const tzId = typeof tz === "string" ? tz : tz.timeZone;
|
|
284
|
+
const epoch = wallToEpoch(
|
|
285
|
+
this.year,
|
|
286
|
+
this.month,
|
|
287
|
+
this.day,
|
|
288
|
+
this.hour,
|
|
289
|
+
this.minute,
|
|
290
|
+
this.second,
|
|
291
|
+
tzId,
|
|
292
|
+
);
|
|
293
|
+
return new MiniZonedDateTime(epoch, tzId);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
toPlainDate(): MiniPlainDate {
|
|
297
|
+
return new MiniPlainDate(this.year, this.month, this.day);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
toString(): string {
|
|
301
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}-${pad2(this.day)}T${pad2(this.hour)}:${pad2(this.minute)}:${pad2(this.second)}`;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ---------------------------------------------------------------------------
|
|
306
|
+
// MiniPlainMonthDay
|
|
307
|
+
// ---------------------------------------------------------------------------
|
|
308
|
+
|
|
309
|
+
class MiniPlainMonthDay {
|
|
310
|
+
readonly monthCode: string;
|
|
311
|
+
readonly month: number;
|
|
312
|
+
readonly day: number;
|
|
313
|
+
|
|
314
|
+
constructor(month: number, day: number) {
|
|
315
|
+
this.month = month;
|
|
316
|
+
this.day = day;
|
|
317
|
+
this.monthCode = `M${pad2(month)}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
toPlainDate(fields: { year: number }): MiniPlainDate {
|
|
321
|
+
return new MiniPlainDate(fields.year, this.month, this.day);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
equals(other: MiniPlainMonthDay): boolean {
|
|
325
|
+
return this.month === other.month && this.day === other.day;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
toString(): string {
|
|
329
|
+
return `${pad2(this.month)}-${pad2(this.day)}`;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ---------------------------------------------------------------------------
|
|
334
|
+
// MiniPlainYearMonth
|
|
335
|
+
// ---------------------------------------------------------------------------
|
|
336
|
+
|
|
337
|
+
class MiniPlainYearMonth {
|
|
338
|
+
readonly year: number;
|
|
339
|
+
readonly month: number;
|
|
340
|
+
|
|
341
|
+
constructor(year: number, month: number) {
|
|
342
|
+
this.year = year;
|
|
343
|
+
this.month = month;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
get monthCode(): string {
|
|
347
|
+
return `M${pad2(this.month)}`;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
get daysInMonth(): number {
|
|
351
|
+
return daysInMonth(this.year, this.month);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
toPlainDate(fields: { day: number }): MiniPlainDate {
|
|
355
|
+
return new MiniPlainDate(this.year, this.month, fields.day);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
add(dur: { months?: number; years?: number }): MiniPlainYearMonth {
|
|
359
|
+
let y = this.year + (dur.years || 0);
|
|
360
|
+
let m = this.month + (dur.months || 0);
|
|
361
|
+
while (m > 12) {
|
|
362
|
+
m -= 12;
|
|
363
|
+
y += 1;
|
|
364
|
+
}
|
|
365
|
+
while (m < 1) {
|
|
366
|
+
m += 12;
|
|
367
|
+
y -= 1;
|
|
368
|
+
}
|
|
369
|
+
return new MiniPlainYearMonth(y, m);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
subtract(dur: { months?: number; years?: number }): MiniPlainYearMonth {
|
|
373
|
+
return this.add({
|
|
374
|
+
months: dur.months ? -dur.months : undefined,
|
|
375
|
+
years: dur.years ? -dur.years : undefined,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
equals(other: MiniPlainYearMonth): boolean {
|
|
380
|
+
return this.year === other.year && this.month === other.month;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
toString(): string {
|
|
384
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}`;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ---------------------------------------------------------------------------
|
|
389
|
+
// MiniZonedDateTime
|
|
390
|
+
// ---------------------------------------------------------------------------
|
|
391
|
+
|
|
392
|
+
class MiniZonedDateTime {
|
|
393
|
+
readonly epochMilliseconds: number;
|
|
394
|
+
readonly timeZoneId: string;
|
|
395
|
+
private _parts?: TzParts;
|
|
396
|
+
|
|
397
|
+
constructor(epochMs: number, timeZoneId: string) {
|
|
398
|
+
this.epochMilliseconds = epochMs;
|
|
399
|
+
this.timeZoneId = timeZoneId;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private get parts(): TzParts {
|
|
403
|
+
if (!this._parts) {
|
|
404
|
+
this._parts = extractTzParts(this.epochMilliseconds, this.timeZoneId);
|
|
405
|
+
}
|
|
406
|
+
return this._parts;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
get year(): number {
|
|
410
|
+
return this.parts.year;
|
|
411
|
+
}
|
|
412
|
+
get month(): number {
|
|
413
|
+
return this.parts.month;
|
|
414
|
+
}
|
|
415
|
+
get day(): number {
|
|
416
|
+
return this.parts.day;
|
|
417
|
+
}
|
|
418
|
+
get hour(): number {
|
|
419
|
+
return this.parts.hour;
|
|
420
|
+
}
|
|
421
|
+
get minute(): number {
|
|
422
|
+
return this.parts.minute;
|
|
423
|
+
}
|
|
424
|
+
get second(): number {
|
|
425
|
+
return this.parts.second;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
get dayOfWeek(): number {
|
|
429
|
+
return isoWeekday(new Date(toUTC(this.year, this.month, this.day)));
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
toPlainDate(): MiniPlainDate {
|
|
433
|
+
return new MiniPlainDate(this.year, this.month, this.day);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
toPlainDateTime(): MiniPlainDateTime {
|
|
437
|
+
return new MiniPlainDateTime(
|
|
438
|
+
this.year,
|
|
439
|
+
this.month,
|
|
440
|
+
this.day,
|
|
441
|
+
this.hour,
|
|
442
|
+
this.minute,
|
|
443
|
+
this.second,
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
toPlainTime(): { hour: number; minute: number; second: number } {
|
|
448
|
+
return { hour: this.hour, minute: this.minute, second: this.second };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
toPlainYearMonth(): MiniPlainYearMonth {
|
|
452
|
+
return new MiniPlainYearMonth(this.year, this.month);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
toString(): string {
|
|
456
|
+
return `${String(this.year).padStart(4, "0")}-${pad2(this.month)}-${pad2(this.day)}T${pad2(this.hour)}:${pad2(this.minute)}:${pad2(this.second)}[${this.timeZoneId}]`;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// ---------------------------------------------------------------------------
|
|
461
|
+
// Static constructors
|
|
462
|
+
// ---------------------------------------------------------------------------
|
|
463
|
+
|
|
464
|
+
const PlainDate = {
|
|
465
|
+
from(item: any, options?: { overflow?: string }): MiniPlainDate {
|
|
466
|
+
if (typeof item === "string") {
|
|
467
|
+
const { year, month, day } = parseDateISO(item);
|
|
468
|
+
if (options?.overflow === "constrain") {
|
|
469
|
+
return new MiniPlainDate(year, month, clamp(day, 1, daysInMonth(year, month)));
|
|
470
|
+
}
|
|
471
|
+
return new MiniPlainDate(year, month, day);
|
|
472
|
+
}
|
|
473
|
+
const year = item.year;
|
|
474
|
+
const month = item.month;
|
|
475
|
+
let day = item.day;
|
|
476
|
+
if (options?.overflow === "constrain") {
|
|
477
|
+
day = clamp(day, 1, daysInMonth(year, month));
|
|
478
|
+
}
|
|
479
|
+
return new MiniPlainDate(year, month, day);
|
|
480
|
+
},
|
|
481
|
+
|
|
482
|
+
compare(a: MiniPlainDate, b: MiniPlainDate): number {
|
|
483
|
+
if (a.year !== b.year) return Math.sign(a.year - b.year);
|
|
484
|
+
if (a.month !== b.month) return Math.sign(a.month - b.month);
|
|
485
|
+
if (a.day !== b.day) return Math.sign(a.day - b.day);
|
|
486
|
+
return 0;
|
|
487
|
+
},
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
const PlainDateTime = {
|
|
491
|
+
from(item: any, _options?: { overflow?: string }): MiniPlainDateTime {
|
|
492
|
+
if (typeof item === "string") {
|
|
493
|
+
const { year, month, day, hour, minute, second } = parseDateTimeISO(item);
|
|
494
|
+
return new MiniPlainDateTime(year, month, day, hour, minute, second);
|
|
495
|
+
}
|
|
496
|
+
return new MiniPlainDateTime(
|
|
497
|
+
item.year,
|
|
498
|
+
item.month,
|
|
499
|
+
item.day,
|
|
500
|
+
item.hour ?? 0,
|
|
501
|
+
item.minute ?? 0,
|
|
502
|
+
item.second ?? 0,
|
|
503
|
+
);
|
|
504
|
+
},
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
const PlainMonthDay = {
|
|
508
|
+
from(item: any): MiniPlainMonthDay {
|
|
509
|
+
if (typeof item === "string") {
|
|
510
|
+
// "MM-DD" or "YYYY-MM-DD" — take last two numeric segments
|
|
511
|
+
const parts = item.split("-").map(Number);
|
|
512
|
+
if (parts.length >= 3) return new MiniPlainMonthDay(parts[1], parts[2]);
|
|
513
|
+
return new MiniPlainMonthDay(parts[0], parts[1]);
|
|
514
|
+
}
|
|
515
|
+
const month = item.month ?? (item.monthCode ? Number.parseInt(item.monthCode.slice(1), 10) : 1);
|
|
516
|
+
return new MiniPlainMonthDay(month, item.day);
|
|
517
|
+
},
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
const PlainYearMonth = {
|
|
521
|
+
from(item: any): MiniPlainYearMonth {
|
|
522
|
+
if (typeof item === "string") {
|
|
523
|
+
const [y, m] = item.split("-").map(Number);
|
|
524
|
+
return new MiniPlainYearMonth(y, m);
|
|
525
|
+
}
|
|
526
|
+
return new MiniPlainYearMonth(item.year, item.month);
|
|
527
|
+
},
|
|
528
|
+
|
|
529
|
+
compare(a: MiniPlainYearMonth, b: MiniPlainYearMonth): number {
|
|
530
|
+
if (a.year !== b.year) return Math.sign(a.year - b.year);
|
|
531
|
+
if (a.month !== b.month) return Math.sign(a.month - b.month);
|
|
532
|
+
return 0;
|
|
533
|
+
},
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// ---------------------------------------------------------------------------
|
|
537
|
+
// Now
|
|
538
|
+
// ---------------------------------------------------------------------------
|
|
539
|
+
|
|
540
|
+
const Now = {
|
|
541
|
+
timeZoneId(): string {
|
|
542
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
543
|
+
},
|
|
544
|
+
|
|
545
|
+
zonedDateTimeISO(tz?: string): MiniZonedDateTime {
|
|
546
|
+
return new MiniZonedDateTime(Date.now(), tz ?? Now.timeZoneId());
|
|
547
|
+
},
|
|
548
|
+
|
|
549
|
+
plainDateISO(): MiniPlainDate {
|
|
550
|
+
const tz = Now.timeZoneId();
|
|
551
|
+
const parts = extractTzParts(Date.now(), tz);
|
|
552
|
+
return new MiniPlainDate(parts.year, parts.month, parts.day);
|
|
553
|
+
},
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// ---------------------------------------------------------------------------
|
|
557
|
+
// Export
|
|
558
|
+
// ---------------------------------------------------------------------------
|
|
559
|
+
|
|
560
|
+
export const Temporal = {
|
|
561
|
+
Now,
|
|
562
|
+
PlainDate,
|
|
563
|
+
PlainDateTime,
|
|
564
|
+
PlainMonthDay,
|
|
565
|
+
PlainYearMonth,
|
|
566
|
+
} as unknown as TemporalNamespace;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Temporal as FullTemporal } from "@js-temporal/polyfill";
|
|
2
|
+
import { Temporal as MiniTemporal } from "./temporal-polyfill";
|
|
3
|
+
import type { TemporalNamespace } from "./types";
|
|
4
|
+
|
|
5
|
+
export const temporalVariants: { name: string; T: TemporalNamespace }[] = [
|
|
6
|
+
{
|
|
7
|
+
name: "full polyfill",
|
|
8
|
+
T: {
|
|
9
|
+
Now: FullTemporal.Now,
|
|
10
|
+
PlainDate: FullTemporal.PlainDate,
|
|
11
|
+
PlainDateTime: FullTemporal.PlainDateTime,
|
|
12
|
+
PlainMonthDay: FullTemporal.PlainMonthDay,
|
|
13
|
+
PlainYearMonth: FullTemporal.PlainYearMonth,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
{ name: "mini polyfill", T: MiniTemporal },
|
|
17
|
+
];
|