@momoi-labs/kiso-react 0.22.1 → 0.24.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.
@@ -0,0 +1,121 @@
1
+ // Time zone helpers built on Intl only. Values stay epoch milliseconds; wall
2
+ // strings use the datetime-local shape YYYY-MM-DDTHH:mm:ss(.sss).
3
+ const formatters = new Map();
4
+ export function dateFormat(locale, options) {
5
+ const key = JSON.stringify([locale ?? "", options]);
6
+ let format = formatters.get(key);
7
+ if (!format)
8
+ formatters.set(key, format = new Intl.DateTimeFormat(locale, options));
9
+ return format;
10
+ }
11
+ export const browserTimeZone = () => {
12
+ try {
13
+ return new Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
14
+ }
15
+ catch {
16
+ return "UTC";
17
+ }
18
+ };
19
+ export const validTimeZone = (zone) => {
20
+ if (typeof zone !== "string" || !zone)
21
+ return false;
22
+ try {
23
+ dateFormat("en-US", { timeZone: zone });
24
+ return true;
25
+ }
26
+ catch {
27
+ return false;
28
+ }
29
+ };
30
+ export const validInstant = (value) => Number.isFinite(value) && Math.abs(value) <= 8.64e15;
31
+ export function timeZones() {
32
+ const zones = typeof Intl.supportedValuesOf === "function" ? Intl.supportedValuesOf("timeZone") : [];
33
+ return zones.includes("UTC") ? zones : [...zones, "UTC"];
34
+ }
35
+ const pad = (value, length = 2) => String(value).padStart(length, "0");
36
+ function wallParts(value, zone) {
37
+ const parts = {};
38
+ for (const part of dateFormat("en-US", { timeZone: zone, hourCycle: "h23", year: "numeric", month: "2-digit",
39
+ day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", era: "short" }).formatToParts(value)) {
40
+ parts[part.type] = part.value;
41
+ }
42
+ const year = Number(parts.year);
43
+ return { year: parts.era === "BC" || parts.era === "B" ? 1 - year : year, month: Number(parts.month),
44
+ day: Number(parts.day), hour: Number(parts.hour) % 24, minute: Number(parts.minute), second: Number(parts.second),
45
+ millisecond: ((value % 1000) + 1000) % 1000 };
46
+ }
47
+ const utc = (year, month, day, hour, minute, second, ms) => {
48
+ const date = new Date(0);
49
+ date.setUTCFullYear(year, month - 1, day);
50
+ date.setUTCHours(hour, minute, second, ms);
51
+ return date.getTime();
52
+ };
53
+ /** The zone's offset from UTC at an instant, in milliseconds. */
54
+ export function zoneOffset(value, zone) {
55
+ const wall = wallParts(value, zone);
56
+ return utc(wall.year, wall.month, wall.day, wall.hour, wall.minute, wall.second, wall.millisecond) - value;
57
+ }
58
+ /** Wall-clock time of an instant in a zone, as a datetime-local value. */
59
+ export function wallString(value, zone, precision) {
60
+ if (zone === "UTC" && precision === "millisecond")
61
+ return new Date(value).toISOString().slice(0, 23);
62
+ const wall = wallParts(value, zone);
63
+ const year = wall.year < 0 ? `-${pad(-wall.year, 6)}` : wall.year > 9999 ? `+${pad(wall.year, 6)}` : pad(wall.year, 4);
64
+ const text = `${year}-${pad(wall.month)}-${pad(wall.day)}T${pad(wall.hour)}:${pad(wall.minute)}:${pad(wall.second)}`;
65
+ return precision === "millisecond" ? `${text}.${pad(wall.millisecond, 3)}` : text;
66
+ }
67
+ /** Instant of a datetime-local wall time in a zone. A wall time inside a
68
+ daylight-saving gap moves forward to the first valid instant; one inside an
69
+ overlap takes the earlier instant. Returns NaN for unparsable input. */
70
+ export function parseWallTime(text, zone) {
71
+ try {
72
+ return wallInstant(text, zone);
73
+ }
74
+ catch {
75
+ return NaN;
76
+ }
77
+ }
78
+ function wallInstant(text, zone) {
79
+ const match = /^([+-]\d{6}|\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?$/.exec(text);
80
+ if (!match)
81
+ return NaN;
82
+ const [year, month, day, hour, minute, second = 0] = match.slice(1, 7).map(part => Number(part ?? 0));
83
+ const ms = Number((match[7] ?? "").padEnd(3, "0"));
84
+ const wall = utc(year, month, day, hour, minute, second, ms);
85
+ if (!validInstant(wall) || month < 1 || month > 12 || day < 1 || day > 31 || hour > 23 || minute > 59 || second > 59)
86
+ return NaN;
87
+ if (zone === "UTC")
88
+ return wall;
89
+ const oneDay = 86_400_000;
90
+ const before = zoneOffset(wall - oneDay, zone);
91
+ const after = zoneOffset(wall + oneDay, zone);
92
+ const same = (value) => {
93
+ const parts = wallParts(value, zone);
94
+ return utc(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond) === wall;
95
+ };
96
+ const candidates = [...new Set([wall - before, wall - after])].filter(same);
97
+ if (candidates.length)
98
+ return Math.min(...candidates);
99
+ if (before === after)
100
+ return NaN;
101
+ // In a gap: find the transition instant between both readings.
102
+ let low = Math.min(wall - before, wall - after);
103
+ let high = Math.max(wall - before, wall - after);
104
+ const target = zoneOffset(high, zone);
105
+ while (high - low > 1) {
106
+ const middle = Math.floor((low + high) / 2);
107
+ if (zoneOffset(middle, zone) === target)
108
+ high = middle;
109
+ else
110
+ low = middle;
111
+ }
112
+ return high;
113
+ }
114
+ /** A short zone name at an instant, such as "UTC" or "GMT-3". */
115
+ export function zoneName(value, zone, locale) {
116
+ if (zone === "UTC")
117
+ return "UTC";
118
+ const at = validInstant(value) ? value : Date.now();
119
+ return dateFormat(locale, { timeZone: zone, timeZoneName: "short" }).formatToParts(at)
120
+ .find(part => part.type === "timeZoneName")?.value ?? zone;
121
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momoi-labs/kiso-react",
3
- "version": "0.22.1",
3
+ "version": "0.24.0",
4
4
  "description": "Kiso React components built with shadcn/ui and Radix",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "prepack": "npm run build"
33
33
  },
34
34
  "dependencies": {
35
- "@momoi-labs/kiso": "^1.0.1",
35
+ "@momoi-labs/kiso": "^1.2.0",
36
36
  "class-variance-authority": "0.7.1",
37
37
  "clsx": "2.1.1",
38
38
  "radix-ui": "1.6.7",