@super-calendar/core 2.17.0 → 2.19.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/README.md +8 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +17 -10
- package/dist/index.mjs +9 -11
- package/dist/{recurrence-CvIJfSMl.mjs → recurrence-BqpCShcV.mjs} +402 -101
- package/dist/{recurrence-D_oJ7A9D.js → recurrence-CTjr6nxJ.js} +454 -99
- package/dist/{timezone-vbd68uRS.d.mts → timezone-DhGhYIeB.d.mts} +150 -13
- package/dist/{timezone-vbd68uRS.d.ts → timezone-DhGhYIeB.d.ts} +150 -13
- package/dist/vanilla.d.mts +2 -2
- package/dist/vanilla.d.ts +2 -2
- package/dist/vanilla.js +10 -1
- package/dist/vanilla.mjs +1 -1
- package/package.json +1 -1
- package/src/types.ts +7 -0
- package/src/utils/dates.ts +7 -2
- package/src/utils/drag.ts +14 -6
- package/src/utils/eventSource.ts +2 -0
- package/src/utils/ical.ts +5 -0
- package/src/utils/layout.ts +19 -5
- package/src/utils/monthGrid.ts +135 -1
- package/src/utils/multiWeekGrid.ts +2 -1
- package/src/utils/recurrence.ts +203 -77
- package/src/utils/timezone.ts +151 -48
- package/src/utils/useMonthGrid.ts +5 -0
- package/src/utils/useNow.ts +4 -9
- package/src/utils/zoneFields.ts +71 -0
package/src/utils/useNow.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useState } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { nowInTimeZone } from "./timezone";
|
|
3
3
|
|
|
4
4
|
/** Options for {@link useNow}. */
|
|
5
5
|
export interface UseNowOptions {
|
|
@@ -20,11 +20,6 @@ export interface UseNowOptions {
|
|
|
20
20
|
|
|
21
21
|
const MINUTE_MS = 60_000;
|
|
22
22
|
|
|
23
|
-
const resolveNow = (now?: Date, timeZone?: string): Date => {
|
|
24
|
-
const instant = now ?? new Date();
|
|
25
|
-
return timeZone ? toZonedTime(instant, timeZone) : instant;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
23
|
/**
|
|
29
24
|
* The current wall-clock time, re-read every `tickMs` while `enabled`, shifted
|
|
30
25
|
* into `timeZone` when given, or pinned to a fixed `now` override. Drives the
|
|
@@ -34,13 +29,13 @@ export function useNow(
|
|
|
34
29
|
enabled: boolean,
|
|
35
30
|
{ now, timeZone, tickMs = MINUTE_MS }: UseNowOptions = {},
|
|
36
31
|
): Date {
|
|
37
|
-
const [value, setValue] = useState(() =>
|
|
32
|
+
const [value, setValue] = useState(() => nowInTimeZone(now, timeZone));
|
|
38
33
|
const nowMs = now?.getTime();
|
|
39
34
|
useEffect(() => {
|
|
40
35
|
if (!enabled) return;
|
|
41
|
-
setValue(
|
|
36
|
+
setValue(nowInTimeZone(nowMs != null ? new Date(nowMs) : undefined, timeZone));
|
|
42
37
|
if (nowMs != null) return; // a fixed instant doesn't tick
|
|
43
|
-
const id = setInterval(() => setValue(
|
|
38
|
+
const id = setInterval(() => setValue(nowInTimeZone(undefined, timeZone)), tickMs);
|
|
44
39
|
return () => clearInterval(id);
|
|
45
40
|
}, [enabled, nowMs, timeZone, tickMs]);
|
|
46
41
|
return value;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Internal: read an instant's calendar fields in an IANA time zone. Kept apart
|
|
2
|
+
// from `timezone.ts`, whose exports are public API.
|
|
3
|
+
|
|
4
|
+
/** Calendar fields: year, month (0 = January), day, hour, minute, second. */
|
|
5
|
+
export type ZoneFields = [number, number, number, number, number, number];
|
|
6
|
+
|
|
7
|
+
// Building a formatter costs far more than formatting with it, and the views
|
|
8
|
+
// convert once per event and per rendered day, so keep one per zone. Zone names
|
|
9
|
+
// can come from untrusted input (an iCal `TZID`) and match in any letter case,
|
|
10
|
+
// so the cache is bounded: an app uses a handful of zones.
|
|
11
|
+
const MAX_ZONE_FORMATTERS = 32;
|
|
12
|
+
const zoneFormatters = new Map<string, Intl.DateTimeFormat>();
|
|
13
|
+
|
|
14
|
+
function zoneFormatter(timeZone: string): Intl.DateTimeFormat {
|
|
15
|
+
let formatter = zoneFormatters.get(timeZone);
|
|
16
|
+
if (!formatter) {
|
|
17
|
+
formatter = new Intl.DateTimeFormat("en-US", {
|
|
18
|
+
timeZone,
|
|
19
|
+
hourCycle: "h23",
|
|
20
|
+
year: "numeric",
|
|
21
|
+
month: "2-digit",
|
|
22
|
+
day: "2-digit",
|
|
23
|
+
hour: "2-digit",
|
|
24
|
+
minute: "2-digit",
|
|
25
|
+
second: "2-digit",
|
|
26
|
+
});
|
|
27
|
+
if (zoneFormatters.size >= MAX_ZONE_FORMATTERS) zoneFormatters.clear();
|
|
28
|
+
zoneFormatters.set(timeZone, formatter);
|
|
29
|
+
}
|
|
30
|
+
return formatter;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The wall-clock fields `date` reads in `timeZone`, as plain numbers. Unlike a
|
|
35
|
+
* device-local `Date` holding those fields, the result can't be bent by the
|
|
36
|
+
* device's own DST changes.
|
|
37
|
+
*/
|
|
38
|
+
export function zoneFields(date: Date, timeZone: string): ZoneFields {
|
|
39
|
+
const parts = zoneFormatter(timeZone).formatToParts(date);
|
|
40
|
+
const field = (type: Intl.DateTimeFormatPartTypes): number => {
|
|
41
|
+
const part = parts.find((p) => p.type === type);
|
|
42
|
+
return part ? Number(part.value) : 0;
|
|
43
|
+
};
|
|
44
|
+
// `h23` can report midnight as 24; normalise to 0.
|
|
45
|
+
return [
|
|
46
|
+
field("year"),
|
|
47
|
+
field("month") - 1,
|
|
48
|
+
field("day"),
|
|
49
|
+
field("hour") % 24,
|
|
50
|
+
field("minute"),
|
|
51
|
+
field("second"),
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A calendar reading as a UTC time value. Fields may overflow and roll over.
|
|
57
|
+
* Setters, not `Date.UTC`, which reads a year below 100 as 19xx.
|
|
58
|
+
*/
|
|
59
|
+
export function utcTime(
|
|
60
|
+
year: number,
|
|
61
|
+
month: number,
|
|
62
|
+
day: number,
|
|
63
|
+
hour = 0,
|
|
64
|
+
minute = 0,
|
|
65
|
+
second = 0,
|
|
66
|
+
ms = 0,
|
|
67
|
+
): number {
|
|
68
|
+
const date = new Date(0);
|
|
69
|
+
date.setUTCFullYear(year, month, day);
|
|
70
|
+
return date.setUTCHours(hour, minute, second, ms);
|
|
71
|
+
}
|