@terpjs/react-core 0.38.0 → 0.40.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/package.json +2 -2
- package/src/ui/DateTimeInput.test.tsx +18 -0
- package/src/ui/DateTimeInput.tsx +32 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terpjs/react-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Terp React stack core — typed @terpjs/contract client provider, auth session, capability gates, TanStack Router adapter, app shell, page archetypes, DataView and token-styled UI primitives. First frontend stack; see README.md for the component catalog.",
|
|
6
6
|
"exports": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@tanstack/react-router": "^1.170.46",
|
|
18
|
-
"@terpjs/contract": "^0.
|
|
18
|
+
"@terpjs/contract": "^0.40.0"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"react": "^19.0.0",
|
|
@@ -66,6 +66,24 @@ describe("DateTimeInput", () => {
|
|
|
66
66
|
expect(onChange).toHaveBeenLastCalledWith("2026-11-06T10:00:00-03:30");
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
+
it("keeps a year below 100 in its own century, as one is retyped", () => {
|
|
70
|
+
// Retyping 2026 passes through 0002 and 0020; Date.UTC read those as 1902 and 1920.
|
|
71
|
+
const onChange = vi.fn();
|
|
72
|
+
render(<DateTimeInput aria-label="Starts" value={null} onChange={onChange} zone="UTC" />);
|
|
73
|
+
pick(screen.getByLabelText("Starts"), "0020-11-06T10:00");
|
|
74
|
+
expect(onChange).toHaveBeenLastCalledWith("0020-11-06T10:00:00Z");
|
|
75
|
+
pick(screen.getByLabelText("Starts"), "0002-03-01T00:00");
|
|
76
|
+
expect(onChange).toHaveBeenLastCalledWith("0002-03-01T00:00:00Z");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("writes an instant in UTC where the zone then ran by seconds, never moving it", () => {
|
|
80
|
+
// Monrovia ran at -0:44:30 until 1972, which an RFC 3339 offset cannot write.
|
|
81
|
+
const onChange = vi.fn();
|
|
82
|
+
render(<DateTimeInput aria-label="Starts" value={null} onChange={onChange} zone="Africa/Monrovia" />);
|
|
83
|
+
pick(screen.getByLabelText("Starts"), "1971-06-01T12:00");
|
|
84
|
+
expect(onChange).toHaveBeenLastCalledWith("1971-06-01T12:44:30Z");
|
|
85
|
+
});
|
|
86
|
+
|
|
69
87
|
it("reads in the browser's own zone when it is given none", () => {
|
|
70
88
|
// The same instant the browser's own dates name for that wall time, whatever zone this
|
|
71
89
|
// test runs in.
|
package/src/ui/DateTimeInput.tsx
CHANGED
|
@@ -131,11 +131,18 @@ function fieldsIn(instant: number, zone: string): Fields {
|
|
|
131
131
|
};
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
/**
|
|
135
|
-
function
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
134
|
+
/** The UTC instant of a calendar date and wall time, in any year: 0 to 99 included. */
|
|
135
|
+
function utcMillis(fields: Fields): number {
|
|
136
|
+
// Date.UTC reads a year from 0 to 99 as 1900 plus it, which turns a year being retyped
|
|
137
|
+
// into another century, so the year is set on its own.
|
|
138
|
+
const date = new Date(Date.UTC(2000, 0, 1, fields.hour, fields.minute, fields.second));
|
|
139
|
+
date.setUTCFullYear(fields.year, fields.month - 1, fields.day);
|
|
140
|
+
return date.getTime();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** How far *zone* runs ahead of UTC at *instant*, in milliseconds: a zone once ran by seconds. */
|
|
144
|
+
function offsetMillis(instant: number, zone: string): number {
|
|
145
|
+
return utcMillis(fieldsIn(instant, zone)) - Math.floor(instant / 1000) * 1000;
|
|
139
146
|
}
|
|
140
147
|
|
|
141
148
|
function pad(value: number, width = 2): string {
|
|
@@ -162,24 +169,35 @@ function instantOf(wall: string, zone: string): string | null {
|
|
|
162
169
|
return null;
|
|
163
170
|
}
|
|
164
171
|
const [, year, month, day, hour, minute, second] = match;
|
|
165
|
-
const local =
|
|
172
|
+
const local = utcMillis({
|
|
173
|
+
year: +year,
|
|
174
|
+
month: +month,
|
|
175
|
+
day: +day,
|
|
176
|
+
hour: +hour,
|
|
177
|
+
minute: +minute,
|
|
178
|
+
second: second === undefined ? 0 : +second,
|
|
179
|
+
});
|
|
166
180
|
// The offsets on either side of any change of the clocks near this wall time; each is a
|
|
167
181
|
// candidate, and it names this wall time if the zone runs at that offset at that instant.
|
|
168
|
-
const before =
|
|
169
|
-
const after =
|
|
182
|
+
const before = offsetMillis(local - DAY, zone);
|
|
183
|
+
const after = offsetMillis(local + DAY, zone);
|
|
170
184
|
const fits = [before, after]
|
|
171
|
-
.map((offset) => local - offset
|
|
172
|
-
.filter((candidate) =>
|
|
185
|
+
.map((offset) => local - offset)
|
|
186
|
+
.filter((candidate) => offsetMillis(candidate, zone) === local - candidate);
|
|
173
187
|
// Repeated, both fit and the earlier is taken; skipped, none fits and the offset before the
|
|
174
188
|
// gap carries the wall time past it.
|
|
175
|
-
const instant = fits.length > 0 ? Math.min(...fits) : local - before
|
|
189
|
+
const instant = fits.length > 0 ? Math.min(...fits) : local - before;
|
|
176
190
|
return rfc3339(instant, zone);
|
|
177
191
|
}
|
|
178
192
|
|
|
179
|
-
/** *instant* as an RFC 3339 date and time in *zone*, with the zone's offset at that instant.
|
|
193
|
+
/** *instant* as an RFC 3339 date and time in *zone*, with the zone's offset at that instant.
|
|
194
|
+
*
|
|
195
|
+
* RFC 3339 writes an offset in whole minutes. Where the zone then ran by seconds (Monrovia,
|
|
196
|
+
* until 1972), the same instant is written in UTC instead, so it is never moved. */
|
|
180
197
|
function rfc3339(instant: number, zone: string): string {
|
|
181
|
-
const
|
|
182
|
-
const
|
|
198
|
+
const exact = offsetMillis(instant, zone) % MINUTE === 0;
|
|
199
|
+
const fields = fieldsIn(instant, exact ? zone : "UTC");
|
|
200
|
+
const offset = exact ? offsetMillis(instant, zone) / MINUTE : 0;
|
|
183
201
|
const magnitude = Math.abs(offset);
|
|
184
202
|
const suffix =
|
|
185
203
|
offset === 0
|