@terpjs/react-core 0.37.0 → 0.39.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 +1 -0
- package/package.json +2 -2
- package/src/index.ts +2 -0
- package/src/ui/DateTimeInput.test.tsx +138 -0
- package/src/ui/DateTimeInput.tsx +210 -0
package/README.md
CHANGED
|
@@ -222,6 +222,7 @@ marker, counted by the escape-hatch budget.
|
|
|
222
222
|
| `Select` options (`SelectOption<T>`) | Two forms. Pass `options={[{ value, label, disabled? }]}` with `onValueChange` when the choices are data — `T` is inferred from the list, so a closed enum is checked at both ends and the `event.target.value as Status` cast goes away; `placeholder` renders the disabled empty-valued leading row. Or pass `<option>` children as before. The two are mutually exclusive at the type level, so neither can silently ignore the other. |
|
|
223
223
|
| `Combobox` | Accessible autocomplete/typeahead single-select: filterable options, controlled or uncontrolled value, loading state, disabled state, and ARIA combobox/listbox keyboard navigation. |
|
|
224
224
|
| `DatePicker`, `DateRangePicker` | Locale-aware calendar popover controls with keyboard-navigable month grids, min/max bounds, and range selection for ERP date filters. |
|
|
225
|
+
| `DateTimeInput` | A date and time a person picks, sent as the instant it names: it shows an RFC 3339 instant as wall time in a zone (the browser's own, or `zone="Europe/Amsterdam"`) and sends what is picked as an instant with that zone's offset, which the API's timestamp fields require (ADR 0206). A wall time the clocks skip moves past the gap; one they repeat is the earlier. `Input type="datetime-local"` is lint-refused, since its value carries no offset. |
|
|
225
226
|
| `Checkbox` | Labelled checkbox with `checked` / `defaultChecked` and boolean `onChange`. |
|
|
226
227
|
| `Radio`, `RadioGroup` | Labelled radio and accessible grouped radio options with controlled or uncontrolled value. |
|
|
227
228
|
| `Switch` | Labelled boolean toggle (`role="switch"`) with `checked` / `defaultChecked` and boolean `onChange`. |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terpjs/react-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.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.39.0"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"react": "^19.0.0",
|
package/src/index.ts
CHANGED
|
@@ -129,6 +129,8 @@ export { Button } from "./ui/Button";
|
|
|
129
129
|
export type { ButtonProps, ButtonSize, ButtonVariant } from "./ui/Button";
|
|
130
130
|
export { Input } from "./ui/Input";
|
|
131
131
|
export type { InputProps } from "./ui/Input";
|
|
132
|
+
export { DateTimeInput } from "./ui/DateTimeInput";
|
|
133
|
+
export type { DateTimeInputProps } from "./ui/DateTimeInput";
|
|
132
134
|
export { Select } from "./ui/Select";
|
|
133
135
|
export type { SelectProps, SelectOption } from "./ui/Select";
|
|
134
136
|
export { Textarea } from "./ui/Textarea";
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { Field } from "../Field";
|
|
6
|
+
import { DateTimeInput } from "./DateTimeInput";
|
|
7
|
+
|
|
8
|
+
afterEach(cleanup);
|
|
9
|
+
|
|
10
|
+
function pick(input: HTMLElement, wall: string) {
|
|
11
|
+
fireEvent.change(input, { target: { value: wall } });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe("DateTimeInput", () => {
|
|
15
|
+
it("shows an instant as the wall time it reads as in the zone", () => {
|
|
16
|
+
render(<DateTimeInput aria-label="Starts" value="2026-11-06T09:00:00Z" zone="Europe/Amsterdam" />);
|
|
17
|
+
const input = screen.getByLabelText("Starts");
|
|
18
|
+
expect(input).toHaveAttribute("type", "datetime-local");
|
|
19
|
+
expect(input).toHaveAttribute("data-terp", "input");
|
|
20
|
+
expect(input).toHaveValue("2026-11-06T10:00");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("sends what is picked as the instant it names, with the zone's offset", () => {
|
|
24
|
+
// The point of the control: a datetime-local value carries no offset, which the server
|
|
25
|
+
// refuses (ADR 0206). What leaves here always names an instant.
|
|
26
|
+
const onChange = vi.fn();
|
|
27
|
+
render(<DateTimeInput aria-label="Starts" value={null} onChange={onChange} zone="Europe/Amsterdam" />);
|
|
28
|
+
pick(screen.getByLabelText("Starts"), "2026-11-06T10:00");
|
|
29
|
+
expect(onChange).toHaveBeenLastCalledWith("2026-11-06T10:00:00+01:00");
|
|
30
|
+
pick(screen.getByLabelText("Starts"), "2026-07-01T10:00");
|
|
31
|
+
expect(onChange).toHaveBeenLastCalledWith("2026-07-01T10:00:00+02:00");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("sends null once the field is cleared", () => {
|
|
35
|
+
const onChange = vi.fn();
|
|
36
|
+
render(<DateTimeInput aria-label="Starts" value="2026-11-06T09:00:00Z" onChange={onChange} zone="UTC" />);
|
|
37
|
+
pick(screen.getByLabelText("Starts"), "");
|
|
38
|
+
expect(onChange).toHaveBeenLastCalledWith(null);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("moves a wall time the clocks skip forward past the gap", () => {
|
|
42
|
+
// 29 March 2026: Amsterdam goes from 02:00 to 03:00, so 02:30 never happens there.
|
|
43
|
+
const onChange = vi.fn();
|
|
44
|
+
render(<DateTimeInput aria-label="Starts" value={null} onChange={onChange} zone="Europe/Amsterdam" />);
|
|
45
|
+
pick(screen.getByLabelText("Starts"), "2026-03-29T02:30");
|
|
46
|
+
expect(onChange).toHaveBeenLastCalledWith("2026-03-29T03:30:00+02:00");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("takes the earlier of a wall time the clocks repeat", () => {
|
|
50
|
+
// 25 October 2026: Amsterdam goes from 03:00 back to 02:00, so 02:30 happens twice.
|
|
51
|
+
const onChange = vi.fn();
|
|
52
|
+
render(<DateTimeInput aria-label="Starts" value={null} onChange={onChange} zone="Europe/Amsterdam" />);
|
|
53
|
+
pick(screen.getByLabelText("Starts"), "2026-10-25T02:30");
|
|
54
|
+
expect(onChange).toHaveBeenLastCalledWith("2026-10-25T02:30:00+02:00");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("writes UTC with its Z, and a zone behind UTC with a negative offset", () => {
|
|
58
|
+
const onChange = vi.fn();
|
|
59
|
+
const { rerender } = render(
|
|
60
|
+
<DateTimeInput aria-label="Starts" value={null} onChange={onChange} zone="UTC" />,
|
|
61
|
+
);
|
|
62
|
+
pick(screen.getByLabelText("Starts"), "2026-11-06T10:00");
|
|
63
|
+
expect(onChange).toHaveBeenLastCalledWith("2026-11-06T10:00:00Z");
|
|
64
|
+
rerender(<DateTimeInput aria-label="Starts" value={null} onChange={onChange} zone="America/St_Johns" />);
|
|
65
|
+
pick(screen.getByLabelText("Starts"), "2026-11-06T10:00");
|
|
66
|
+
expect(onChange).toHaveBeenLastCalledWith("2026-11-06T10:00:00-03:30");
|
|
67
|
+
});
|
|
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
|
+
|
|
87
|
+
it("reads in the browser's own zone when it is given none", () => {
|
|
88
|
+
// The same instant the browser's own dates name for that wall time, whatever zone this
|
|
89
|
+
// test runs in.
|
|
90
|
+
const onChange = vi.fn();
|
|
91
|
+
render(<DateTimeInput aria-label="Starts" value={null} onChange={onChange} />);
|
|
92
|
+
pick(screen.getByLabelText("Starts"), "2026-11-06T10:00");
|
|
93
|
+
const sent = onChange.mock.lastCall?.[0] as string;
|
|
94
|
+
expect(Date.parse(sent)).toBe(new Date(2026, 10, 6, 10, 0).getTime());
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("shows a value without an offset as empty, since it names no instant", () => {
|
|
98
|
+
render(<DateTimeInput aria-label="Starts" value="2026-11-06T10:00:00" zone="UTC" />);
|
|
99
|
+
expect(screen.getByLabelText("Starts")).toHaveValue("");
|
|
100
|
+
cleanup();
|
|
101
|
+
render(<DateTimeInput aria-label="Starts" value="not a date+01:00" zone="UTC" />);
|
|
102
|
+
expect(screen.getByLabelText("Starts")).toHaveValue("");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("bounds the picker by instants, shown in the zone", () => {
|
|
106
|
+
render(
|
|
107
|
+
<DateTimeInput
|
|
108
|
+
aria-label="Starts"
|
|
109
|
+
value={null}
|
|
110
|
+
zone="Europe/Amsterdam"
|
|
111
|
+
min="2026-11-01T00:00:00Z"
|
|
112
|
+
max="2026-11-30T23:00:00Z"
|
|
113
|
+
/>,
|
|
114
|
+
);
|
|
115
|
+
const input = screen.getByLabelText("Starts");
|
|
116
|
+
expect(input).toHaveAttribute("min", "2026-11-01T01:00");
|
|
117
|
+
expect(input).toHaveAttribute("max", "2026-12-01T00:00");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("keeps what is picked itself when no value is given", () => {
|
|
121
|
+
render(<DateTimeInput aria-label="Starts" defaultValue="2026-11-06T09:00:00Z" zone="UTC" />);
|
|
122
|
+
const input = screen.getByLabelText("Starts");
|
|
123
|
+
expect(input).toHaveValue("2026-11-06T09:00");
|
|
124
|
+
pick(input, "2026-11-07T08:15");
|
|
125
|
+
expect(input).toHaveValue("2026-11-07T08:15");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("takes the label, hint and error a Field gives its control", () => {
|
|
129
|
+
render(
|
|
130
|
+
<Field label="Starts" hint="In Amsterdam time" error="Pick a time">
|
|
131
|
+
<DateTimeInput value={null} zone="Europe/Amsterdam" />
|
|
132
|
+
</Field>,
|
|
133
|
+
);
|
|
134
|
+
const input = screen.getByLabelText("Starts");
|
|
135
|
+
expect(input).toHaveAttribute("aria-invalid", "true");
|
|
136
|
+
expect(input).toHaveAccessibleDescription(/In Amsterdam time/);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import type { InputHTMLAttributes } from "react";
|
|
3
|
+
|
|
4
|
+
import { injectTerpStyles } from "../styles";
|
|
5
|
+
|
|
6
|
+
injectTerpStyles();
|
|
7
|
+
|
|
8
|
+
type NativeProps = Omit<
|
|
9
|
+
InputHTMLAttributes<HTMLInputElement>,
|
|
10
|
+
"type" | "value" | "defaultValue" | "onChange" | "min" | "max"
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
export interface DateTimeInputProps extends NativeProps {
|
|
14
|
+
/**
|
|
15
|
+
* The instant shown, as the API sends one: an RFC 3339 date and time with its UTC offset
|
|
16
|
+
* (`2026-11-06T09:00:00Z`), or `null` for none. A value without an offset names no instant,
|
|
17
|
+
* so it is shown as empty rather than guessed at.
|
|
18
|
+
*/
|
|
19
|
+
value?: string | null;
|
|
20
|
+
/** The instant shown first, uncontrolled. */
|
|
21
|
+
defaultValue?: string | null;
|
|
22
|
+
/**
|
|
23
|
+
* The instant picked, as an RFC 3339 date and time with the zone's offset
|
|
24
|
+
* (`2026-11-06T10:00:00+01:00`), ready to send; `null` once the field is cleared.
|
|
25
|
+
*/
|
|
26
|
+
onChange?: (value: string | null) => void;
|
|
27
|
+
/**
|
|
28
|
+
* The IANA time zone the time is shown and read in (`Europe/Amsterdam`); the browser's own
|
|
29
|
+
* when absent. When it is not the person's own zone, say which it is in the field's hint.
|
|
30
|
+
*/
|
|
31
|
+
zone?: string;
|
|
32
|
+
/** The earliest instant that may be picked, as `value` takes one. */
|
|
33
|
+
min?: string;
|
|
34
|
+
/** The latest instant that may be picked, as `value` takes one. */
|
|
35
|
+
max?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A date and time a person picks, sent as the instant it names (ADR 0206).
|
|
40
|
+
*
|
|
41
|
+
* A `datetime-local` input's own value is a wall time with no UTC offset, which names no
|
|
42
|
+
* instant, and the server refuses one. This shows the instant as a wall time in a zone and
|
|
43
|
+
* sends what the person picks back as an instant with that zone's offset. A wall time the
|
|
44
|
+
* clocks skip moves forward past the gap, and one they repeat is the earlier of the two, the
|
|
45
|
+
* choice JavaScript's own dates and Temporal make. The control is the browser's, so it is
|
|
46
|
+
* localised by the browser, and the `data-terp="input"` marker styles it like every input.
|
|
47
|
+
*/
|
|
48
|
+
export function DateTimeInput({
|
|
49
|
+
value,
|
|
50
|
+
defaultValue = null,
|
|
51
|
+
onChange,
|
|
52
|
+
zone,
|
|
53
|
+
min,
|
|
54
|
+
max,
|
|
55
|
+
...rest
|
|
56
|
+
}: DateTimeInputProps) {
|
|
57
|
+
const timeZone = zone ?? browserZone();
|
|
58
|
+
const [uncontrolled, setUncontrolled] = useState<string | null>(defaultValue);
|
|
59
|
+
const shown = value === undefined ? uncontrolled : value;
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<input
|
|
63
|
+
{...rest}
|
|
64
|
+
data-terp="input"
|
|
65
|
+
type="datetime-local"
|
|
66
|
+
value={wallTime(shown, timeZone)}
|
|
67
|
+
min={min === undefined ? undefined : wallTime(min, timeZone) || undefined}
|
|
68
|
+
max={max === undefined ? undefined : wallTime(max, timeZone) || undefined}
|
|
69
|
+
onChange={(event) => {
|
|
70
|
+
const next = instantOf(event.target.value, timeZone);
|
|
71
|
+
if (value === undefined) {
|
|
72
|
+
setUncontrolled(next);
|
|
73
|
+
}
|
|
74
|
+
onChange?.(next);
|
|
75
|
+
}}
|
|
76
|
+
/>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The zone the browser runs in, which is the person's own unless they set it otherwise. The
|
|
82
|
+
* locale is named only because the constructor asks for one: the zone it resolves does not
|
|
83
|
+
* depend on it, and the parts read below are numbers, which this locale writes in plain digits.
|
|
84
|
+
*/
|
|
85
|
+
function browserZone(): string {
|
|
86
|
+
return new Intl.DateTimeFormat("en-US").resolvedOptions().timeZone ?? "UTC";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const MINUTE = 60_000;
|
|
90
|
+
const DAY = 86_400_000;
|
|
91
|
+
const WITH_OFFSET = /(?:Z|[+-]\d{2}:\d{2})$/i;
|
|
92
|
+
const WALL_TIME = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?/;
|
|
93
|
+
|
|
94
|
+
const formatters = new Map<string, Intl.DateTimeFormat>();
|
|
95
|
+
|
|
96
|
+
interface Fields {
|
|
97
|
+
year: number;
|
|
98
|
+
month: number;
|
|
99
|
+
day: number;
|
|
100
|
+
hour: number;
|
|
101
|
+
minute: number;
|
|
102
|
+
second: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The calendar date and wall time *instant* reads as in *zone*. */
|
|
106
|
+
function fieldsIn(instant: number, zone: string): Fields {
|
|
107
|
+
let formatter = formatters.get(zone);
|
|
108
|
+
if (formatter === undefined) {
|
|
109
|
+
formatter = new Intl.DateTimeFormat("en-US", {
|
|
110
|
+
timeZone: zone,
|
|
111
|
+
hourCycle: "h23",
|
|
112
|
+
year: "numeric",
|
|
113
|
+
month: "2-digit",
|
|
114
|
+
day: "2-digit",
|
|
115
|
+
hour: "2-digit",
|
|
116
|
+
minute: "2-digit",
|
|
117
|
+
second: "2-digit",
|
|
118
|
+
});
|
|
119
|
+
formatters.set(zone, formatter);
|
|
120
|
+
}
|
|
121
|
+
const parts = Object.fromEntries(
|
|
122
|
+
formatter.formatToParts(new Date(instant)).map((part) => [part.type, part.value]),
|
|
123
|
+
);
|
|
124
|
+
return {
|
|
125
|
+
year: Number(parts.year),
|
|
126
|
+
month: Number(parts.month),
|
|
127
|
+
day: Number(parts.day),
|
|
128
|
+
hour: Number(parts.hour),
|
|
129
|
+
minute: Number(parts.minute),
|
|
130
|
+
second: Number(parts.second),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
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;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function pad(value: number, width = 2): string {
|
|
149
|
+
return String(value).padStart(width, "0");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The wall time an instant reads as in *zone*, as a `datetime-local` input takes it. */
|
|
153
|
+
function wallTime(value: string | null, zone: string): string {
|
|
154
|
+
if (value === null || !WITH_OFFSET.test(value)) {
|
|
155
|
+
return "";
|
|
156
|
+
}
|
|
157
|
+
const instant = Date.parse(value);
|
|
158
|
+
if (Number.isNaN(instant)) {
|
|
159
|
+
return "";
|
|
160
|
+
}
|
|
161
|
+
const fields = fieldsIn(instant, zone);
|
|
162
|
+
return `${pad(fields.year, 4)}-${pad(fields.month)}-${pad(fields.day)}T${pad(fields.hour)}:${pad(fields.minute)}`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The instant a wall time names in *zone*, with that zone's offset; `null` for none. */
|
|
166
|
+
function instantOf(wall: string, zone: string): string | null {
|
|
167
|
+
const match = WALL_TIME.exec(wall);
|
|
168
|
+
if (match === null) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const [, year, month, day, hour, minute, second] = match;
|
|
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
|
+
});
|
|
180
|
+
// The offsets on either side of any change of the clocks near this wall time; each is a
|
|
181
|
+
// candidate, and it names this wall time if the zone runs at that offset at that instant.
|
|
182
|
+
const before = offsetMillis(local - DAY, zone);
|
|
183
|
+
const after = offsetMillis(local + DAY, zone);
|
|
184
|
+
const fits = [before, after]
|
|
185
|
+
.map((offset) => local - offset)
|
|
186
|
+
.filter((candidate) => offsetMillis(candidate, zone) === local - candidate);
|
|
187
|
+
// Repeated, both fit and the earlier is taken; skipped, none fits and the offset before the
|
|
188
|
+
// gap carries the wall time past it.
|
|
189
|
+
const instant = fits.length > 0 ? Math.min(...fits) : local - before;
|
|
190
|
+
return rfc3339(instant, zone);
|
|
191
|
+
}
|
|
192
|
+
|
|
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. */
|
|
197
|
+
function rfc3339(instant: number, zone: string): string {
|
|
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;
|
|
201
|
+
const magnitude = Math.abs(offset);
|
|
202
|
+
const suffix =
|
|
203
|
+
offset === 0
|
|
204
|
+
? "Z"
|
|
205
|
+
: `${offset < 0 ? "-" : "+"}${pad(Math.floor(magnitude / 60))}:${pad(magnitude % 60)}`;
|
|
206
|
+
return (
|
|
207
|
+
`${pad(fields.year, 4)}-${pad(fields.month)}-${pad(fields.day)}` +
|
|
208
|
+
`T${pad(fields.hour)}:${pad(fields.minute)}:${pad(fields.second)}${suffix}`
|
|
209
|
+
);
|
|
210
|
+
}
|