@amritk/nish-x86_64-linux 0.40.1 → 0.41.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/INSTALL.md +5 -4
- package/bin/nish +0 -0
- package/package.json +1 -1
- package/runtime/runtime-uring.c +1194 -0
- package/scripts/build.sh +7 -6
- package/std/README.md +1 -0
- package/std/net/prefork.ts +49 -13
- package/std/temporal.ts +3091 -0
package/std/temporal.ts
ADDED
|
@@ -0,0 +1,3091 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/temporal` — dates, times, instants and durations in the ISO 8601
|
|
3
|
+
* calendar, under the names and the rules of TC39's `Temporal`
|
|
4
|
+
* (`docs/wp64-temporal.md`).
|
|
5
|
+
*
|
|
6
|
+
* import { PlainDate, Duration, plainDateFrom } from "nish/temporal";
|
|
7
|
+
*
|
|
8
|
+
* export const main = (): i32 => {
|
|
9
|
+
* const due: PlainDate = plainDateFrom("2024-01-31");
|
|
10
|
+
* const next: PlainDate = due.add(new Duration(0, 1)); // 2024-02-29
|
|
11
|
+
* console.log(next.toString());
|
|
12
|
+
* console.log(due.until(next).toString()); // P29D
|
|
13
|
+
* return 0;
|
|
14
|
+
* };
|
|
15
|
+
*
|
|
16
|
+
* **Five types, each immutable.** `PlainDate`, `PlainTime` and `PlainDateTime`
|
|
17
|
+
* are wall-clock values with no time zone; `Instant` is an exact point on the
|
|
18
|
+
* UTC timeline; `Duration` is ten signed fields, `years` to `nanoseconds`.
|
|
19
|
+
* Every method answers a new value. A field reads as Temporal's getter of the
|
|
20
|
+
* same name does (`d.year`, `d.dayOfWeek`, `t.nanosecond`), and the string
|
|
21
|
+
* forms are Temporal's byte for byte.
|
|
22
|
+
*
|
|
23
|
+
* **What is Temporal's and what is this module's.** A class here is the
|
|
24
|
+
* Temporal class of the same name, and `plainDateFrom(s)` is
|
|
25
|
+
* `Temporal.PlainDate.from(s)`, `plainDateCompare(a, b)` is
|
|
26
|
+
* `Temporal.PlainDate.compare(a, b)`, and so on for each type: Nish has no
|
|
27
|
+
* `static` members, so the class's statics are free functions named after it.
|
|
28
|
+
* The arithmetic follows the specification's: `add` constrains a day past the
|
|
29
|
+
* end of the month (`2024-01-31` plus a month is `2024-02-29`), `until` and
|
|
30
|
+
* `since` answer the largest unit Temporal's default does (days for a date or a
|
|
31
|
+
* date-time, hours for a time, seconds for an instant), and a string Temporal
|
|
32
|
+
* refuses with a `RangeError` panics here with a message that starts
|
|
33
|
+
* `RangeError:`. The `...OrNull` functions are this module's own: the same
|
|
34
|
+
* parse, answering `null` instead.
|
|
35
|
+
*
|
|
36
|
+
* **What is not here yet** (`docs/wp64-temporal.md` §6): `ZonedDateTime` and
|
|
37
|
+
* every time zone but UTC, `PlainYearMonth`, `PlainMonthDay`, the option
|
|
38
|
+
* bags (`{ largestUnit: "year" }`, `with({ day: 1 })`, `round`, `total`),
|
|
39
|
+
* calendars other than `iso8601`, `toLocaleString`, and `epochNanoseconds`,
|
|
40
|
+
* which is a `bigint` in JavaScript. An `Instant` is any nanosecond in
|
|
41
|
+
* Temporal's range of ±10^8 days from 1970, held as `i64` seconds and `i32`
|
|
42
|
+
* nanoseconds; a `Duration`'s fields are `i64`, which is narrower than
|
|
43
|
+
* Temporal's for microseconds and nanoseconds alone.
|
|
44
|
+
*
|
|
45
|
+
* **Private helpers carry the module's name** because they share the
|
|
46
|
+
* importing program's flat symbol namespace (`docs/wp26-stdlib.md` §3e).
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import {
|
|
50
|
+
addWrapping,
|
|
51
|
+
asU8x16,
|
|
52
|
+
mulWrapping,
|
|
53
|
+
shr,
|
|
54
|
+
shuffle,
|
|
55
|
+
splatU16x8,
|
|
56
|
+
store,
|
|
57
|
+
subWrapping,
|
|
58
|
+
u8x16,
|
|
59
|
+
u16x8,
|
|
60
|
+
} from "nish:simd"
|
|
61
|
+
|
|
62
|
+
/** Nanoseconds in a second. */
|
|
63
|
+
const TEMPORAL_NS_PER_SECOND: i64 = 1000000000
|
|
64
|
+
|
|
65
|
+
/** Seconds in a day; a day is 24 hours in every type here, none of which has a zone. */
|
|
66
|
+
const TEMPORAL_SECONDS_PER_DAY: i64 = 86400
|
|
67
|
+
|
|
68
|
+
/** Nanoseconds in a day. */
|
|
69
|
+
const TEMPORAL_NS_PER_DAY: i64 = 86400000000000
|
|
70
|
+
|
|
71
|
+
/** The epoch day of +275760-09-13, the last date Temporal can represent. */
|
|
72
|
+
const TEMPORAL_MAX_EPOCH_DAY: i64 = 100000000
|
|
73
|
+
|
|
74
|
+
/** The epoch day of -271821-04-19, the first date Temporal can represent. */
|
|
75
|
+
const TEMPORAL_MIN_EPOCH_DAY: i64 = -100000001
|
|
76
|
+
|
|
77
|
+
/** The largest magnitude of a duration's normalized seconds: 2^53 - 1. */
|
|
78
|
+
const TEMPORAL_MAX_DURATION_SECONDS: i64 = 9007199254740991
|
|
79
|
+
|
|
80
|
+
/** The bound on `years`, `months` and `weeks`: 2^32, exclusive. */
|
|
81
|
+
const TEMPORAL_MAX_CALENDAR_FIELD: i64 = 4294967296
|
|
82
|
+
|
|
83
|
+
/** The units, smallest first, so that "larger" is `>`. */
|
|
84
|
+
const TEMPORAL_UNIT_NANOSECOND: i32 = 0
|
|
85
|
+
const TEMPORAL_UNIT_MICROSECOND: i32 = 1
|
|
86
|
+
const TEMPORAL_UNIT_MILLISECOND: i32 = 2
|
|
87
|
+
const TEMPORAL_UNIT_SECOND: i32 = 3
|
|
88
|
+
const TEMPORAL_UNIT_MINUTE: i32 = 4
|
|
89
|
+
const TEMPORAL_UNIT_HOUR: i32 = 5
|
|
90
|
+
const TEMPORAL_UNIT_DAY: i32 = 6
|
|
91
|
+
const TEMPORAL_UNIT_WEEK: i32 = 7
|
|
92
|
+
const TEMPORAL_UNIT_MONTH: i32 = 8
|
|
93
|
+
const TEMPORAL_UNIT_YEAR: i32 = 9
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// Integer helpers. Every division here is written `toI64(a / b)` so that it
|
|
97
|
+
// truncates under Node exactly as the native `sdiv` does, and every value that
|
|
98
|
+
// is divided stays below 2^53 so that Node's doubles hold it exactly.
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
/** `a / b` rounded toward negative infinity, for `b > 0`. */
|
|
102
|
+
const temporalFloorDiv = (a: i64, b: i64): i64 => {
|
|
103
|
+
const q: i64 = toI64(a / b)
|
|
104
|
+
return q * b > a ? q - 1 : q
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** `a` modulo `b` in `[0, b)`, for `b > 0`. */
|
|
108
|
+
const temporalFloorMod = (a: i64, b: i64): i64 => a - temporalFloorDiv(a, b) * b
|
|
109
|
+
|
|
110
|
+
/** `a / b` rounded toward zero, for `b > 0`. */
|
|
111
|
+
const temporalTruncDiv = (a: i64, b: i64): i64 => toI64(a / b)
|
|
112
|
+
|
|
113
|
+
/** -1, 0 or 1 as `a` is negative, zero or positive. */
|
|
114
|
+
const temporalSign = (a: i64): i32 => {
|
|
115
|
+
if (a < 0) {
|
|
116
|
+
return -1
|
|
117
|
+
}
|
|
118
|
+
return a > 0 ? 1 : 0
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The writers. Every `toString` fills one byte buffer and makes one string of
|
|
122
|
+
// it, rather than joining a template's parts: each `${n}` and each join is an
|
|
123
|
+
// allocation and a copy, and they were a fifth of the time `toString` took.
|
|
124
|
+
// Each writer puts its text at `at` and answers the index just past it.
|
|
125
|
+
|
|
126
|
+
/** The most bytes any `toString` here writes: a `Duration` of ten 19-digit fields. */
|
|
127
|
+
const TEMPORAL_TEXT_MAX: i32 = 256
|
|
128
|
+
|
|
129
|
+
/** Write the ASCII byte `c` at `at`. */
|
|
130
|
+
const temporalPutByte = (out: u8[], at: i32, c: i32): i32 => {
|
|
131
|
+
if (at < 0 || at >= toI32(out.length)) {
|
|
132
|
+
return at
|
|
133
|
+
}
|
|
134
|
+
out[at] = toU8(c)
|
|
135
|
+
return at + 1
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Write `n`, which is not negative, in decimal, zero-padded to at least `width` digits. */
|
|
139
|
+
const temporalPutNumber = (out: u8[], at: i32, n: i64, width: i32): i32 => {
|
|
140
|
+
let count: i32 = 1
|
|
141
|
+
let rest: i64 = n
|
|
142
|
+
while (rest >= 10) {
|
|
143
|
+
rest = toI64(rest / 10)
|
|
144
|
+
count = count + 1
|
|
145
|
+
}
|
|
146
|
+
if (count < width) {
|
|
147
|
+
count = width
|
|
148
|
+
}
|
|
149
|
+
let v: i64 = n
|
|
150
|
+
for (let i: i32 = count - 1; i >= 0; i--) {
|
|
151
|
+
out[at + i] = toU8(48 + toI32(v % 10))
|
|
152
|
+
v = toI64(v / 10)
|
|
153
|
+
}
|
|
154
|
+
return at + count
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// The fixed-width writers: a date or a time is always the same digits, so
|
|
158
|
+
// they are written two at a time, with one bounds test that covers both, and
|
|
159
|
+
// no count of digits first. `n / 10` of an `i32` is a multiply and a shift.
|
|
160
|
+
|
|
161
|
+
/** Write `n`, 0 to 99, as two digits. */
|
|
162
|
+
const temporalPut2 = (out: u8[], at: i32, n: i32): i32 => {
|
|
163
|
+
if (at < 0 || at > toI32(out.length) - 2) {
|
|
164
|
+
return at
|
|
165
|
+
}
|
|
166
|
+
const tens: i32 = toI32(n / 10)
|
|
167
|
+
out[at] = toU8(48 + tens)
|
|
168
|
+
out[at + 1] = toU8(48 + n - tens * 10)
|
|
169
|
+
return at + 2
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Write `n`, 0 to 9999, as four digits. */
|
|
173
|
+
const temporalPut4 = (out: u8[], at: i32, n: i32): i32 => {
|
|
174
|
+
const high: i32 = toI32(n / 100)
|
|
175
|
+
return temporalPut2(out, temporalPut2(out, at, high), n - high * 100)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Write a year as Temporal does: four digits for 0 to 9999, otherwise a sign and six digits. */
|
|
179
|
+
const temporalPutYear = (out: u8[], at: i32, year: i32): i32 => {
|
|
180
|
+
if (year >= 0 && year <= 9999) {
|
|
181
|
+
return temporalPut4(out, at, year)
|
|
182
|
+
}
|
|
183
|
+
const magnitude: i32 = year < 0 ? -year : year
|
|
184
|
+
const high: i32 = toI32(magnitude / 10000)
|
|
185
|
+
const signed: i32 = temporalPutByte(out, at, year < 0 ? 45 : 43)
|
|
186
|
+
return temporalPut4(out, temporalPut2(out, signed, high), magnitude - high * 10000)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Write `YYYY-MM-DD` for a year from 0 to 9999 in one vector store: the four
|
|
191
|
+
* two-digit groups are lanes of a `u16x8`, each lane's tens are
|
|
192
|
+
* `(v * 103) >> 10` (exact below 100), one shuffle interleaves the tens and
|
|
193
|
+
* ones bytes with a zero byte where each `-` goes, and adding a template of
|
|
194
|
+
* `0` and `-` makes the text. `out` must have 16 bytes from `at`.
|
|
195
|
+
*/
|
|
196
|
+
const temporalPutDate4 = (out: u8[], at: i32, year: i32, month: i32, day: i32): i32 => {
|
|
197
|
+
const century: i32 = toI32(year / 100)
|
|
198
|
+
const v: u16x8 = u16x8(
|
|
199
|
+
toU16(century),
|
|
200
|
+
toU16(year - century * 100),
|
|
201
|
+
toU16(month),
|
|
202
|
+
toU16(day),
|
|
203
|
+
toU16(0),
|
|
204
|
+
toU16(0),
|
|
205
|
+
toU16(0),
|
|
206
|
+
toU16(0)
|
|
207
|
+
)
|
|
208
|
+
const tens: u16x8 = shr(mulWrapping(v, splatU16x8(toU16(103))), 10)
|
|
209
|
+
const ones: u16x8 = subWrapping(v, mulWrapping(tens, splatU16x8(toU16(10))))
|
|
210
|
+
// Byte 2k of a little-endian u16x8 is lane k's low byte; 8 is lane 4, a zero.
|
|
211
|
+
const digits: u8x16 = shuffle(
|
|
212
|
+
asU8x16(tens),
|
|
213
|
+
asU8x16(ones),
|
|
214
|
+
0,
|
|
215
|
+
16,
|
|
216
|
+
2,
|
|
217
|
+
18,
|
|
218
|
+
8,
|
|
219
|
+
4,
|
|
220
|
+
20,
|
|
221
|
+
8,
|
|
222
|
+
6,
|
|
223
|
+
22,
|
|
224
|
+
8,
|
|
225
|
+
8,
|
|
226
|
+
8,
|
|
227
|
+
8,
|
|
228
|
+
8,
|
|
229
|
+
8
|
|
230
|
+
)
|
|
231
|
+
const template: u8x16 = u8x16(
|
|
232
|
+
toU8(48),
|
|
233
|
+
toU8(48),
|
|
234
|
+
toU8(48),
|
|
235
|
+
toU8(48),
|
|
236
|
+
toU8(45),
|
|
237
|
+
toU8(48),
|
|
238
|
+
toU8(48),
|
|
239
|
+
toU8(45),
|
|
240
|
+
toU8(48),
|
|
241
|
+
toU8(48),
|
|
242
|
+
toU8(0),
|
|
243
|
+
toU8(0),
|
|
244
|
+
toU8(0),
|
|
245
|
+
toU8(0),
|
|
246
|
+
toU8(0),
|
|
247
|
+
toU8(0)
|
|
248
|
+
)
|
|
249
|
+
store(addWrapping(digits, template), out, at)
|
|
250
|
+
return at + 10
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Write `YYYY-MM-DDTHH:MM:SS` for a year from 0 to 9999 in two vector stores,
|
|
255
|
+
* as `temporalPutDate4` writes a date: seven two-digit groups in one `u16x8`,
|
|
256
|
+
* the first sixteen bytes of text from one shuffle and the last three from a
|
|
257
|
+
* second. `out` must have 32 bytes from `at`; the bytes past the nineteenth
|
|
258
|
+
* are zeros for the caller to write over.
|
|
259
|
+
*/
|
|
260
|
+
const temporalPutDateTime4 = (
|
|
261
|
+
out: u8[],
|
|
262
|
+
at: i32,
|
|
263
|
+
year: i32,
|
|
264
|
+
month: i32,
|
|
265
|
+
day: i32,
|
|
266
|
+
hour: i32,
|
|
267
|
+
minute: i32,
|
|
268
|
+
second: i32
|
|
269
|
+
): i32 => {
|
|
270
|
+
const century: i32 = toI32(year / 100)
|
|
271
|
+
const v: u16x8 = u16x8(
|
|
272
|
+
toU16(century),
|
|
273
|
+
toU16(year - century * 100),
|
|
274
|
+
toU16(month),
|
|
275
|
+
toU16(day),
|
|
276
|
+
toU16(hour),
|
|
277
|
+
toU16(minute),
|
|
278
|
+
toU16(second),
|
|
279
|
+
toU16(0)
|
|
280
|
+
)
|
|
281
|
+
const tens: u16x8 = shr(mulWrapping(v, splatU16x8(toU16(103))), 10)
|
|
282
|
+
const ones: u16x8 = subWrapping(v, mulWrapping(tens, splatU16x8(toU16(10))))
|
|
283
|
+
const t: u8x16 = asU8x16(tens)
|
|
284
|
+
const o: u8x16 = asU8x16(ones)
|
|
285
|
+
// Byte 14 is lane 7's tens, a zero, where each separator goes.
|
|
286
|
+
const head: u8x16 = shuffle(t, o, 0, 16, 2, 18, 14, 4, 20, 14, 6, 22, 14, 8, 24, 14, 10, 26)
|
|
287
|
+
const tail: u8x16 = shuffle(t, o, 14, 12, 28, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14)
|
|
288
|
+
const headText: u8x16 = u8x16(
|
|
289
|
+
toU8(48),
|
|
290
|
+
toU8(48),
|
|
291
|
+
toU8(48),
|
|
292
|
+
toU8(48),
|
|
293
|
+
toU8(45),
|
|
294
|
+
toU8(48),
|
|
295
|
+
toU8(48),
|
|
296
|
+
toU8(45),
|
|
297
|
+
toU8(48),
|
|
298
|
+
toU8(48),
|
|
299
|
+
toU8(84),
|
|
300
|
+
toU8(48),
|
|
301
|
+
toU8(48),
|
|
302
|
+
toU8(58),
|
|
303
|
+
toU8(48),
|
|
304
|
+
toU8(48)
|
|
305
|
+
)
|
|
306
|
+
const tailText: u8x16 = u8x16(
|
|
307
|
+
toU8(58),
|
|
308
|
+
toU8(48),
|
|
309
|
+
toU8(48),
|
|
310
|
+
toU8(0),
|
|
311
|
+
toU8(0),
|
|
312
|
+
toU8(0),
|
|
313
|
+
toU8(0),
|
|
314
|
+
toU8(0),
|
|
315
|
+
toU8(0),
|
|
316
|
+
toU8(0),
|
|
317
|
+
toU8(0),
|
|
318
|
+
toU8(0),
|
|
319
|
+
toU8(0),
|
|
320
|
+
toU8(0),
|
|
321
|
+
toU8(0),
|
|
322
|
+
toU8(0)
|
|
323
|
+
)
|
|
324
|
+
store(addWrapping(head, headText), out, at)
|
|
325
|
+
store(addWrapping(tail, tailText), out, at + 16)
|
|
326
|
+
return at + 19
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Write `YYYY-MM-DD`. */
|
|
330
|
+
const temporalPutDate = (out: u8[], at: i32, year: i32, month: i32, day: i32): i32 => {
|
|
331
|
+
let i: i32 = temporalPutYear(out, at, year)
|
|
332
|
+
i = temporalPut2(out, temporalPutByte(out, i, 45), month)
|
|
333
|
+
return temporalPut2(out, temporalPutByte(out, i, 45), day)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Write `ns` nanoseconds of a second as Temporal's automatic precision does:
|
|
338
|
+
* nothing for zero, otherwise a `.` and up to nine digits with the trailing
|
|
339
|
+
* zeros dropped. All nine are written and the zeros then given back, which
|
|
340
|
+
* is one pass rather than a division per zero first.
|
|
341
|
+
*/
|
|
342
|
+
const temporalPutFraction = (out: u8[], at: i32, ns: i32): i32 => {
|
|
343
|
+
if (ns === 0) {
|
|
344
|
+
return at
|
|
345
|
+
}
|
|
346
|
+
const head: i32 = toI32(ns / 100000000)
|
|
347
|
+
const rest: i32 = ns - head * 100000000
|
|
348
|
+
const middle: i32 = toI32(rest / 10000)
|
|
349
|
+
let end: i32 = temporalPutByte(out, temporalPutByte(out, at, 46), 48 + head)
|
|
350
|
+
end = temporalPut4(out, temporalPut4(out, end, middle), rest - middle * 10000)
|
|
351
|
+
while (end > at + 2 && end <= toI32(out.length) && out[end - 1] === 48) {
|
|
352
|
+
end = end - 1
|
|
353
|
+
}
|
|
354
|
+
return end
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Write `HH:MM:SS` and the fraction of the second. */
|
|
358
|
+
const temporalPutTime = (out: u8[], at: i32, hour: i32, minute: i32, second: i32, sub: i32): i32 => {
|
|
359
|
+
let i: i32 = temporalPut2(out, at, hour)
|
|
360
|
+
i = temporalPut2(out, temporalPutByte(out, i, 58), minute)
|
|
361
|
+
i = temporalPut2(out, temporalPutByte(out, i, 58), second)
|
|
362
|
+
return temporalPutFraction(out, i, sub)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** Write the time of day `ns` nanoseconds after midnight. */
|
|
366
|
+
const temporalPutNanoOfDay = (out: u8[], at: i32, ns: i64): i32 => {
|
|
367
|
+
const secs: i32 = toI32(ns / TEMPORAL_NS_PER_SECOND)
|
|
368
|
+
const minutes: i32 = toI32(secs / 60)
|
|
369
|
+
return temporalPutTime(
|
|
370
|
+
out,
|
|
371
|
+
at,
|
|
372
|
+
toI32(minutes / 60),
|
|
373
|
+
minutes % 60,
|
|
374
|
+
secs - minutes * 60,
|
|
375
|
+
toI32(ns % TEMPORAL_NS_PER_SECOND)
|
|
376
|
+
)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Write `n` and the designator `c`, as a duration field is written. */
|
|
380
|
+
const temporalPutField = (out: u8[], at: i32, n: i64, c: i32): i32 =>
|
|
381
|
+
temporalPutByte(out, temporalPutNumber(out, at, n, 1), c)
|
|
382
|
+
|
|
383
|
+
/** Panic with Temporal's `RangeError`, naming `what`. */
|
|
384
|
+
const temporalRangeError = (what: string): void => {
|
|
385
|
+
panic(`RangeError: ${what}`)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ---------------------------------------------------------------------------
|
|
389
|
+
// The proleptic Gregorian calendar (Hinnant, "chrono-Compatible Low-Level Date
|
|
390
|
+
// Algorithms": days_from_civil and civil_from_days over 400-year eras).
|
|
391
|
+
// ---------------------------------------------------------------------------
|
|
392
|
+
|
|
393
|
+
/** Whether `year` is a leap year of the proleptic Gregorian calendar. */
|
|
394
|
+
const temporalIsLeapYear = (year: i64): boolean => year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)
|
|
395
|
+
|
|
396
|
+
/** The days in `month` (1 to 12) of `year`. */
|
|
397
|
+
const temporalDaysInMonth = (year: i64, month: i32): i32 =>
|
|
398
|
+
temporalMonthLength(month, month === 2 && temporalIsLeapYear(year))
|
|
399
|
+
|
|
400
|
+
/** The days in `month` (1 to 12) of a year that is a leap year when `leap`. */
|
|
401
|
+
const temporalMonthLength = (month: i32, leap: boolean): i32 => {
|
|
402
|
+
if (month === 2) {
|
|
403
|
+
return leap ? 29 : 28
|
|
404
|
+
}
|
|
405
|
+
if (month === 4 || month === 6 || month === 9 || month === 11) {
|
|
406
|
+
return 30
|
|
407
|
+
}
|
|
408
|
+
return 31
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** The days of a common year before the first of each month. */
|
|
412
|
+
const TEMPORAL_DAYS_BEFORE_MONTH: readonly i32[] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
|
|
413
|
+
|
|
414
|
+
/** The month as Temporal codes it, `M01` to `M12`, without building a string. */
|
|
415
|
+
const temporalMonthCode = (month: i32): string => {
|
|
416
|
+
switch (month) {
|
|
417
|
+
case 1:
|
|
418
|
+
return "M01"
|
|
419
|
+
case 2:
|
|
420
|
+
return "M02"
|
|
421
|
+
case 3:
|
|
422
|
+
return "M03"
|
|
423
|
+
case 4:
|
|
424
|
+
return "M04"
|
|
425
|
+
case 5:
|
|
426
|
+
return "M05"
|
|
427
|
+
case 6:
|
|
428
|
+
return "M06"
|
|
429
|
+
case 7:
|
|
430
|
+
return "M07"
|
|
431
|
+
case 8:
|
|
432
|
+
return "M08"
|
|
433
|
+
case 9:
|
|
434
|
+
return "M09"
|
|
435
|
+
case 10:
|
|
436
|
+
return "M10"
|
|
437
|
+
case 11:
|
|
438
|
+
return "M11"
|
|
439
|
+
default:
|
|
440
|
+
return "M12"
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** The days from 1970-01-01 to the given date, negative before it. */
|
|
445
|
+
const temporalEpochDay = (year: i64, month: i32, day: i32): i64 => {
|
|
446
|
+
const y: i64 = month <= 2 ? year - 1 : year
|
|
447
|
+
const era: i64 = temporalFloorDiv(y, 400)
|
|
448
|
+
const yoe: i64 = y - era * 400
|
|
449
|
+
const mp: i64 = toI64(month > 2 ? month - 3 : month + 9)
|
|
450
|
+
const doy: i64 = temporalTruncDiv(153 * mp + 2, 5) + toI64(day) - 1
|
|
451
|
+
const doe: i64 = yoe * 365 + temporalTruncDiv(yoe, 4) - temporalTruncDiv(yoe, 100) + doy
|
|
452
|
+
return era * 146097 + doe - 719468
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** The days from 1 January to the given date, plus one, in a year that is a leap year when `leap`. */
|
|
456
|
+
const temporalDayOfYear = (month: i32, day: i32, leap: boolean): i32 => {
|
|
457
|
+
const before: i32 = month >= 1 && month <= 12 ? TEMPORAL_DAYS_BEFORE_MONTH[month - 1] : 0
|
|
458
|
+
return before + day + (leap && month > 2 ? 1 : 0)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** The ISO weekday of an epoch day: 1 for Monday to 7 for Sunday. */
|
|
462
|
+
const temporalDayOfWeek = (epochDay: i64): i32 => toI32(temporalFloorMod(epochDay + 3, 7)) + 1
|
|
463
|
+
|
|
464
|
+
/** How many ISO weeks `year` has: 53 when it starts on a Thursday, or is a leap year starting on a Wednesday. */
|
|
465
|
+
const temporalWeeksInYear = (year: i64): i32 =>
|
|
466
|
+
temporalWeekdayOfDecember31(year) === 4 || temporalWeekdayOfDecember31(year - 1) === 3 ? 53 : 52
|
|
467
|
+
|
|
468
|
+
/** The weekday of 31 December of `year`, 0 for Sunday to 6 for Saturday. */
|
|
469
|
+
const temporalWeekdayOfDecember31 = (y: i64): i64 =>
|
|
470
|
+
temporalFloorMod(y + temporalFloorDiv(y, 4) - temporalFloorDiv(y, 100) + temporalFloorDiv(y, 400), 7)
|
|
471
|
+
|
|
472
|
+
/** Panic unless `year`, `month` and `day` name a real date. */
|
|
473
|
+
const temporalCheckDate = (what: string, year: i64, month: i64, day: i64): void => {
|
|
474
|
+
if (year < -271821 || year > 275760) {
|
|
475
|
+
temporalRangeError(`${what}: year ${year} is outside -271821 to 275760`)
|
|
476
|
+
}
|
|
477
|
+
if (month < 1 || month > 12) {
|
|
478
|
+
temporalRangeError(`${what}: month ${month} is outside 1 to 12`)
|
|
479
|
+
}
|
|
480
|
+
const most: i32 = temporalDaysInMonth(year, toI32(month))
|
|
481
|
+
if (day < 1 || day > toI64(most)) {
|
|
482
|
+
temporalRangeError(`${what}: day ${day} is outside 1 to ${most}`)
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Panic unless the fields name a time of day. */
|
|
487
|
+
const temporalCheckTime = (
|
|
488
|
+
what: string,
|
|
489
|
+
hour: i64,
|
|
490
|
+
minute: i64,
|
|
491
|
+
second: i64,
|
|
492
|
+
millisecond: i64,
|
|
493
|
+
microsecond: i64,
|
|
494
|
+
nanosecond: i64
|
|
495
|
+
): void => {
|
|
496
|
+
if (hour < 0 || hour > 23) {
|
|
497
|
+
temporalRangeError(`${what}: hour ${hour} is outside 0 to 23`)
|
|
498
|
+
}
|
|
499
|
+
if (minute < 0 || minute > 59) {
|
|
500
|
+
temporalRangeError(`${what}: minute ${minute} is outside 0 to 59`)
|
|
501
|
+
}
|
|
502
|
+
if (second < 0 || second > 59) {
|
|
503
|
+
temporalRangeError(`${what}: second ${second} is outside 0 to 59`)
|
|
504
|
+
}
|
|
505
|
+
if (millisecond < 0 || millisecond > 999) {
|
|
506
|
+
temporalRangeError(`${what}: millisecond ${millisecond} is outside 0 to 999`)
|
|
507
|
+
}
|
|
508
|
+
if (microsecond < 0 || microsecond > 999) {
|
|
509
|
+
temporalRangeError(`${what}: microsecond ${microsecond} is outside 0 to 999`)
|
|
510
|
+
}
|
|
511
|
+
if (nanosecond < 0 || nanosecond > 999) {
|
|
512
|
+
temporalRangeError(`${what}: nanosecond ${nanosecond} is outside 0 to 999`)
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** The nanoseconds since midnight of a time of day whose fields are in range. */
|
|
517
|
+
const temporalNanoOfDay = (
|
|
518
|
+
hour: i32,
|
|
519
|
+
minute: i32,
|
|
520
|
+
second: i32,
|
|
521
|
+
millisecond: i32,
|
|
522
|
+
microsecond: i32,
|
|
523
|
+
nanosecond: i32
|
|
524
|
+
): i64 =>
|
|
525
|
+
(toI64(hour) * 3600 + toI64(minute) * 60 + toI64(second)) * TEMPORAL_NS_PER_SECOND +
|
|
526
|
+
toI64(millisecond) * 1000000 +
|
|
527
|
+
toI64(microsecond) * 1000 +
|
|
528
|
+
toI64(nanosecond)
|
|
529
|
+
|
|
530
|
+
/** A date on an epoch day, after checking the day is inside Temporal's range. */
|
|
531
|
+
const temporalDateOfEpochDay = (epochDay: i64): PlainDate => {
|
|
532
|
+
if (epochDay < TEMPORAL_MIN_EPOCH_DAY || epochDay > TEMPORAL_MAX_EPOCH_DAY) {
|
|
533
|
+
temporalRangeError("PlainDate: the date is outside Temporal's range")
|
|
534
|
+
}
|
|
535
|
+
const civil: i64 = temporalCivilOfEpochDay(epochDay)
|
|
536
|
+
return new PlainDate(temporalCivilYear(civil), temporalCivilMonth(civil), temporalCivilDay(civil))
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* The year, month and day of an epoch day, packed into one integer so that
|
|
541
|
+
* nothing is allocated: `(year + 400000) * 512 + month * 32 + day`, which
|
|
542
|
+
* stays below 2^31 over Temporal's whole range. `temporalCivilYear`,
|
|
543
|
+
* `temporalCivilMonth` and `temporalCivilDay` take it apart.
|
|
544
|
+
*/
|
|
545
|
+
const temporalCivilOfEpochDay = (epochDay: i64): i64 => {
|
|
546
|
+
const z: i64 = epochDay + 719468
|
|
547
|
+
const era: i64 = temporalFloorDiv(z, 146097)
|
|
548
|
+
const doe: i64 = z - era * 146097
|
|
549
|
+
const yoe: i64 = temporalTruncDiv(
|
|
550
|
+
doe - temporalTruncDiv(doe, 1460) + temporalTruncDiv(doe, 36524) - temporalTruncDiv(doe, 146096),
|
|
551
|
+
365
|
|
552
|
+
)
|
|
553
|
+
const doy: i64 = doe - (365 * yoe + temporalTruncDiv(yoe, 4) - temporalTruncDiv(yoe, 100))
|
|
554
|
+
const mp: i64 = temporalTruncDiv(5 * doy + 2, 153)
|
|
555
|
+
const day: i64 = doy - temporalTruncDiv(153 * mp + 2, 5) + 1
|
|
556
|
+
const month: i64 = mp < 10 ? mp + 3 : mp - 9
|
|
557
|
+
const year: i64 = yoe + era * 400 + (month <= 2 ? 1 : 0)
|
|
558
|
+
return (year + 400000) * 512 + month * 32 + day
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/** The year of a packed civil date. */
|
|
562
|
+
const temporalCivilYear = (civil: i64): i32 => toI32(toI64(civil / 512) - 400000)
|
|
563
|
+
|
|
564
|
+
/** The month of a packed civil date. */
|
|
565
|
+
const temporalCivilMonth = (civil: i64): i32 => toI32(toI64(civil / 32) % 16)
|
|
566
|
+
|
|
567
|
+
/** The day of a packed civil date. */
|
|
568
|
+
const temporalCivilDay = (civil: i64): i32 => toI32(civil % 32)
|
|
569
|
+
|
|
570
|
+
/** The time of day `ns` nanoseconds after midnight, `0 <= ns < 86,400 s`. */
|
|
571
|
+
const temporalTimeOfNanos = (ns: i64): PlainTime => {
|
|
572
|
+
const sub: i64 = temporalFloorMod(ns, TEMPORAL_NS_PER_SECOND)
|
|
573
|
+
const secs: i64 = temporalFloorDiv(ns, TEMPORAL_NS_PER_SECOND)
|
|
574
|
+
return new PlainTime(
|
|
575
|
+
toI32(temporalTruncDiv(secs, 3600)),
|
|
576
|
+
toI32(temporalTruncDiv(secs, 60) % 60),
|
|
577
|
+
toI32(secs % 60),
|
|
578
|
+
toI32(temporalTruncDiv(sub, 1000000)),
|
|
579
|
+
toI32(temporalTruncDiv(sub, 1000) % 1000),
|
|
580
|
+
toI32(sub % 1000)
|
|
581
|
+
)
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// ---------------------------------------------------------------------------
|
|
585
|
+
// Time spans: a signed length of time as whole seconds and a nanosecond
|
|
586
|
+
// remainder in [0, 10^9), which holds Temporal's whole range without the 83
|
|
587
|
+
// bits one nanosecond count would need.
|
|
588
|
+
// ---------------------------------------------------------------------------
|
|
589
|
+
|
|
590
|
+
/** @internal A signed length of time, `seconds + nanos / 10^9`; not part of Temporal. */
|
|
591
|
+
class TemporalSpan {
|
|
592
|
+
/** Whole seconds, rounded toward negative infinity. */
|
|
593
|
+
readonly seconds: i64
|
|
594
|
+
/** The nanoseconds past `seconds`, `0 <= nanos < 10^9`. */
|
|
595
|
+
readonly nanos: i32
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* A span of `seconds` and `nanos`, normalized so that `nanos` is in range.
|
|
599
|
+
*
|
|
600
|
+
* @param seconds whole seconds
|
|
601
|
+
* @param nanos nanoseconds to add, of any sign and size below 2^53
|
|
602
|
+
*/
|
|
603
|
+
constructor(seconds: i64, nanos: i64) {
|
|
604
|
+
this.seconds = seconds + temporalFloorDiv(nanos, TEMPORAL_NS_PER_SECOND)
|
|
605
|
+
this.nanos = toI32(temporalFloorMod(nanos, TEMPORAL_NS_PER_SECOND))
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* This span plus `other`.
|
|
610
|
+
*
|
|
611
|
+
* @param other the span to add
|
|
612
|
+
* @returns the sum
|
|
613
|
+
*/
|
|
614
|
+
plus(other: TemporalSpan): TemporalSpan {
|
|
615
|
+
return new TemporalSpan(this.seconds + other.seconds, toI64(this.nanos) + toI64(other.nanos))
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* This span negated.
|
|
620
|
+
*
|
|
621
|
+
* @returns the span of the same length in the other direction
|
|
622
|
+
*/
|
|
623
|
+
negated(): TemporalSpan {
|
|
624
|
+
return new TemporalSpan(-this.seconds, -toI64(this.nanos))
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* -1, 0 or 1 as the span is negative, zero or positive.
|
|
629
|
+
*
|
|
630
|
+
* @returns the sign
|
|
631
|
+
*/
|
|
632
|
+
sign(): i32 {
|
|
633
|
+
if (this.seconds < 0) {
|
|
634
|
+
return -1
|
|
635
|
+
}
|
|
636
|
+
return this.seconds > 0 || this.nanos > 0 ? 1 : 0
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* How many whole days the span holds, rounded toward zero.
|
|
641
|
+
*
|
|
642
|
+
* @returns the days, with the span's sign
|
|
643
|
+
*/
|
|
644
|
+
truncDays(): i64 {
|
|
645
|
+
if (this.sign() >= 0) {
|
|
646
|
+
return temporalFloorDiv(this.seconds, TEMPORAL_SECONDS_PER_DAY)
|
|
647
|
+
}
|
|
648
|
+
const magnitude: TemporalSpan = this.negated()
|
|
649
|
+
return -temporalFloorDiv(magnitude.seconds, TEMPORAL_SECONDS_PER_DAY)
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/** The time fields of `d` as one span; its days too, each 24 hours, when `withDays`. */
|
|
654
|
+
const temporalSpanOfDuration = (d: Duration, withDays: boolean): TemporalSpan =>
|
|
655
|
+
new TemporalSpan(temporalDurationSeconds(d, withDays), temporalDurationNanos(d))
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* The whole seconds of `d`'s time fields, and of its days when `withDays`,
|
|
659
|
+
* with each sub-second field's whole seconds; `temporalDurationNanos` is the
|
|
660
|
+
* rest. Arithmetic on the fields, so that nothing is allocated: the sum stays
|
|
661
|
+
* below 2^53 for every valid duration.
|
|
662
|
+
*/
|
|
663
|
+
const temporalDurationSeconds = (d: Duration, withDays: boolean): i64 => {
|
|
664
|
+
let seconds: i64 = d.hours * 3600 + d.minutes * 60 + d.seconds
|
|
665
|
+
if (withDays) {
|
|
666
|
+
seconds = seconds + d.days * TEMPORAL_SECONDS_PER_DAY
|
|
667
|
+
}
|
|
668
|
+
if (d.milliseconds !== 0) {
|
|
669
|
+
seconds = seconds + temporalFloorDiv(d.milliseconds, 1000)
|
|
670
|
+
}
|
|
671
|
+
if (d.microseconds !== 0) {
|
|
672
|
+
seconds = seconds + temporalFloorDiv(d.microseconds, 1000000)
|
|
673
|
+
}
|
|
674
|
+
if (d.nanoseconds !== 0) {
|
|
675
|
+
seconds = seconds + temporalFloorDiv(d.nanoseconds, TEMPORAL_NS_PER_SECOND)
|
|
676
|
+
}
|
|
677
|
+
return seconds
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** The nanoseconds `temporalDurationSeconds` leaves out, each field's in `[0, 10^9)`, so below 3 × 10^9. */
|
|
681
|
+
const temporalDurationNanos = (d: Duration): i64 => {
|
|
682
|
+
let nanos: i64 = 0
|
|
683
|
+
if (d.milliseconds !== 0) {
|
|
684
|
+
nanos = temporalFloorMod(d.milliseconds, 1000) * 1000000
|
|
685
|
+
}
|
|
686
|
+
if (d.microseconds !== 0) {
|
|
687
|
+
nanos = nanos + temporalFloorMod(d.microseconds, 1000000) * 1000
|
|
688
|
+
}
|
|
689
|
+
if (d.nanoseconds !== 0) {
|
|
690
|
+
nanos = nanos + temporalFloorMod(d.nanoseconds, TEMPORAL_NS_PER_SECOND)
|
|
691
|
+
}
|
|
692
|
+
return nanos
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** Whether every time field of `d`, hours to nanoseconds, is zero. */
|
|
696
|
+
const temporalNoTime = (d: Duration): boolean =>
|
|
697
|
+
d.hours === 0 &&
|
|
698
|
+
d.minutes === 0 &&
|
|
699
|
+
d.seconds === 0 &&
|
|
700
|
+
d.milliseconds === 0 &&
|
|
701
|
+
d.microseconds === 0 &&
|
|
702
|
+
d.nanoseconds === 0
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* The epoch day `(year, month, day)` lands on after `years` and `months`,
|
|
706
|
+
* with the day constrained to the new month, then `days` more; `epochDay` is
|
|
707
|
+
* where it starts, used as it is when there are no years or months. Panics
|
|
708
|
+
* with a `RangeError` outside Temporal's range.
|
|
709
|
+
*/
|
|
710
|
+
const temporalAddToDate = (
|
|
711
|
+
year0: i32,
|
|
712
|
+
month0: i32,
|
|
713
|
+
day0: i32,
|
|
714
|
+
epochDay: i64,
|
|
715
|
+
years: i64,
|
|
716
|
+
months: i64,
|
|
717
|
+
days: i64
|
|
718
|
+
): i64 => {
|
|
719
|
+
let start: i64 = epochDay
|
|
720
|
+
if (years !== 0 || months !== 0) {
|
|
721
|
+
let year: i64 = toI64(year0) + years
|
|
722
|
+
let month: i64 = toI64(month0) + months
|
|
723
|
+
year = year + temporalFloorDiv(month - 1, 12)
|
|
724
|
+
month = temporalFloorMod(month - 1, 12) + 1
|
|
725
|
+
if (year < -271821 || year > 275760) {
|
|
726
|
+
temporalRangeError("PlainDate: the date is outside Temporal's range")
|
|
727
|
+
}
|
|
728
|
+
const most: i32 = temporalDaysInMonth(year, toI32(month))
|
|
729
|
+
start = temporalEpochDay(year, toI32(month), day0 > most ? most : day0)
|
|
730
|
+
}
|
|
731
|
+
const result: i64 = start + days
|
|
732
|
+
if (result < TEMPORAL_MIN_EPOCH_DAY || result > TEMPORAL_MAX_EPOCH_DAY) {
|
|
733
|
+
temporalRangeError("PlainDate: the date is outside Temporal's range")
|
|
734
|
+
}
|
|
735
|
+
return result
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* `span` as a duration whose largest field is `largest` (a day at most), as
|
|
740
|
+
* Temporal's BalanceTimeDuration writes one: every field has the span's sign.
|
|
741
|
+
*/
|
|
742
|
+
const temporalBalance = (span: TemporalSpan, largest: i32): Duration => {
|
|
743
|
+
const sign: i32 = span.sign()
|
|
744
|
+
const m: TemporalSpan = sign < 0 ? span.negated() : span
|
|
745
|
+
const s: i64 = m.seconds
|
|
746
|
+
const n: i64 = toI64(m.nanos)
|
|
747
|
+
let days: i64 = 0
|
|
748
|
+
let hours: i64 = 0
|
|
749
|
+
let minutes: i64 = 0
|
|
750
|
+
let seconds: i64 = 0
|
|
751
|
+
let ms: i64 = temporalTruncDiv(n, 1000000)
|
|
752
|
+
let us: i64 = temporalTruncDiv(n, 1000) % 1000
|
|
753
|
+
let ns: i64 = n % 1000
|
|
754
|
+
if (largest >= TEMPORAL_UNIT_DAY) {
|
|
755
|
+
days = temporalTruncDiv(s, TEMPORAL_SECONDS_PER_DAY)
|
|
756
|
+
hours = temporalTruncDiv(s, 3600) % 24
|
|
757
|
+
minutes = temporalTruncDiv(s, 60) % 60
|
|
758
|
+
seconds = s % 60
|
|
759
|
+
} else if (largest === TEMPORAL_UNIT_HOUR) {
|
|
760
|
+
hours = temporalTruncDiv(s, 3600)
|
|
761
|
+
minutes = temporalTruncDiv(s, 60) % 60
|
|
762
|
+
seconds = s % 60
|
|
763
|
+
} else if (largest === TEMPORAL_UNIT_MINUTE) {
|
|
764
|
+
minutes = temporalTruncDiv(s, 60)
|
|
765
|
+
seconds = s % 60
|
|
766
|
+
} else if (largest === TEMPORAL_UNIT_SECOND) {
|
|
767
|
+
seconds = s
|
|
768
|
+
} else {
|
|
769
|
+
// A sub-second largest unit: the whole count in that unit must fit an i64.
|
|
770
|
+
let limit: i64 = 9007199
|
|
771
|
+
if (largest === TEMPORAL_UNIT_MILLISECOND) {
|
|
772
|
+
limit = 9007199254740
|
|
773
|
+
} else if (largest === TEMPORAL_UNIT_MICROSECOND) {
|
|
774
|
+
limit = 9007199254
|
|
775
|
+
}
|
|
776
|
+
if (s > limit) {
|
|
777
|
+
temporalRangeError("Duration: the length does not fit this module's i64 fields in that unit")
|
|
778
|
+
}
|
|
779
|
+
if (largest === TEMPORAL_UNIT_MILLISECOND) {
|
|
780
|
+
ms = s * 1000 + ms
|
|
781
|
+
} else if (largest === TEMPORAL_UNIT_MICROSECOND) {
|
|
782
|
+
us = s * 1000000 + ms * 1000 + us
|
|
783
|
+
ms = 0
|
|
784
|
+
} else {
|
|
785
|
+
ns = s * 1000000000 + ms * 1000000 + us * 1000 + ns
|
|
786
|
+
ms = 0
|
|
787
|
+
us = 0
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
const k: i64 = toI64(sign < 0 ? -1 : 1)
|
|
791
|
+
return new Duration(0, 0, 0, days * k, hours * k, minutes * k, seconds * k, ms * k, us * k, ns * k)
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// ---------------------------------------------------------------------------
|
|
795
|
+
// The string reader: RFC 9557 date-times (ISO 8601 with RFC 3339's offsets and
|
|
796
|
+
// bracketed annotations) and ISO 8601 durations, as Temporal's grammar reads
|
|
797
|
+
// them.
|
|
798
|
+
// ---------------------------------------------------------------------------
|
|
799
|
+
|
|
800
|
+
/** @internal One parse of a Temporal string; `ok` is false once anything is wrong. Not part of Temporal. */
|
|
801
|
+
class TemporalReader {
|
|
802
|
+
/** The text being read. */
|
|
803
|
+
readonly text: string
|
|
804
|
+
/** The offset's nanoseconds east of UTC, when `hasOffset`. */
|
|
805
|
+
offsetNanos: i64
|
|
806
|
+
/** The index of the next character. */
|
|
807
|
+
at: i32
|
|
808
|
+
/** The date's fields, when `hasDate`. */
|
|
809
|
+
year: i32
|
|
810
|
+
month: i32
|
|
811
|
+
day: i32
|
|
812
|
+
/** The time's fields, when `hasTime`; midnight otherwise. */
|
|
813
|
+
hour: i32
|
|
814
|
+
minute: i32
|
|
815
|
+
second: i32
|
|
816
|
+
/** The nanoseconds past `second`. */
|
|
817
|
+
fraction: i32
|
|
818
|
+
/** Whether the text has parsed so far. */
|
|
819
|
+
ok: boolean
|
|
820
|
+
/** Whether a date was read. */
|
|
821
|
+
hasDate: boolean
|
|
822
|
+
/** Whether a time was read. */
|
|
823
|
+
hasTime: boolean
|
|
824
|
+
/** Whether a `Z` was read in the offset's place. */
|
|
825
|
+
utc: boolean
|
|
826
|
+
/** Whether a numeric offset was read. */
|
|
827
|
+
hasOffset: boolean
|
|
828
|
+
/** Whether a bracketed time zone annotation was read. */
|
|
829
|
+
hasZone: boolean
|
|
830
|
+
/** Whether the string's calendar, if it names one, is `iso8601`. */
|
|
831
|
+
isoCalendar: boolean
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* A reader at the start of `text`.
|
|
835
|
+
*
|
|
836
|
+
* @param text the string to read
|
|
837
|
+
*/
|
|
838
|
+
constructor(text: string) {
|
|
839
|
+
this.text = text
|
|
840
|
+
this.at = 0
|
|
841
|
+
this.ok = true
|
|
842
|
+
this.hasDate = false
|
|
843
|
+
this.year = 0
|
|
844
|
+
this.month = 1
|
|
845
|
+
this.day = 1
|
|
846
|
+
this.hasTime = false
|
|
847
|
+
this.hour = 0
|
|
848
|
+
this.minute = 0
|
|
849
|
+
this.second = 0
|
|
850
|
+
this.fraction = 0
|
|
851
|
+
this.utc = false
|
|
852
|
+
this.hasOffset = false
|
|
853
|
+
this.offsetNanos = 0
|
|
854
|
+
this.hasZone = false
|
|
855
|
+
this.isoCalendar = true
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* The character code at `at + ahead`, or -1 past the end.
|
|
860
|
+
*
|
|
861
|
+
* @param ahead how far past `at` to look
|
|
862
|
+
* @returns the code
|
|
863
|
+
*/
|
|
864
|
+
peek(ahead: i32): i32 {
|
|
865
|
+
const i: i32 = this.at + ahead
|
|
866
|
+
if (i < 0 || i >= toI32(this.text.length)) {
|
|
867
|
+
return -1
|
|
868
|
+
}
|
|
869
|
+
return toI32(this.text.charCodeAt(i))
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* Whether the character `ahead` past `at` is a decimal digit.
|
|
874
|
+
*
|
|
875
|
+
* @param ahead how far past `at` to look
|
|
876
|
+
* @returns whether it is one
|
|
877
|
+
*/
|
|
878
|
+
digitAt(ahead: i32): boolean {
|
|
879
|
+
const c: i32 = this.peek(ahead)
|
|
880
|
+
return c >= 48 && c <= 57
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/**
|
|
884
|
+
* Whether every character has been read.
|
|
885
|
+
*
|
|
886
|
+
* @returns whether the reader is at the end
|
|
887
|
+
*/
|
|
888
|
+
done(): boolean {
|
|
889
|
+
return this.at >= toI32(this.text.length)
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Consume `c` if it is next.
|
|
894
|
+
*
|
|
895
|
+
* @param c the character code
|
|
896
|
+
* @returns whether it was there
|
|
897
|
+
*/
|
|
898
|
+
eat(c: i32): boolean {
|
|
899
|
+
if (this.peek(toI32(0)) === c) {
|
|
900
|
+
this.at = this.at + 1
|
|
901
|
+
return true
|
|
902
|
+
}
|
|
903
|
+
return false
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Read exactly `count` digits as a number, or mark the parse failed.
|
|
908
|
+
*
|
|
909
|
+
* @param count how many digits
|
|
910
|
+
* @returns their value, or 0 on failure
|
|
911
|
+
*/
|
|
912
|
+
digits(count: i32): i32 {
|
|
913
|
+
let value: i32 = 0
|
|
914
|
+
for (let i: i32 = 0; i < count; i++) {
|
|
915
|
+
if (!this.digitAt(toI32(0))) {
|
|
916
|
+
this.ok = false
|
|
917
|
+
return 0
|
|
918
|
+
}
|
|
919
|
+
value = value * 10 + this.peek(toI32(0)) - 48
|
|
920
|
+
this.at = this.at + 1
|
|
921
|
+
}
|
|
922
|
+
return value
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Read a `.` or `,` and one to nine digits as nanoseconds, if one is next.
|
|
927
|
+
*
|
|
928
|
+
* @returns the nanoseconds the fraction names, or 0 when there is none
|
|
929
|
+
*/
|
|
930
|
+
fractionDigits(): i32 {
|
|
931
|
+
const c: i32 = this.peek(toI32(0))
|
|
932
|
+
if ((c !== 46 && c !== 44) || !this.digitAt(toI32(1))) {
|
|
933
|
+
return 0
|
|
934
|
+
}
|
|
935
|
+
this.at = this.at + 1
|
|
936
|
+
let value: i32 = 0
|
|
937
|
+
let count: i32 = 0
|
|
938
|
+
while (this.digitAt(toI32(0))) {
|
|
939
|
+
if (count === 9) {
|
|
940
|
+
this.ok = false
|
|
941
|
+
return 0
|
|
942
|
+
}
|
|
943
|
+
value = value * 10 + this.peek(toI32(0)) - 48
|
|
944
|
+
count = count + 1
|
|
945
|
+
this.at = this.at + 1
|
|
946
|
+
}
|
|
947
|
+
while (count < 9) {
|
|
948
|
+
value = value * 10
|
|
949
|
+
count = count + 1
|
|
950
|
+
}
|
|
951
|
+
return value
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/** Read a date: `YYYY-MM-DD`, `±YYYYYY-MM-DD`, or either without the dashes. */
|
|
955
|
+
readDate(): void {
|
|
956
|
+
const sign: i32 = this.peek(toI32(0))
|
|
957
|
+
let year: i32 = 0
|
|
958
|
+
if (sign === 43 || sign === 45) {
|
|
959
|
+
this.at = this.at + 1
|
|
960
|
+
year = this.digits(toI32(6))
|
|
961
|
+
if (sign !== 43 && year === 0) {
|
|
962
|
+
this.ok = false // -000000 is not a year
|
|
963
|
+
}
|
|
964
|
+
if (sign !== 43) {
|
|
965
|
+
year = -year
|
|
966
|
+
}
|
|
967
|
+
} else {
|
|
968
|
+
year = this.digits(toI32(4))
|
|
969
|
+
}
|
|
970
|
+
const extended: boolean = this.eat(toI32(45))
|
|
971
|
+
const month: i32 = this.digits(toI32(2))
|
|
972
|
+
if (extended && !this.eat(toI32(45))) {
|
|
973
|
+
this.ok = false
|
|
974
|
+
}
|
|
975
|
+
const day: i32 = this.digits(toI32(2))
|
|
976
|
+
if (!this.ok) {
|
|
977
|
+
return
|
|
978
|
+
}
|
|
979
|
+
if (month < 1 || month > 12 || day < 1 || day > temporalDaysInMonth(toI64(year), month)) {
|
|
980
|
+
this.ok = false
|
|
981
|
+
return
|
|
982
|
+
}
|
|
983
|
+
this.hasDate = true
|
|
984
|
+
this.year = year
|
|
985
|
+
this.month = month
|
|
986
|
+
this.day = day
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/** Read a time: `HH`, `HH:MM`, `HH:MM:SS[.f]`, or the same without colons. */
|
|
990
|
+
readTime(): void {
|
|
991
|
+
this.hour = this.digits(toI32(2))
|
|
992
|
+
if (this.digitAt(toI32(0)) || this.peek(toI32(0)) === 58) {
|
|
993
|
+
const extended: boolean = this.eat(toI32(58))
|
|
994
|
+
this.minute = this.digits(toI32(2))
|
|
995
|
+
if ((extended && this.peek(toI32(0)) === 58) || (!extended && this.digitAt(toI32(0)))) {
|
|
996
|
+
if (extended) {
|
|
997
|
+
this.at = this.at + 1
|
|
998
|
+
}
|
|
999
|
+
this.second = this.digits(toI32(2))
|
|
1000
|
+
this.fraction = this.fractionDigits()
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
if (this.second === 60) {
|
|
1004
|
+
this.second = 59 // a leap second reads as the second before it
|
|
1005
|
+
}
|
|
1006
|
+
if (this.hour > 23 || this.minute > 59 || this.second > 59) {
|
|
1007
|
+
this.ok = false
|
|
1008
|
+
}
|
|
1009
|
+
this.hasTime = this.ok
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/** Read `Z`, or a `±HH[:MM[:SS[.f]]]` offset, if one is next. */
|
|
1013
|
+
readOffset(): void {
|
|
1014
|
+
const c: i32 = this.peek(toI32(0))
|
|
1015
|
+
if (c === 90 || c === 122) {
|
|
1016
|
+
this.at = this.at + 1
|
|
1017
|
+
this.utc = true
|
|
1018
|
+
return
|
|
1019
|
+
}
|
|
1020
|
+
if (c !== 43 && c !== 45) {
|
|
1021
|
+
return
|
|
1022
|
+
}
|
|
1023
|
+
this.at = this.at + 1
|
|
1024
|
+
const hours: i32 = this.digits(toI32(2))
|
|
1025
|
+
let minutes: i32 = 0
|
|
1026
|
+
let seconds: i32 = 0
|
|
1027
|
+
let fraction: i32 = 0
|
|
1028
|
+
if (this.digitAt(toI32(0)) || this.peek(toI32(0)) === 58) {
|
|
1029
|
+
const extended: boolean = this.eat(toI32(58))
|
|
1030
|
+
minutes = this.digits(toI32(2))
|
|
1031
|
+
if ((extended && this.peek(toI32(0)) === 58) || (!extended && this.digitAt(toI32(0)))) {
|
|
1032
|
+
if (extended) {
|
|
1033
|
+
this.at = this.at + 1
|
|
1034
|
+
}
|
|
1035
|
+
seconds = this.digits(toI32(2))
|
|
1036
|
+
fraction = this.fractionDigits()
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
if (hours > 23 || minutes > 59 || seconds > 59) {
|
|
1040
|
+
this.ok = false
|
|
1041
|
+
return
|
|
1042
|
+
}
|
|
1043
|
+
const total: i64 =
|
|
1044
|
+
(toI64(hours) * 3600 + toI64(minutes) * 60 + toI64(seconds)) * TEMPORAL_NS_PER_SECOND + toI64(fraction)
|
|
1045
|
+
this.hasOffset = true
|
|
1046
|
+
this.offsetNanos = c === 43 ? total : -total
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* Read the bracketed annotations: at most one time zone, first, then
|
|
1051
|
+
* `key=value` pairs, each spelled as the grammar spells them. Two calendars
|
|
1052
|
+
* where either is critical, and an unknown critical key, fail the parse; a
|
|
1053
|
+
* calendar other than `iso8601` only clears `isoCalendar`, for the types
|
|
1054
|
+
* that hold a calendar to refuse.
|
|
1055
|
+
*/
|
|
1056
|
+
readAnnotations(): void {
|
|
1057
|
+
let calendars: i32 = 0
|
|
1058
|
+
let criticalCalendar: boolean = false
|
|
1059
|
+
let first: boolean = true
|
|
1060
|
+
while (this.ok && this.eat(toI32(91))) {
|
|
1061
|
+
const critical: boolean = this.eat(toI32(33))
|
|
1062
|
+
const start: i32 = this.at
|
|
1063
|
+
while (!this.done() && this.peek(toI32(0)) !== 93) {
|
|
1064
|
+
this.at = this.at + 1
|
|
1065
|
+
}
|
|
1066
|
+
if (this.done() || this.at === start) {
|
|
1067
|
+
this.ok = false
|
|
1068
|
+
return
|
|
1069
|
+
}
|
|
1070
|
+
// The annotation is `text[start, end)`, read in place: a substring per
|
|
1071
|
+
// pass would be kept until the caller's memory is released (NL9018).
|
|
1072
|
+
const end: i32 = this.at
|
|
1073
|
+
this.at = this.at + 1
|
|
1074
|
+
let eq: i32 = start
|
|
1075
|
+
while (eq < end && temporalCharAt(this.text, eq) !== 61) {
|
|
1076
|
+
eq = eq + 1
|
|
1077
|
+
}
|
|
1078
|
+
if (eq === end) {
|
|
1079
|
+
// A time zone: only first, only once, and spelled as the grammar's
|
|
1080
|
+
// TimeZoneIdentifier. Whether the zone exists is not asked: no type
|
|
1081
|
+
// here holds one.
|
|
1082
|
+
if (!first || !temporalIsTimeZoneName(this.text, start, end)) {
|
|
1083
|
+
this.ok = false
|
|
1084
|
+
return
|
|
1085
|
+
}
|
|
1086
|
+
this.hasZone = true
|
|
1087
|
+
} else {
|
|
1088
|
+
const text: string = this.text
|
|
1089
|
+
if (!temporalIsAnnotationKey(text, start, eq) || !temporalIsAnnotationValue(text, eq + 1, end)) {
|
|
1090
|
+
this.ok = false
|
|
1091
|
+
return
|
|
1092
|
+
}
|
|
1093
|
+
if (temporalRangeIs(text, start, eq, "u-ca")) {
|
|
1094
|
+
calendars = calendars + 1
|
|
1095
|
+
criticalCalendar = criticalCalendar || critical
|
|
1096
|
+
// The first calendar is the string's; only a type that holds a
|
|
1097
|
+
// calendar refuses one other than iso8601 (`isoCalendar`).
|
|
1098
|
+
if (calendars === 1) {
|
|
1099
|
+
this.isoCalendar = temporalRangeIs(text, eq + 1, end, "iso8601")
|
|
1100
|
+
}
|
|
1101
|
+
} else if (critical) {
|
|
1102
|
+
this.ok = false
|
|
1103
|
+
return
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
first = false
|
|
1107
|
+
}
|
|
1108
|
+
if (calendars > 1 && criticalCalendar) {
|
|
1109
|
+
this.ok = false
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
/**
|
|
1114
|
+
* Read a whole date-time string: a date, then optionally `T`, `t` or a space
|
|
1115
|
+
* and a time, an offset, and annotations, to the end of the text.
|
|
1116
|
+
*/
|
|
1117
|
+
readDateTime(): void {
|
|
1118
|
+
this.readDate()
|
|
1119
|
+
if (!this.ok) {
|
|
1120
|
+
return
|
|
1121
|
+
}
|
|
1122
|
+
const c: i32 = this.peek(toI32(0))
|
|
1123
|
+
if ((c === 84 || c === 116 || c === 32) && this.digitAt(toI32(1))) {
|
|
1124
|
+
this.at = this.at + 1
|
|
1125
|
+
this.readTime()
|
|
1126
|
+
if (this.ok) {
|
|
1127
|
+
this.readOffset()
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
if (this.ok) {
|
|
1131
|
+
this.readAnnotations()
|
|
1132
|
+
}
|
|
1133
|
+
if (!this.done()) {
|
|
1134
|
+
this.ok = false
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// The annotation checks read `text[from, to)` in place, so that reading
|
|
1140
|
+
// annotations allocates nothing.
|
|
1141
|
+
|
|
1142
|
+
/** Whether `text[from, to)` is an annotation key: a lowercase letter or `_`, then lowercase letters, digits, `_` or `-`. */
|
|
1143
|
+
const temporalIsAnnotationKey = (text: string, from: i32, to: i32): boolean => {
|
|
1144
|
+
if (to <= from) {
|
|
1145
|
+
return false
|
|
1146
|
+
}
|
|
1147
|
+
for (let i: i32 = from; i < to; i++) {
|
|
1148
|
+
const c: i32 = temporalCharAt(text, i)
|
|
1149
|
+
const letter: boolean = (c >= 97 && c <= 122) || c === 95
|
|
1150
|
+
if (!letter && (i === from || !((c >= 48 && c <= 57) || c === 45))) {
|
|
1151
|
+
return false
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
return true
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/** Whether `c` is an ASCII letter or digit. */
|
|
1158
|
+
const temporalIsAlphaNumeric = (c: i32): boolean =>
|
|
1159
|
+
(c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)
|
|
1160
|
+
|
|
1161
|
+
/** Whether `text[from, to)` is an annotation value: runs of ASCII letters and digits joined by single `-`. */
|
|
1162
|
+
const temporalIsAnnotationValue = (text: string, from: i32, to: i32): boolean => {
|
|
1163
|
+
let run: i32 = 0
|
|
1164
|
+
for (let i: i32 = from; i < to; i++) {
|
|
1165
|
+
const c: i32 = temporalCharAt(text, i)
|
|
1166
|
+
if (temporalIsAlphaNumeric(c)) {
|
|
1167
|
+
run = run + 1
|
|
1168
|
+
} else if (c === 45 && run > 0) {
|
|
1169
|
+
run = 0
|
|
1170
|
+
} else {
|
|
1171
|
+
return false
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
return run > 0
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
/** The two decimal digits at `at` as a number, or -1, for the annotation checks. */
|
|
1178
|
+
const temporalAnnotationTwo = (text: string, at: i32): i32 => {
|
|
1179
|
+
const high: i32 = temporalCharAt(text, at) - 48
|
|
1180
|
+
const low: i32 = temporalCharAt(text, at + 1) - 48
|
|
1181
|
+
return high >= 0 && high <= 9 && low >= 0 && low <= 9 ? high * 10 + low : -1
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
/**
|
|
1185
|
+
* Whether `text[from, to)` is a TimeZoneIdentifier by Temporal's grammar: an
|
|
1186
|
+
* offset, `±HH`, `±HHMM` or `±HH:MM`, or names joined by `/`, each starting
|
|
1187
|
+
* with a letter, `.` or `_` and going on in letters, digits, `.`, `_`, `-` or
|
|
1188
|
+
* `+`.
|
|
1189
|
+
*/
|
|
1190
|
+
const temporalIsTimeZoneName = (text: string, from: i32, to: i32): boolean => {
|
|
1191
|
+
const n: i32 = to - from
|
|
1192
|
+
const first: i32 = temporalCharAt(text, from)
|
|
1193
|
+
if (first === 43 || first === 45) {
|
|
1194
|
+
const hours: i32 = temporalAnnotationTwo(text, from + 1)
|
|
1195
|
+
if (hours < 0 || hours > 23) {
|
|
1196
|
+
return false
|
|
1197
|
+
}
|
|
1198
|
+
if (n === 3) {
|
|
1199
|
+
return true
|
|
1200
|
+
}
|
|
1201
|
+
const at: i32 = n === 6 && temporalCharAt(text, from + 3) === 58 ? from + 4 : from + 3
|
|
1202
|
+
const minutes: i32 = temporalAnnotationTwo(text, at)
|
|
1203
|
+
return (n === 5 || n === 6) && at + 2 === to && minutes >= 0 && minutes <= 59
|
|
1204
|
+
}
|
|
1205
|
+
let length: i32 = 0
|
|
1206
|
+
for (let i: i32 = from; i <= to; i++) {
|
|
1207
|
+
const c: i32 = i < to ? temporalCharAt(text, i) : 47
|
|
1208
|
+
if (c === 47) {
|
|
1209
|
+
if (length === 0) {
|
|
1210
|
+
return false
|
|
1211
|
+
}
|
|
1212
|
+
length = 0
|
|
1213
|
+
} else {
|
|
1214
|
+
const leading: boolean = (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 46 || c === 95
|
|
1215
|
+
if (!leading && (length === 0 || !(temporalIsAlphaNumeric(c) || c === 45 || c === 43))) {
|
|
1216
|
+
return false
|
|
1217
|
+
}
|
|
1218
|
+
length = length + 1
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
return true
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
/** Whether `text[from, to)` is `want`, ignoring the case of ASCII letters; `want` is lowercase. */
|
|
1225
|
+
const temporalRangeIs = (text: string, from: i32, to: i32, want: string): boolean => {
|
|
1226
|
+
if (to - from !== toI32(want.length)) {
|
|
1227
|
+
return false
|
|
1228
|
+
}
|
|
1229
|
+
for (let i: i32 = 0; i < to - from; i++) {
|
|
1230
|
+
let c: i32 = temporalCharAt(text, from + i)
|
|
1231
|
+
if (c >= 65 && c <= 90) {
|
|
1232
|
+
c = c + 32
|
|
1233
|
+
}
|
|
1234
|
+
if (c !== temporalCharAt(want, i)) {
|
|
1235
|
+
return false
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
return true
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
// The fast paths. Nearly every string a program reads is in the one form
|
|
1242
|
+
// `toString` writes: `YYYY-MM-DD`, then `THH:MM`, `:SS` and a fraction. These
|
|
1243
|
+
// read exactly that form, straight from the string, with no reader to
|
|
1244
|
+
// allocate and no range to check (a four-digit year is always inside it).
|
|
1245
|
+
// Anything else, valid or not, answers -1 and goes to `TemporalReader`, so a
|
|
1246
|
+
// fast path accepts only strings the reader would read the same way.
|
|
1247
|
+
|
|
1248
|
+
/** The character code at `at`, or -1 outside the text. */
|
|
1249
|
+
const temporalCharAt = (text: string, at: i32): i32 =>
|
|
1250
|
+
at >= 0 && at < toI32(text.length) ? toI32(text.charCodeAt(at)) : -1
|
|
1251
|
+
|
|
1252
|
+
/** Whether `c` is `T`, `t` or a space, which may stand between a date and a time. */
|
|
1253
|
+
const temporalIsTimeDesignator = (c: i32): boolean => c === 84 || c === 116 || c === 32
|
|
1254
|
+
|
|
1255
|
+
/** The two decimal digits at `at` as a number, or -1 when either is not a digit. */
|
|
1256
|
+
const temporalFastTwo = (text: string, at: i32): i32 => {
|
|
1257
|
+
const high: i32 = temporalCharAt(text, at) - 48
|
|
1258
|
+
const low: i32 = temporalCharAt(text, at + 1) - 48
|
|
1259
|
+
if (high < 0 || high > 9 || low < 0 || low > 9) {
|
|
1260
|
+
return -1
|
|
1261
|
+
}
|
|
1262
|
+
return high * 10 + low
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
/**
|
|
1266
|
+
* The date `YYYY-MM-DD` at the start of `text`, packed as
|
|
1267
|
+
* `temporalCivilOfEpochDay` packs one, or -1 when the text does not start
|
|
1268
|
+
* with one in exactly that form or the date does not exist.
|
|
1269
|
+
*/
|
|
1270
|
+
const temporalFastDate = (text: string): i64 => {
|
|
1271
|
+
const century: i32 = temporalFastTwo(text, 0)
|
|
1272
|
+
const yy: i32 = temporalFastTwo(text, 2)
|
|
1273
|
+
const month: i32 = temporalFastTwo(text, 5)
|
|
1274
|
+
const day: i32 = temporalFastTwo(text, 8)
|
|
1275
|
+
if (century < 0 || yy < 0 || month < 1 || month > 12 || day < 1) {
|
|
1276
|
+
return -1
|
|
1277
|
+
}
|
|
1278
|
+
if (temporalCharAt(text, 4) !== 45 || temporalCharAt(text, 7) !== 45) {
|
|
1279
|
+
return -1
|
|
1280
|
+
}
|
|
1281
|
+
const year: i32 = century * 100 + yy
|
|
1282
|
+
if (day > temporalDaysInMonth(toI64(year), month)) {
|
|
1283
|
+
return -1
|
|
1284
|
+
}
|
|
1285
|
+
return (toI64(year) + 400000) * 512 + toI64(month) * 32 + toI64(day)
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* The time `HH:MM`, `HH:MM:SS` or `HH:MM:SS.f` that runs from `at` to
|
|
1290
|
+
* exactly `end`, as nanoseconds since midnight, or -1 when it is not in that
|
|
1291
|
+
* form. Second 60 reads as 59, as the reader reads it.
|
|
1292
|
+
*/
|
|
1293
|
+
const temporalFastTime = (text: string, at: i32, end: i32): i64 => {
|
|
1294
|
+
const hour: i32 = temporalFastTwo(text, at)
|
|
1295
|
+
const minute: i32 = temporalFastTwo(text, at + 3)
|
|
1296
|
+
if (hour < 0 || hour > 23 || minute < 0 || minute > 59 || temporalCharAt(text, at + 2) !== 58) {
|
|
1297
|
+
return -1
|
|
1298
|
+
}
|
|
1299
|
+
let second: i32 = 0
|
|
1300
|
+
let fraction: i32 = 0
|
|
1301
|
+
let i: i32 = at + 5
|
|
1302
|
+
if (i < end) {
|
|
1303
|
+
second = temporalFastTwo(text, i + 1)
|
|
1304
|
+
if (temporalCharAt(text, i) !== 58 || second < 0 || second > 60) {
|
|
1305
|
+
return -1
|
|
1306
|
+
}
|
|
1307
|
+
if (second === 60) {
|
|
1308
|
+
second = 59
|
|
1309
|
+
}
|
|
1310
|
+
i = i + 3
|
|
1311
|
+
if (i < end) {
|
|
1312
|
+
const dot: i32 = temporalCharAt(text, i)
|
|
1313
|
+
const digits: i32 = end - i - 1
|
|
1314
|
+
if ((dot !== 46 && dot !== 44) || digits < 1 || digits > 9) {
|
|
1315
|
+
return -1
|
|
1316
|
+
}
|
|
1317
|
+
for (let k: i32 = i + 1; k < end; k++) {
|
|
1318
|
+
const d: i32 = temporalCharAt(text, k) - 48
|
|
1319
|
+
if (d < 0 || d > 9) {
|
|
1320
|
+
return -1
|
|
1321
|
+
}
|
|
1322
|
+
fraction = fraction * 10 + d
|
|
1323
|
+
}
|
|
1324
|
+
for (let k: i32 = digits; k < 9; k++) {
|
|
1325
|
+
fraction = fraction * 10
|
|
1326
|
+
}
|
|
1327
|
+
i = end
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
if (i !== end) {
|
|
1331
|
+
return -1
|
|
1332
|
+
}
|
|
1333
|
+
return (toI64(hour) * 3600 + toI64(minute) * 60 + toI64(second)) * TEMPORAL_NS_PER_SECOND + toI64(fraction)
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
/**
|
|
1337
|
+
* Whether the date `r` read is inside Temporal's range: a date from
|
|
1338
|
+
* -271821-04-19 to +275760-09-13, and with `withTime`, a date-time after
|
|
1339
|
+
* -271821-04-19T00:00. The reader has already checked the fields.
|
|
1340
|
+
*/
|
|
1341
|
+
const temporalReadInRange = (r: TemporalReader, withTime: boolean): boolean => {
|
|
1342
|
+
const day: i64 = temporalEpochDay(toI64(r.year), r.month, r.day)
|
|
1343
|
+
if (day < TEMPORAL_MIN_EPOCH_DAY || day > TEMPORAL_MAX_EPOCH_DAY) {
|
|
1344
|
+
return false
|
|
1345
|
+
}
|
|
1346
|
+
const midnight: boolean = r.hour === 0 && r.minute === 0 && r.second === 0 && r.fraction === 0
|
|
1347
|
+
return !(withTime && day === TEMPORAL_MIN_EPOCH_DAY && midnight)
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
/** The reader after reading `text` as a date-time string. */
|
|
1351
|
+
const temporalReadDateTime = (text: string): TemporalReader => {
|
|
1352
|
+
const r: TemporalReader = new TemporalReader(text)
|
|
1353
|
+
r.readDateTime()
|
|
1354
|
+
return r
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
// ---------------------------------------------------------------------------
|
|
1358
|
+
// PlainDate
|
|
1359
|
+
// ---------------------------------------------------------------------------
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
* A calendar date with no time and no time zone: `Temporal.PlainDate` in the
|
|
1363
|
+
* ISO 8601 calendar. Every field is Temporal's getter of the same name.
|
|
1364
|
+
*/
|
|
1365
|
+
export class PlainDate {
|
|
1366
|
+
/** The month as Temporal codes it: `M01` to `M12`. */
|
|
1367
|
+
readonly monthCode: string
|
|
1368
|
+
/** The calendar, always `iso8601`. */
|
|
1369
|
+
readonly calendarId: string
|
|
1370
|
+
/** @internal The days since 1970-01-01; not part of Temporal. */
|
|
1371
|
+
readonly epochDay: i64
|
|
1372
|
+
/** The year; negative before 1 BCE's year 0. */
|
|
1373
|
+
readonly year: i32
|
|
1374
|
+
/** The month, 1 to 12. */
|
|
1375
|
+
readonly month: i32
|
|
1376
|
+
/** The day of the month, from 1. */
|
|
1377
|
+
readonly day: i32
|
|
1378
|
+
/** The weekday, 1 for Monday to 7 for Sunday. */
|
|
1379
|
+
readonly dayOfWeek: i32
|
|
1380
|
+
/** The day of the year, 1 for 1 January. */
|
|
1381
|
+
readonly dayOfYear: i32
|
|
1382
|
+
/** The ISO week number, 1 to 53. */
|
|
1383
|
+
readonly weekOfYear: i32
|
|
1384
|
+
/** The year the ISO week belongs to, which differs from `year` near New Year. */
|
|
1385
|
+
readonly yearOfWeek: i32
|
|
1386
|
+
/** Days in a week: 7. */
|
|
1387
|
+
readonly daysInWeek: i32
|
|
1388
|
+
/** Days in this date's month. */
|
|
1389
|
+
readonly daysInMonth: i32
|
|
1390
|
+
/** Days in this date's year: 365 or 366. */
|
|
1391
|
+
readonly daysInYear: i32
|
|
1392
|
+
/** Months in a year: 12. */
|
|
1393
|
+
readonly monthsInYear: i32
|
|
1394
|
+
/** Whether this date's year is a leap year. */
|
|
1395
|
+
readonly inLeapYear: boolean
|
|
1396
|
+
|
|
1397
|
+
/**
|
|
1398
|
+
* The date `isoYear-isoMonth-isoDay`, as `new Temporal.PlainDate(y, m, d)`.
|
|
1399
|
+
* Panics with a `RangeError` when the fields name no date, or one outside
|
|
1400
|
+
* Temporal's range of -271821-04-19 to +275760-09-13.
|
|
1401
|
+
*
|
|
1402
|
+
* @param isoYear the year
|
|
1403
|
+
* @param isoMonth the month, 1 to 12
|
|
1404
|
+
* @param isoDay the day of the month, from 1
|
|
1405
|
+
*/
|
|
1406
|
+
constructor(isoYear: i32, isoMonth: i32, isoDay: i32) {
|
|
1407
|
+
temporalCheckDate("PlainDate", toI64(isoYear), toI64(isoMonth), toI64(isoDay))
|
|
1408
|
+
const epochDay: i64 = temporalEpochDay(toI64(isoYear), isoMonth, isoDay)
|
|
1409
|
+
if (epochDay < TEMPORAL_MIN_EPOCH_DAY || epochDay > TEMPORAL_MAX_EPOCH_DAY) {
|
|
1410
|
+
temporalRangeError("PlainDate: the date is outside Temporal's range")
|
|
1411
|
+
}
|
|
1412
|
+
const y: i64 = toI64(isoYear)
|
|
1413
|
+
const leap: boolean = temporalIsLeapYear(y)
|
|
1414
|
+
const doy: i32 = temporalDayOfYear(isoMonth, isoDay, leap)
|
|
1415
|
+
const dow: i32 = temporalDayOfWeek(epochDay)
|
|
1416
|
+
let week: i32 = toI32((doy - dow + 10) / 7)
|
|
1417
|
+
let weekYear: i32 = isoYear
|
|
1418
|
+
// Only the first and last days of a year can be in another year's week, so
|
|
1419
|
+
// the 52-or-53 question is asked only there.
|
|
1420
|
+
if (week < 1) {
|
|
1421
|
+
weekYear = isoYear - 1
|
|
1422
|
+
week = temporalWeeksInYear(y - 1)
|
|
1423
|
+
} else if (week === 53 && temporalWeeksInYear(y) === 52) {
|
|
1424
|
+
weekYear = isoYear + 1
|
|
1425
|
+
week = 1
|
|
1426
|
+
}
|
|
1427
|
+
this.year = isoYear
|
|
1428
|
+
this.month = isoMonth
|
|
1429
|
+
this.day = isoDay
|
|
1430
|
+
this.monthCode = temporalMonthCode(isoMonth)
|
|
1431
|
+
this.calendarId = "iso8601"
|
|
1432
|
+
this.dayOfWeek = dow
|
|
1433
|
+
this.dayOfYear = doy
|
|
1434
|
+
this.weekOfYear = week
|
|
1435
|
+
this.yearOfWeek = weekYear
|
|
1436
|
+
this.daysInWeek = 7
|
|
1437
|
+
this.daysInMonth = temporalMonthLength(isoMonth, leap)
|
|
1438
|
+
this.daysInYear = leap ? 366 : 365
|
|
1439
|
+
this.monthsInYear = 12
|
|
1440
|
+
this.inLeapYear = leap
|
|
1441
|
+
this.epochDay = epochDay
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
/**
|
|
1445
|
+
* The date as Temporal writes it: `2024-01-15`, or `+012345-01-15` for a
|
|
1446
|
+
* year past 9999 or before 0.
|
|
1447
|
+
*
|
|
1448
|
+
* @returns the ISO 8601 form
|
|
1449
|
+
*/
|
|
1450
|
+
toString(): string {
|
|
1451
|
+
const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
|
|
1452
|
+
if (this.year >= 0 && this.year <= 9999) {
|
|
1453
|
+
return String.fromBytes(out, 0, temporalPutDate4(out, 0, this.year, this.month, this.day))
|
|
1454
|
+
}
|
|
1455
|
+
const end: i32 = temporalPutDate(out, 0, this.year, this.month, this.day)
|
|
1456
|
+
return String.fromBytes(out, 0, end)
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
/**
|
|
1460
|
+
* The same string as `toString`, which is what `JSON.stringify` writes.
|
|
1461
|
+
*
|
|
1462
|
+
* @returns the ISO 8601 form
|
|
1463
|
+
*/
|
|
1464
|
+
toJSON(): string {
|
|
1465
|
+
return this.toString()
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
/**
|
|
1469
|
+
* Whether `other` is the same date.
|
|
1470
|
+
*
|
|
1471
|
+
* @param other the date to compare with
|
|
1472
|
+
* @returns whether the two are equal
|
|
1473
|
+
*/
|
|
1474
|
+
equals(other: PlainDate): boolean {
|
|
1475
|
+
return this.epochDay === other.epochDay
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
/**
|
|
1479
|
+
* This date moved by `duration`: its years and months first, with a day
|
|
1480
|
+
* past the end of the new month moved back to its last day (Temporal's
|
|
1481
|
+
* `overflow: "constrain"`), then its weeks and days, then whole days of its
|
|
1482
|
+
* time fields. Panics with a `RangeError` when the answer is outside
|
|
1483
|
+
* Temporal's range.
|
|
1484
|
+
*
|
|
1485
|
+
* @param duration how far to move
|
|
1486
|
+
* @returns the new date
|
|
1487
|
+
*/
|
|
1488
|
+
add(duration: Duration): PlainDate {
|
|
1489
|
+
let days: i64 = duration.weeks * 7 + duration.days
|
|
1490
|
+
if (!temporalNoTime(duration)) {
|
|
1491
|
+
days = days + temporalSpanOfDuration(duration, false).truncDays()
|
|
1492
|
+
}
|
|
1493
|
+
const epochDay: i64 = temporalAddToDate(
|
|
1494
|
+
this.year,
|
|
1495
|
+
this.month,
|
|
1496
|
+
this.day,
|
|
1497
|
+
this.epochDay,
|
|
1498
|
+
duration.years,
|
|
1499
|
+
duration.months,
|
|
1500
|
+
days
|
|
1501
|
+
)
|
|
1502
|
+
return temporalDateOfEpochDay(epochDay)
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
/**
|
|
1506
|
+
* This date moved back by `duration`: `add` of its negation.
|
|
1507
|
+
*
|
|
1508
|
+
* @param duration how far to move back
|
|
1509
|
+
* @returns the new date
|
|
1510
|
+
*/
|
|
1511
|
+
subtract(duration: Duration): PlainDate {
|
|
1512
|
+
return this.add(duration.negated())
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/**
|
|
1516
|
+
* The days from this date to `other`, as a duration of days alone
|
|
1517
|
+
* (Temporal's default `largestUnit: "day"`); negative when `other` is
|
|
1518
|
+
* earlier.
|
|
1519
|
+
*
|
|
1520
|
+
* @param other the later date
|
|
1521
|
+
* @returns the difference
|
|
1522
|
+
*/
|
|
1523
|
+
until(other: PlainDate): Duration {
|
|
1524
|
+
return new Duration(0, 0, 0, other.epochDay - this.epochDay)
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
/**
|
|
1528
|
+
* The days from `other` to this date: `other.until(this)`.
|
|
1529
|
+
*
|
|
1530
|
+
* @param other the earlier date
|
|
1531
|
+
* @returns the difference
|
|
1532
|
+
*/
|
|
1533
|
+
since(other: PlainDate): Duration {
|
|
1534
|
+
return new Duration(0, 0, 0, this.epochDay - other.epochDay)
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
/**
|
|
1538
|
+
* This date at `time`.
|
|
1539
|
+
*
|
|
1540
|
+
* @param time the time of day
|
|
1541
|
+
* @returns the date-time
|
|
1542
|
+
*/
|
|
1543
|
+
toPlainDateTime(time: PlainTime): PlainDateTime {
|
|
1544
|
+
return new PlainDateTime(
|
|
1545
|
+
this.year,
|
|
1546
|
+
this.month,
|
|
1547
|
+
this.day,
|
|
1548
|
+
time.hour,
|
|
1549
|
+
time.minute,
|
|
1550
|
+
time.second,
|
|
1551
|
+
time.millisecond,
|
|
1552
|
+
time.microsecond,
|
|
1553
|
+
time.nanosecond
|
|
1554
|
+
)
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
/**
|
|
1559
|
+
* `Temporal.PlainDate.from(text)`: the date an RFC 9557 string names. A time,
|
|
1560
|
+
* an offset and annotations may follow the date and are ignored but for a
|
|
1561
|
+
* calendar, which must be `iso8601`; a `Z` is refused, as it is in Temporal.
|
|
1562
|
+
* Panics with a `RangeError` on anything else.
|
|
1563
|
+
*
|
|
1564
|
+
* @param text such as `2024-01-15`, `20240115` or `2024-01-15T12:00[u-ca=iso8601]`
|
|
1565
|
+
* @returns the date
|
|
1566
|
+
*/
|
|
1567
|
+
export const plainDateFrom = (text: string): PlainDate => {
|
|
1568
|
+
const date: PlainDate | null = plainDateFromOrNull(text)
|
|
1569
|
+
if (date === null) {
|
|
1570
|
+
temporalRangeError(`PlainDate: cannot parse "${text}"`)
|
|
1571
|
+
return new PlainDate(1970, 1, 1)
|
|
1572
|
+
}
|
|
1573
|
+
return date
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
/**
|
|
1577
|
+
* `plainDateFrom`, answering `null` instead of panicking on a string it
|
|
1578
|
+
* refuses. Not part of Temporal.
|
|
1579
|
+
*
|
|
1580
|
+
* @param text the string to parse
|
|
1581
|
+
* @returns the date, or `null`
|
|
1582
|
+
*/
|
|
1583
|
+
export const plainDateFromOrNull = (text: string): PlainDate | null => {
|
|
1584
|
+
if (text.length === 10) {
|
|
1585
|
+
const civil: i64 = temporalFastDate(text)
|
|
1586
|
+
if (civil >= 0) {
|
|
1587
|
+
return new PlainDate(temporalCivilYear(civil), temporalCivilMonth(civil), temporalCivilDay(civil))
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
const r: TemporalReader = temporalReadDateTime(text)
|
|
1591
|
+
if (!r.ok || r.utc || !r.isoCalendar || !temporalReadInRange(r, false)) {
|
|
1592
|
+
return null
|
|
1593
|
+
}
|
|
1594
|
+
return new PlainDate(r.year, r.month, r.day)
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
/**
|
|
1598
|
+
* `Temporal.PlainDate.compare(a, b)`: -1, 0 or 1 as `a` is before, the same
|
|
1599
|
+
* as, or after `b`, which is the comparator `sort` takes.
|
|
1600
|
+
*
|
|
1601
|
+
* @param a the first date
|
|
1602
|
+
* @param b the second date
|
|
1603
|
+
* @returns the order of the two
|
|
1604
|
+
*/
|
|
1605
|
+
export const plainDateCompare = (a: PlainDate, b: PlainDate): i32 => temporalSign(a.epochDay - b.epochDay)
|
|
1606
|
+
|
|
1607
|
+
// ---------------------------------------------------------------------------
|
|
1608
|
+
// PlainTime
|
|
1609
|
+
// ---------------------------------------------------------------------------
|
|
1610
|
+
|
|
1611
|
+
/** A time of day with no date and no time zone: `Temporal.PlainTime`. */
|
|
1612
|
+
export class PlainTime {
|
|
1613
|
+
/** The hour, 0 to 23. */
|
|
1614
|
+
readonly hour: i32
|
|
1615
|
+
/** The minute, 0 to 59. */
|
|
1616
|
+
readonly minute: i32
|
|
1617
|
+
/** The second, 0 to 59. */
|
|
1618
|
+
readonly second: i32
|
|
1619
|
+
/** The millisecond, 0 to 999. */
|
|
1620
|
+
readonly millisecond: i32
|
|
1621
|
+
/** The microsecond, 0 to 999. */
|
|
1622
|
+
readonly microsecond: i32
|
|
1623
|
+
/** The nanosecond, 0 to 999. */
|
|
1624
|
+
readonly nanosecond: i32
|
|
1625
|
+
/** @internal Nanoseconds since midnight; not part of Temporal. */
|
|
1626
|
+
readonly nanoOfDay: i64
|
|
1627
|
+
|
|
1628
|
+
/**
|
|
1629
|
+
* The time `hour:minute:second.millisecond microsecond nanosecond`, as
|
|
1630
|
+
* `new Temporal.PlainTime(...)`; every field defaults to 0, so `new
|
|
1631
|
+
* PlainTime()` is midnight. Panics with a `RangeError` on a field out of
|
|
1632
|
+
* range.
|
|
1633
|
+
*
|
|
1634
|
+
* @param hour 0 to 23
|
|
1635
|
+
* @param minute 0 to 59
|
|
1636
|
+
* @param second 0 to 59
|
|
1637
|
+
* @param millisecond 0 to 999
|
|
1638
|
+
* @param microsecond 0 to 999
|
|
1639
|
+
* @param nanosecond 0 to 999
|
|
1640
|
+
*/
|
|
1641
|
+
constructor(
|
|
1642
|
+
hour: i32 = 0,
|
|
1643
|
+
minute: i32 = 0,
|
|
1644
|
+
second: i32 = 0,
|
|
1645
|
+
millisecond: i32 = 0,
|
|
1646
|
+
microsecond: i32 = 0,
|
|
1647
|
+
nanosecond: i32 = 0
|
|
1648
|
+
) {
|
|
1649
|
+
temporalCheckTime(
|
|
1650
|
+
"PlainTime",
|
|
1651
|
+
toI64(hour),
|
|
1652
|
+
toI64(minute),
|
|
1653
|
+
toI64(second),
|
|
1654
|
+
toI64(millisecond),
|
|
1655
|
+
toI64(microsecond),
|
|
1656
|
+
toI64(nanosecond)
|
|
1657
|
+
)
|
|
1658
|
+
this.hour = hour
|
|
1659
|
+
this.minute = minute
|
|
1660
|
+
this.second = second
|
|
1661
|
+
this.millisecond = millisecond
|
|
1662
|
+
this.microsecond = microsecond
|
|
1663
|
+
this.nanosecond = nanosecond
|
|
1664
|
+
this.nanoOfDay = temporalNanoOfDay(hour, minute, second, millisecond, microsecond, nanosecond)
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
/**
|
|
1668
|
+
* The time as Temporal writes it: `09:05:00`, with the fraction of the
|
|
1669
|
+
* second to as many digits as it needs (`09:05:00.25`).
|
|
1670
|
+
*
|
|
1671
|
+
* @returns the ISO 8601 form
|
|
1672
|
+
*/
|
|
1673
|
+
toString(): string {
|
|
1674
|
+
const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
|
|
1675
|
+
const sub: i32 = this.millisecond * 1000000 + this.microsecond * 1000 + this.nanosecond
|
|
1676
|
+
const end: i32 = temporalPutTime(out, 0, this.hour, this.minute, this.second, sub)
|
|
1677
|
+
return String.fromBytes(out, 0, end)
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
/**
|
|
1681
|
+
* The same string as `toString`.
|
|
1682
|
+
*
|
|
1683
|
+
* @returns the ISO 8601 form
|
|
1684
|
+
*/
|
|
1685
|
+
toJSON(): string {
|
|
1686
|
+
return this.toString()
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
/**
|
|
1690
|
+
* Whether `other` is the same time.
|
|
1691
|
+
*
|
|
1692
|
+
* @param other the time to compare with
|
|
1693
|
+
* @returns whether the two are equal
|
|
1694
|
+
*/
|
|
1695
|
+
equals(other: PlainTime): boolean {
|
|
1696
|
+
return this.nanoOfDay === other.nanoOfDay
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
/**
|
|
1700
|
+
* This time moved by the time fields of `duration`, wrapping past midnight;
|
|
1701
|
+
* the years, months, weeks and days are ignored, as Temporal ignores them.
|
|
1702
|
+
*
|
|
1703
|
+
* @param duration how far to move
|
|
1704
|
+
* @returns the new time
|
|
1705
|
+
*/
|
|
1706
|
+
add(duration: Duration): PlainTime {
|
|
1707
|
+
const span: TemporalSpan = temporalSpanOfDuration(duration, false)
|
|
1708
|
+
const seconds: i64 = temporalFloorMod(span.seconds, TEMPORAL_SECONDS_PER_DAY)
|
|
1709
|
+
const ns: i64 = seconds * TEMPORAL_NS_PER_SECOND + toI64(span.nanos) + this.nanoOfDay
|
|
1710
|
+
return temporalTimeOfNanos(temporalFloorMod(ns, TEMPORAL_NS_PER_DAY))
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
/**
|
|
1714
|
+
* This time moved back by `duration`: `add` of its negation.
|
|
1715
|
+
*
|
|
1716
|
+
* @param duration how far to move back
|
|
1717
|
+
* @returns the new time
|
|
1718
|
+
*/
|
|
1719
|
+
subtract(duration: Duration): PlainTime {
|
|
1720
|
+
return this.add(duration.negated())
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
/**
|
|
1724
|
+
* The time from this time to `other` on the same day, in hours and smaller
|
|
1725
|
+
* units (Temporal's default `largestUnit: "hour"`).
|
|
1726
|
+
*
|
|
1727
|
+
* @param other the later time
|
|
1728
|
+
* @returns the difference
|
|
1729
|
+
*/
|
|
1730
|
+
until(other: PlainTime): Duration {
|
|
1731
|
+
return temporalBalance(new TemporalSpan(0, other.nanoOfDay - this.nanoOfDay), TEMPORAL_UNIT_HOUR)
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
/**
|
|
1735
|
+
* The time from `other` to this time: `other.until(this)`.
|
|
1736
|
+
*
|
|
1737
|
+
* @param other the earlier time
|
|
1738
|
+
* @returns the difference
|
|
1739
|
+
*/
|
|
1740
|
+
since(other: PlainTime): Duration {
|
|
1741
|
+
return temporalBalance(new TemporalSpan(0, this.nanoOfDay - other.nanoOfDay), TEMPORAL_UNIT_HOUR)
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
/**
|
|
1746
|
+
* `Temporal.PlainTime.from(text)`: the time an RFC 9557 string names, either
|
|
1747
|
+
* a time alone (`12:30`, `T1230`, `12:30:15.5`) or a date-time, whose date is
|
|
1748
|
+
* ignored. A string with no time, one with a `Z`, and a time alone without
|
|
1749
|
+
* its `T` that would also read as a year and month or a month and day
|
|
1750
|
+
* (`2021-12`, `1214`) are refused, as Temporal refuses them. Panics with a
|
|
1751
|
+
* `RangeError` on anything else.
|
|
1752
|
+
*
|
|
1753
|
+
* @param text the string to parse
|
|
1754
|
+
* @returns the time
|
|
1755
|
+
*/
|
|
1756
|
+
export const plainTimeFrom = (text: string): PlainTime => {
|
|
1757
|
+
const time: PlainTime | null = plainTimeFromOrNull(text)
|
|
1758
|
+
if (time === null) {
|
|
1759
|
+
temporalRangeError(`PlainTime: cannot parse "${text}"`)
|
|
1760
|
+
return new PlainTime()
|
|
1761
|
+
}
|
|
1762
|
+
return time
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
/**
|
|
1766
|
+
* `plainTimeFrom`, answering `null` instead of panicking on a string it
|
|
1767
|
+
* refuses. Not part of Temporal.
|
|
1768
|
+
*
|
|
1769
|
+
* @param text the string to parse
|
|
1770
|
+
* @returns the time, or `null`
|
|
1771
|
+
*/
|
|
1772
|
+
export const plainTimeFromOrNull = (text: string): PlainTime | null => {
|
|
1773
|
+
const full: TemporalReader = temporalReadDateTime(text)
|
|
1774
|
+
if (full.ok) {
|
|
1775
|
+
if (!full.hasTime || full.utc) {
|
|
1776
|
+
return null
|
|
1777
|
+
}
|
|
1778
|
+
return new PlainTime(
|
|
1779
|
+
full.hour,
|
|
1780
|
+
full.minute,
|
|
1781
|
+
full.second,
|
|
1782
|
+
toI32(full.fraction / 1000000),
|
|
1783
|
+
toI32(full.fraction / 1000) % 1000,
|
|
1784
|
+
full.fraction % 1000
|
|
1785
|
+
)
|
|
1786
|
+
}
|
|
1787
|
+
const r: TemporalReader = new TemporalReader(text)
|
|
1788
|
+
const designated: boolean = r.eat(toI32(84)) || r.eat(toI32(116))
|
|
1789
|
+
r.readTime()
|
|
1790
|
+
if (r.ok) {
|
|
1791
|
+
r.readOffset()
|
|
1792
|
+
}
|
|
1793
|
+
if (r.ok) {
|
|
1794
|
+
r.readAnnotations()
|
|
1795
|
+
}
|
|
1796
|
+
if (!r.ok || !r.done() || r.utc) {
|
|
1797
|
+
return null
|
|
1798
|
+
}
|
|
1799
|
+
if (!designated && temporalLooksLikeYearMonthOrMonthDay(text)) {
|
|
1800
|
+
return null
|
|
1801
|
+
}
|
|
1802
|
+
return new PlainTime(
|
|
1803
|
+
r.hour,
|
|
1804
|
+
r.minute,
|
|
1805
|
+
r.second,
|
|
1806
|
+
toI32(r.fraction / 1000000),
|
|
1807
|
+
toI32(r.fraction / 1000) % 1000,
|
|
1808
|
+
r.fraction % 1000
|
|
1809
|
+
)
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
/**
|
|
1813
|
+
* Whether `text`, before any annotation, reads as a year and month
|
|
1814
|
+
* (`2021-12`, `202112`) or a month and day (`12-14`, `1214`), which a time
|
|
1815
|
+
* without its `T` may not be. Only a month and day that exist count, in the
|
|
1816
|
+
* leap year 1972 that `PlainMonthDay` takes for reference: `0229` is one, so
|
|
1817
|
+
* it is refused as a time, and `0230` is not, so it reads as 02:30, as
|
|
1818
|
+
* Node's `Temporal` reads it.
|
|
1819
|
+
*/
|
|
1820
|
+
const temporalLooksLikeYearMonthOrMonthDay = (text: string): boolean => {
|
|
1821
|
+
const bracket: i32 = toI32(text.indexOf("["))
|
|
1822
|
+
const head: string = bracket < 0 ? text : text.substring(0, bracket)
|
|
1823
|
+
const n: i32 = toI32(head.length)
|
|
1824
|
+
for (let i: i32 = 0; i < n; i++) {
|
|
1825
|
+
const c: i32 = toI32(head.charCodeAt(i))
|
|
1826
|
+
if (!((c >= 48 && c <= 57) || c === 45)) {
|
|
1827
|
+
return false
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
if (n === 7 && toI32(head.charCodeAt(4)) === 45) {
|
|
1831
|
+
const m: i32 = temporalTwoDigits(head, 5)
|
|
1832
|
+
return m >= 1 && m <= 12
|
|
1833
|
+
}
|
|
1834
|
+
if (n === 6 && toI32(head.indexOf("-")) < 0) {
|
|
1835
|
+
const m: i32 = temporalTwoDigits(head, 4)
|
|
1836
|
+
return m >= 1 && m <= 12
|
|
1837
|
+
}
|
|
1838
|
+
if ((n === 5 && toI32(head.charCodeAt(2)) === 45) || (n === 4 && toI32(head.indexOf("-")) < 0)) {
|
|
1839
|
+
const m: i32 = temporalTwoDigits(head, 0)
|
|
1840
|
+
const d: i32 = temporalTwoDigits(head, n === 5 ? 3 : 2)
|
|
1841
|
+
return m >= 1 && m <= 12 && d >= 1 && d <= temporalDaysInMonth(1972, m)
|
|
1842
|
+
}
|
|
1843
|
+
return false
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
/** The two decimal digits of `text` at `at` as a number. */
|
|
1847
|
+
const temporalTwoDigits = (text: string, at: i32): i32 =>
|
|
1848
|
+
(toI32(text.charCodeAt(at)) - 48) * 10 + toI32(text.charCodeAt(at + 1)) - 48
|
|
1849
|
+
|
|
1850
|
+
/**
|
|
1851
|
+
* `Temporal.PlainTime.compare(a, b)`: -1, 0 or 1 as `a` is earlier than, the
|
|
1852
|
+
* same as, or later than `b`.
|
|
1853
|
+
*
|
|
1854
|
+
* @param a the first time
|
|
1855
|
+
* @param b the second time
|
|
1856
|
+
* @returns the order of the two
|
|
1857
|
+
*/
|
|
1858
|
+
export const plainTimeCompare = (a: PlainTime, b: PlainTime): i32 => temporalSign(a.nanoOfDay - b.nanoOfDay)
|
|
1859
|
+
|
|
1860
|
+
// ---------------------------------------------------------------------------
|
|
1861
|
+
// PlainDateTime
|
|
1862
|
+
// ---------------------------------------------------------------------------
|
|
1863
|
+
|
|
1864
|
+
/**
|
|
1865
|
+
* A date and a time of day with no time zone: `Temporal.PlainDateTime` in the
|
|
1866
|
+
* ISO 8601 calendar. It has every field of `PlainDate` and of `PlainTime`.
|
|
1867
|
+
*/
|
|
1868
|
+
export class PlainDateTime {
|
|
1869
|
+
/** The month as Temporal codes it: `M01` to `M12`. */
|
|
1870
|
+
readonly monthCode: string
|
|
1871
|
+
/** The calendar, always `iso8601`. */
|
|
1872
|
+
readonly calendarId: string
|
|
1873
|
+
/** @internal The days since 1970-01-01; not part of Temporal. */
|
|
1874
|
+
readonly epochDay: i64
|
|
1875
|
+
/** @internal Nanoseconds since midnight; not part of Temporal. */
|
|
1876
|
+
readonly nanoOfDay: i64
|
|
1877
|
+
/** The year. */
|
|
1878
|
+
readonly year: i32
|
|
1879
|
+
/** The month, 1 to 12. */
|
|
1880
|
+
readonly month: i32
|
|
1881
|
+
/** The day of the month, from 1. */
|
|
1882
|
+
readonly day: i32
|
|
1883
|
+
/** The weekday, 1 for Monday to 7 for Sunday. */
|
|
1884
|
+
readonly dayOfWeek: i32
|
|
1885
|
+
/** The day of the year, 1 for 1 January. */
|
|
1886
|
+
readonly dayOfYear: i32
|
|
1887
|
+
/** The ISO week number, 1 to 53. */
|
|
1888
|
+
readonly weekOfYear: i32
|
|
1889
|
+
/** The year the ISO week belongs to. */
|
|
1890
|
+
readonly yearOfWeek: i32
|
|
1891
|
+
/** Days in a week: 7. */
|
|
1892
|
+
readonly daysInWeek: i32
|
|
1893
|
+
/** Days in this date's month. */
|
|
1894
|
+
readonly daysInMonth: i32
|
|
1895
|
+
/** Days in this date's year: 365 or 366. */
|
|
1896
|
+
readonly daysInYear: i32
|
|
1897
|
+
/** Months in a year: 12. */
|
|
1898
|
+
readonly monthsInYear: i32
|
|
1899
|
+
/** The hour, 0 to 23. */
|
|
1900
|
+
readonly hour: i32
|
|
1901
|
+
/** The minute, 0 to 59. */
|
|
1902
|
+
readonly minute: i32
|
|
1903
|
+
/** The second, 0 to 59. */
|
|
1904
|
+
readonly second: i32
|
|
1905
|
+
/** The millisecond, 0 to 999. */
|
|
1906
|
+
readonly millisecond: i32
|
|
1907
|
+
/** The microsecond, 0 to 999. */
|
|
1908
|
+
readonly microsecond: i32
|
|
1909
|
+
/** The nanosecond, 0 to 999. */
|
|
1910
|
+
readonly nanosecond: i32
|
|
1911
|
+
/** Whether this date's year is a leap year. */
|
|
1912
|
+
readonly inLeapYear: boolean
|
|
1913
|
+
|
|
1914
|
+
/**
|
|
1915
|
+
* The date-time, as `new Temporal.PlainDateTime(...)`; the time fields
|
|
1916
|
+
* default to 0. Panics with a `RangeError` on a field out of range, or a
|
|
1917
|
+
* moment outside Temporal's range of -271821-04-19T00:00:00.000000001 to
|
|
1918
|
+
* +275760-09-13T23:59:59.999999999.
|
|
1919
|
+
*
|
|
1920
|
+
* @param isoYear the year
|
|
1921
|
+
* @param isoMonth the month, 1 to 12
|
|
1922
|
+
* @param isoDay the day of the month, from 1
|
|
1923
|
+
* @param hour 0 to 23
|
|
1924
|
+
* @param minute 0 to 59
|
|
1925
|
+
* @param second 0 to 59
|
|
1926
|
+
* @param millisecond 0 to 999
|
|
1927
|
+
* @param microsecond 0 to 999
|
|
1928
|
+
* @param nanosecond 0 to 999
|
|
1929
|
+
*/
|
|
1930
|
+
constructor(
|
|
1931
|
+
isoYear: i32,
|
|
1932
|
+
isoMonth: i32,
|
|
1933
|
+
isoDay: i32,
|
|
1934
|
+
hour: i32 = 0,
|
|
1935
|
+
minute: i32 = 0,
|
|
1936
|
+
second: i32 = 0,
|
|
1937
|
+
millisecond: i32 = 0,
|
|
1938
|
+
microsecond: i32 = 0,
|
|
1939
|
+
nanosecond: i32 = 0
|
|
1940
|
+
) {
|
|
1941
|
+
const date: PlainDate = new PlainDate(isoYear, isoMonth, isoDay)
|
|
1942
|
+
temporalCheckTime(
|
|
1943
|
+
"PlainDateTime",
|
|
1944
|
+
toI64(hour),
|
|
1945
|
+
toI64(minute),
|
|
1946
|
+
toI64(second),
|
|
1947
|
+
toI64(millisecond),
|
|
1948
|
+
toI64(microsecond),
|
|
1949
|
+
toI64(nanosecond)
|
|
1950
|
+
)
|
|
1951
|
+
const nanoOfDay: i64 = temporalNanoOfDay(hour, minute, second, millisecond, microsecond, nanosecond)
|
|
1952
|
+
if (date.epochDay === TEMPORAL_MIN_EPOCH_DAY && nanoOfDay === 0) {
|
|
1953
|
+
temporalRangeError("PlainDateTime: the date-time is outside Temporal's range")
|
|
1954
|
+
}
|
|
1955
|
+
this.year = isoYear
|
|
1956
|
+
this.month = isoMonth
|
|
1957
|
+
this.day = isoDay
|
|
1958
|
+
this.monthCode = date.monthCode
|
|
1959
|
+
this.calendarId = date.calendarId
|
|
1960
|
+
this.dayOfWeek = date.dayOfWeek
|
|
1961
|
+
this.dayOfYear = date.dayOfYear
|
|
1962
|
+
this.weekOfYear = date.weekOfYear
|
|
1963
|
+
this.yearOfWeek = date.yearOfWeek
|
|
1964
|
+
this.daysInWeek = date.daysInWeek
|
|
1965
|
+
this.daysInMonth = date.daysInMonth
|
|
1966
|
+
this.daysInYear = date.daysInYear
|
|
1967
|
+
this.monthsInYear = date.monthsInYear
|
|
1968
|
+
this.inLeapYear = date.inLeapYear
|
|
1969
|
+
this.hour = hour
|
|
1970
|
+
this.minute = minute
|
|
1971
|
+
this.second = second
|
|
1972
|
+
this.millisecond = millisecond
|
|
1973
|
+
this.microsecond = microsecond
|
|
1974
|
+
this.nanosecond = nanosecond
|
|
1975
|
+
this.epochDay = date.epochDay
|
|
1976
|
+
this.nanoOfDay = nanoOfDay
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
/**
|
|
1980
|
+
* The date-time as Temporal writes it: `2024-01-15T09:05:00`, with the
|
|
1981
|
+
* fraction of the second to as many digits as it needs.
|
|
1982
|
+
*
|
|
1983
|
+
* @returns the ISO 8601 form
|
|
1984
|
+
*/
|
|
1985
|
+
toString(): string {
|
|
1986
|
+
const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
|
|
1987
|
+
const sub: i32 = this.millisecond * 1000000 + this.microsecond * 1000 + this.nanosecond
|
|
1988
|
+
if (this.year >= 0 && this.year <= 9999) {
|
|
1989
|
+
const seconds: i32 = temporalPutDateTime4(
|
|
1990
|
+
out,
|
|
1991
|
+
0,
|
|
1992
|
+
this.year,
|
|
1993
|
+
this.month,
|
|
1994
|
+
this.day,
|
|
1995
|
+
this.hour,
|
|
1996
|
+
this.minute,
|
|
1997
|
+
this.second
|
|
1998
|
+
)
|
|
1999
|
+
return String.fromBytes(out, 0, temporalPutFraction(out, seconds, sub))
|
|
2000
|
+
}
|
|
2001
|
+
const date: i32 = temporalPutDate(out, 0, this.year, this.month, this.day)
|
|
2002
|
+
const end: i32 = temporalPutTime(
|
|
2003
|
+
out,
|
|
2004
|
+
temporalPutByte(out, date, 84),
|
|
2005
|
+
this.hour,
|
|
2006
|
+
this.minute,
|
|
2007
|
+
this.second,
|
|
2008
|
+
sub
|
|
2009
|
+
)
|
|
2010
|
+
return String.fromBytes(out, 0, end)
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
/**
|
|
2014
|
+
* The same string as `toString`.
|
|
2015
|
+
*
|
|
2016
|
+
* @returns the ISO 8601 form
|
|
2017
|
+
*/
|
|
2018
|
+
toJSON(): string {
|
|
2019
|
+
return this.toString()
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
/**
|
|
2023
|
+
* The date part.
|
|
2024
|
+
*
|
|
2025
|
+
* @returns the date
|
|
2026
|
+
*/
|
|
2027
|
+
toPlainDate(): PlainDate {
|
|
2028
|
+
return new PlainDate(this.year, this.month, this.day)
|
|
2029
|
+
}
|
|
2030
|
+
|
|
2031
|
+
/**
|
|
2032
|
+
* The time part.
|
|
2033
|
+
*
|
|
2034
|
+
* @returns the time
|
|
2035
|
+
*/
|
|
2036
|
+
toPlainTime(): PlainTime {
|
|
2037
|
+
return new PlainTime(
|
|
2038
|
+
this.hour,
|
|
2039
|
+
this.minute,
|
|
2040
|
+
this.second,
|
|
2041
|
+
this.millisecond,
|
|
2042
|
+
this.microsecond,
|
|
2043
|
+
this.nanosecond
|
|
2044
|
+
)
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
/**
|
|
2048
|
+
* This date-time with its time replaced by `time`.
|
|
2049
|
+
*
|
|
2050
|
+
* @param time the new time of day
|
|
2051
|
+
* @returns the new date-time
|
|
2052
|
+
*/
|
|
2053
|
+
withPlainTime(time: PlainTime): PlainDateTime {
|
|
2054
|
+
return this.toPlainDate().toPlainDateTime(time)
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
/**
|
|
2058
|
+
* Whether `other` is the same date-time.
|
|
2059
|
+
*
|
|
2060
|
+
* @param other the date-time to compare with
|
|
2061
|
+
* @returns whether the two are equal
|
|
2062
|
+
*/
|
|
2063
|
+
equals(other: PlainDateTime): boolean {
|
|
2064
|
+
return this.epochDay === other.epochDay && this.nanoOfDay === other.nanoOfDay
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
/**
|
|
2068
|
+
* This date-time moved by `duration`: the time fields first, carrying whole
|
|
2069
|
+
* days, then the years and months with the day constrained to the new
|
|
2070
|
+
* month, then the weeks, the days and the carried days — Temporal's
|
|
2071
|
+
* AddDurationToDateTime. Panics with a `RangeError` outside Temporal's range.
|
|
2072
|
+
*
|
|
2073
|
+
* @param duration how far to move
|
|
2074
|
+
* @returns the new date-time
|
|
2075
|
+
*/
|
|
2076
|
+
add(duration: Duration): PlainDateTime {
|
|
2077
|
+
let carryDays: i64 = 0
|
|
2078
|
+
let nanoOfDay: i64 = this.nanoOfDay
|
|
2079
|
+
if (!temporalNoTime(duration)) {
|
|
2080
|
+
const seconds: i64 = temporalDurationSeconds(duration, false)
|
|
2081
|
+
const nanos: i64 = temporalDurationNanos(duration) + this.nanoOfDay
|
|
2082
|
+
const total: i64 = seconds + temporalFloorDiv(nanos, TEMPORAL_NS_PER_SECOND)
|
|
2083
|
+
carryDays = temporalFloorDiv(total, TEMPORAL_SECONDS_PER_DAY)
|
|
2084
|
+
nanoOfDay =
|
|
2085
|
+
temporalFloorMod(total, TEMPORAL_SECONDS_PER_DAY) * TEMPORAL_NS_PER_SECOND +
|
|
2086
|
+
temporalFloorMod(nanos, TEMPORAL_NS_PER_SECOND)
|
|
2087
|
+
}
|
|
2088
|
+
const epochDay: i64 = temporalAddToDate(
|
|
2089
|
+
this.year,
|
|
2090
|
+
this.month,
|
|
2091
|
+
this.day,
|
|
2092
|
+
this.epochDay,
|
|
2093
|
+
duration.years,
|
|
2094
|
+
duration.months,
|
|
2095
|
+
duration.weeks * 7 + duration.days + carryDays
|
|
2096
|
+
)
|
|
2097
|
+
const civil: i64 = temporalCivilOfEpochDay(epochDay)
|
|
2098
|
+
const secs: i64 = toI64(nanoOfDay / TEMPORAL_NS_PER_SECOND)
|
|
2099
|
+
const sub: i32 = toI32(nanoOfDay % TEMPORAL_NS_PER_SECOND)
|
|
2100
|
+
return new PlainDateTime(
|
|
2101
|
+
temporalCivilYear(civil),
|
|
2102
|
+
temporalCivilMonth(civil),
|
|
2103
|
+
temporalCivilDay(civil),
|
|
2104
|
+
toI32(secs / 3600),
|
|
2105
|
+
toI32(toI64(secs / 60) % 60),
|
|
2106
|
+
toI32(secs % 60),
|
|
2107
|
+
toI32(sub / 1000000),
|
|
2108
|
+
toI32(sub / 1000) % 1000,
|
|
2109
|
+
sub % 1000
|
|
2110
|
+
)
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
/**
|
|
2114
|
+
* This date-time moved back by `duration`: `add` of its negation.
|
|
2115
|
+
*
|
|
2116
|
+
* @param duration how far to move back
|
|
2117
|
+
* @returns the new date-time
|
|
2118
|
+
*/
|
|
2119
|
+
subtract(duration: Duration): PlainDateTime {
|
|
2120
|
+
return this.add(duration.negated())
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
/**
|
|
2124
|
+
* The time from this date-time to `other` in days and smaller units
|
|
2125
|
+
* (Temporal's default `largestUnit: "day"`), every field with one sign.
|
|
2126
|
+
*
|
|
2127
|
+
* @param other the later date-time
|
|
2128
|
+
* @returns the difference
|
|
2129
|
+
*/
|
|
2130
|
+
until(other: PlainDateTime): Duration {
|
|
2131
|
+
return temporalBalance(temporalDateTimeSpan(this, other), TEMPORAL_UNIT_DAY)
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
/**
|
|
2135
|
+
* The time from `other` to this date-time: `other.until(this)`.
|
|
2136
|
+
*
|
|
2137
|
+
* @param other the earlier date-time
|
|
2138
|
+
* @returns the difference
|
|
2139
|
+
*/
|
|
2140
|
+
since(other: PlainDateTime): Duration {
|
|
2141
|
+
return temporalBalance(temporalDateTimeSpan(other, this), TEMPORAL_UNIT_DAY)
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
/** The span from `a` to `b`, every day 24 hours. */
|
|
2146
|
+
const temporalDateTimeSpan = (a: PlainDateTime, b: PlainDateTime): TemporalSpan =>
|
|
2147
|
+
new TemporalSpan((b.epochDay - a.epochDay) * TEMPORAL_SECONDS_PER_DAY, b.nanoOfDay - a.nanoOfDay)
|
|
2148
|
+
|
|
2149
|
+
/**
|
|
2150
|
+
* `Temporal.PlainDateTime.from(text)`: the date-time an RFC 9557 string
|
|
2151
|
+
* names; a date alone is midnight. An offset and annotations are ignored but
|
|
2152
|
+
* for a calendar, which must be `iso8601`; a `Z` is refused. Panics with a
|
|
2153
|
+
* `RangeError` on anything else.
|
|
2154
|
+
*
|
|
2155
|
+
* @param text such as `2024-01-15T09:05` or `2024-01-15 09:05:00.5`
|
|
2156
|
+
* @returns the date-time
|
|
2157
|
+
*/
|
|
2158
|
+
export const plainDateTimeFrom = (text: string): PlainDateTime => {
|
|
2159
|
+
const dt: PlainDateTime | null = plainDateTimeFromOrNull(text)
|
|
2160
|
+
if (dt === null) {
|
|
2161
|
+
temporalRangeError(`PlainDateTime: cannot parse "${text}"`)
|
|
2162
|
+
return new PlainDateTime(1970, 1, 1)
|
|
2163
|
+
}
|
|
2164
|
+
return dt
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2167
|
+
/**
|
|
2168
|
+
* `plainDateTimeFrom`, answering `null` instead of panicking on a string it
|
|
2169
|
+
* refuses. Not part of Temporal.
|
|
2170
|
+
*
|
|
2171
|
+
* @param text the string to parse
|
|
2172
|
+
* @returns the date-time, or `null`
|
|
2173
|
+
*/
|
|
2174
|
+
export const plainDateTimeFromOrNull = (text: string): PlainDateTime | null => {
|
|
2175
|
+
const length: i32 = toI32(text.length)
|
|
2176
|
+
if (length >= 16 && temporalIsTimeDesignator(temporalCharAt(text, 10))) {
|
|
2177
|
+
const civil: i64 = temporalFastDate(text)
|
|
2178
|
+
const nanoOfDay: i64 = civil < 0 ? -1 : temporalFastTime(text, 11, length)
|
|
2179
|
+
if (nanoOfDay >= 0) {
|
|
2180
|
+
const secs: i32 = toI32(nanoOfDay / TEMPORAL_NS_PER_SECOND)
|
|
2181
|
+
const sub: i32 = toI32(nanoOfDay % TEMPORAL_NS_PER_SECOND)
|
|
2182
|
+
return new PlainDateTime(
|
|
2183
|
+
temporalCivilYear(civil),
|
|
2184
|
+
temporalCivilMonth(civil),
|
|
2185
|
+
temporalCivilDay(civil),
|
|
2186
|
+
toI32(secs / 3600),
|
|
2187
|
+
toI32(secs / 60) % 60,
|
|
2188
|
+
secs % 60,
|
|
2189
|
+
toI32(sub / 1000000),
|
|
2190
|
+
toI32(sub / 1000) % 1000,
|
|
2191
|
+
sub % 1000
|
|
2192
|
+
)
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
const r: TemporalReader = temporalReadDateTime(text)
|
|
2196
|
+
if (!r.ok || r.utc || !r.isoCalendar || !temporalReadInRange(r, true)) {
|
|
2197
|
+
return null
|
|
2198
|
+
}
|
|
2199
|
+
return new PlainDateTime(
|
|
2200
|
+
r.year,
|
|
2201
|
+
r.month,
|
|
2202
|
+
r.day,
|
|
2203
|
+
r.hour,
|
|
2204
|
+
r.minute,
|
|
2205
|
+
r.second,
|
|
2206
|
+
toI32(r.fraction / 1000000),
|
|
2207
|
+
toI32(r.fraction / 1000) % 1000,
|
|
2208
|
+
r.fraction % 1000
|
|
2209
|
+
)
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
/**
|
|
2213
|
+
* `Temporal.PlainDateTime.compare(a, b)`: -1, 0 or 1 as `a` is before, the
|
|
2214
|
+
* same as, or after `b`.
|
|
2215
|
+
*
|
|
2216
|
+
* @param a the first date-time
|
|
2217
|
+
* @param b the second date-time
|
|
2218
|
+
* @returns the order of the two
|
|
2219
|
+
*/
|
|
2220
|
+
export const plainDateTimeCompare = (a: PlainDateTime, b: PlainDateTime): i32 => {
|
|
2221
|
+
const byDay: i32 = temporalSign(a.epochDay - b.epochDay)
|
|
2222
|
+
return byDay !== 0 ? byDay : temporalSign(a.nanoOfDay - b.nanoOfDay)
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
// ---------------------------------------------------------------------------
|
|
2226
|
+
// Duration
|
|
2227
|
+
// ---------------------------------------------------------------------------
|
|
2228
|
+
|
|
2229
|
+
/**
|
|
2230
|
+
* A length of time in ten signed fields: `Temporal.Duration`. A duration is
|
|
2231
|
+
* not balanced: `PT90M` keeps its 90 minutes. Every non-zero field has the
|
|
2232
|
+
* same sign.
|
|
2233
|
+
*/
|
|
2234
|
+
export class Duration {
|
|
2235
|
+
/** Years. */
|
|
2236
|
+
readonly years: i64
|
|
2237
|
+
/** Months. */
|
|
2238
|
+
readonly months: i64
|
|
2239
|
+
/** Weeks. */
|
|
2240
|
+
readonly weeks: i64
|
|
2241
|
+
/** Days. */
|
|
2242
|
+
readonly days: i64
|
|
2243
|
+
/** Hours. */
|
|
2244
|
+
readonly hours: i64
|
|
2245
|
+
/** Minutes. */
|
|
2246
|
+
readonly minutes: i64
|
|
2247
|
+
/** Seconds. */
|
|
2248
|
+
readonly seconds: i64
|
|
2249
|
+
/** Milliseconds. */
|
|
2250
|
+
readonly milliseconds: i64
|
|
2251
|
+
/** Microseconds. */
|
|
2252
|
+
readonly microseconds: i64
|
|
2253
|
+
/** Nanoseconds. */
|
|
2254
|
+
readonly nanoseconds: i64
|
|
2255
|
+
/** -1, 0 or 1: the sign every non-zero field shares. */
|
|
2256
|
+
readonly sign: i32
|
|
2257
|
+
/** Whether every field is zero. */
|
|
2258
|
+
readonly blank: boolean
|
|
2259
|
+
|
|
2260
|
+
/**
|
|
2261
|
+
* The duration, as `new Temporal.Duration(...)`; every field defaults to 0.
|
|
2262
|
+
* Panics with a `RangeError` when two fields have different signs, when
|
|
2263
|
+
* `years`, `months` or `weeks` reaches 2^32, or when the rest comes to 2^53
|
|
2264
|
+
* seconds or more.
|
|
2265
|
+
*
|
|
2266
|
+
* @param years years
|
|
2267
|
+
* @param months months
|
|
2268
|
+
* @param weeks weeks
|
|
2269
|
+
* @param days days
|
|
2270
|
+
* @param hours hours
|
|
2271
|
+
* @param minutes minutes
|
|
2272
|
+
* @param seconds seconds
|
|
2273
|
+
* @param milliseconds milliseconds
|
|
2274
|
+
* @param microseconds microseconds
|
|
2275
|
+
* @param nanoseconds nanoseconds
|
|
2276
|
+
*/
|
|
2277
|
+
constructor(
|
|
2278
|
+
years: i64 = 0,
|
|
2279
|
+
months: i64 = 0,
|
|
2280
|
+
weeks: i64 = 0,
|
|
2281
|
+
days: i64 = 0,
|
|
2282
|
+
hours: i64 = 0,
|
|
2283
|
+
minutes: i64 = 0,
|
|
2284
|
+
seconds: i64 = 0,
|
|
2285
|
+
milliseconds: i64 = 0,
|
|
2286
|
+
microseconds: i64 = 0,
|
|
2287
|
+
nanoseconds: i64 = 0
|
|
2288
|
+
) {
|
|
2289
|
+
// The checks are written out here, not a call of `temporalDurationProblem`,
|
|
2290
|
+
// because every `until` and `add` builds a `Duration`: as a call they cost
|
|
2291
|
+
// `until` on a date 70% (bench/temporal.ts). Each bound is tested on both
|
|
2292
|
+
// sides, so the least `i64`, whose magnitude does not fit, is refused too.
|
|
2293
|
+
let sign: i32 = temporalSignOf(0, years)
|
|
2294
|
+
sign = temporalSignOf(sign, months)
|
|
2295
|
+
sign = temporalSignOf(sign, weeks)
|
|
2296
|
+
sign = temporalSignOf(sign, days)
|
|
2297
|
+
sign = temporalSignOf(sign, hours)
|
|
2298
|
+
sign = temporalSignOf(sign, minutes)
|
|
2299
|
+
sign = temporalSignOf(sign, seconds)
|
|
2300
|
+
sign = temporalSignOf(sign, milliseconds)
|
|
2301
|
+
sign = temporalSignOf(sign, microseconds)
|
|
2302
|
+
sign = temporalSignOf(sign, nanoseconds)
|
|
2303
|
+
const c: i64 = TEMPORAL_MAX_CALENDAR_FIELD
|
|
2304
|
+
if (years <= -c || years >= c || months <= -c || months >= c || weeks <= -c || weeks >= c) {
|
|
2305
|
+
temporalRangeError("Duration: years, months and weeks must each be below 2^32")
|
|
2306
|
+
}
|
|
2307
|
+
const m: i64 = TEMPORAL_MAX_DURATION_SECONDS
|
|
2308
|
+
if (
|
|
2309
|
+
days < -104249991374 ||
|
|
2310
|
+
days > 104249991374 ||
|
|
2311
|
+
hours < -2501999792983 ||
|
|
2312
|
+
hours > 2501999792983 ||
|
|
2313
|
+
minutes < -150119987579016 ||
|
|
2314
|
+
minutes > 150119987579016 ||
|
|
2315
|
+
seconds < -m ||
|
|
2316
|
+
seconds > m
|
|
2317
|
+
) {
|
|
2318
|
+
temporalRangeError("Duration: the time fields come to 2^53 seconds or more")
|
|
2319
|
+
}
|
|
2320
|
+
if (temporalWholeSeconds(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) > m) {
|
|
2321
|
+
temporalRangeError("Duration: the time fields come to 2^53 seconds or more")
|
|
2322
|
+
}
|
|
2323
|
+
this.years = years
|
|
2324
|
+
this.months = months
|
|
2325
|
+
this.weeks = weeks
|
|
2326
|
+
this.days = days
|
|
2327
|
+
this.hours = hours
|
|
2328
|
+
this.minutes = minutes
|
|
2329
|
+
this.seconds = seconds
|
|
2330
|
+
this.milliseconds = milliseconds
|
|
2331
|
+
this.microseconds = microseconds
|
|
2332
|
+
this.nanoseconds = nanoseconds
|
|
2333
|
+
this.sign = sign
|
|
2334
|
+
this.blank = sign === 0
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
/**
|
|
2338
|
+
* The duration as ISO 8601 writes it: `P1Y2M3W4DT5H6M7.008S`, `-PT90M`,
|
|
2339
|
+
* and `PT0S` when blank. The seconds and their fractions are written as one
|
|
2340
|
+
* decimal number, so `PT1.5S` reads back as 1 second and 500 milliseconds.
|
|
2341
|
+
*
|
|
2342
|
+
* @returns the ISO 8601 form
|
|
2343
|
+
*/
|
|
2344
|
+
toString(): string {
|
|
2345
|
+
const k: i64 = toI64(this.sign < 0 ? -1 : 1)
|
|
2346
|
+
const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
|
|
2347
|
+
let i: i32 = 0
|
|
2348
|
+
if (this.sign < 0) {
|
|
2349
|
+
i = temporalPutByte(out, i, 45)
|
|
2350
|
+
}
|
|
2351
|
+
i = temporalPutByte(out, i, 80)
|
|
2352
|
+
if (this.years !== 0) {
|
|
2353
|
+
i = temporalPutField(out, i, this.years * k, 89)
|
|
2354
|
+
}
|
|
2355
|
+
if (this.months !== 0) {
|
|
2356
|
+
i = temporalPutField(out, i, this.months * k, 77)
|
|
2357
|
+
}
|
|
2358
|
+
if (this.weeks !== 0) {
|
|
2359
|
+
i = temporalPutField(out, i, this.weeks * k, 87)
|
|
2360
|
+
}
|
|
2361
|
+
if (this.days !== 0) {
|
|
2362
|
+
i = temporalPutField(out, i, this.days * k, 68)
|
|
2363
|
+
}
|
|
2364
|
+
const zeroAbove: boolean =
|
|
2365
|
+
this.years === 0 &&
|
|
2366
|
+
this.months === 0 &&
|
|
2367
|
+
this.weeks === 0 &&
|
|
2368
|
+
this.days === 0 &&
|
|
2369
|
+
this.hours === 0 &&
|
|
2370
|
+
this.minutes === 0
|
|
2371
|
+
// The seconds and their fractions are one decimal number, balanced. Every
|
|
2372
|
+
// field times `k` is not negative, so each division here truncates as a
|
|
2373
|
+
// floor would.
|
|
2374
|
+
const ms: i64 = this.milliseconds * k
|
|
2375
|
+
const us: i64 = this.microseconds * k
|
|
2376
|
+
const ns: i64 = this.nanoseconds * k
|
|
2377
|
+
let fraction: i64 = (ms % 1000) * 1000000 + (us % 1000000) * 1000 + (ns % TEMPORAL_NS_PER_SECOND)
|
|
2378
|
+
const wholeSeconds: i64 =
|
|
2379
|
+
this.seconds * k +
|
|
2380
|
+
toI64(ms / 1000) +
|
|
2381
|
+
toI64(us / 1000000) +
|
|
2382
|
+
toI64(ns / TEMPORAL_NS_PER_SECOND) +
|
|
2383
|
+
toI64(fraction / TEMPORAL_NS_PER_SECOND)
|
|
2384
|
+
fraction = fraction % TEMPORAL_NS_PER_SECOND
|
|
2385
|
+
const withSeconds: boolean = wholeSeconds !== 0 || fraction !== 0 || zeroAbove
|
|
2386
|
+
if (this.hours !== 0 || this.minutes !== 0 || withSeconds) {
|
|
2387
|
+
i = temporalPutByte(out, i, 84)
|
|
2388
|
+
}
|
|
2389
|
+
if (this.hours !== 0) {
|
|
2390
|
+
i = temporalPutField(out, i, this.hours * k, 72)
|
|
2391
|
+
}
|
|
2392
|
+
if (this.minutes !== 0) {
|
|
2393
|
+
i = temporalPutField(out, i, this.minutes * k, 77)
|
|
2394
|
+
}
|
|
2395
|
+
if (withSeconds) {
|
|
2396
|
+
i = temporalPutNumber(out, i, wholeSeconds, 1)
|
|
2397
|
+
i = temporalPutByte(out, temporalPutFraction(out, i, toI32(fraction)), 83)
|
|
2398
|
+
}
|
|
2399
|
+
return String.fromBytes(out, 0, i)
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
/**
|
|
2403
|
+
* The same string as `toString`.
|
|
2404
|
+
*
|
|
2405
|
+
* @returns the ISO 8601 form
|
|
2406
|
+
*/
|
|
2407
|
+
toJSON(): string {
|
|
2408
|
+
return this.toString()
|
|
2409
|
+
}
|
|
2410
|
+
|
|
2411
|
+
/**
|
|
2412
|
+
* This duration with every field's sign flipped.
|
|
2413
|
+
*
|
|
2414
|
+
* @returns the negated duration
|
|
2415
|
+
*/
|
|
2416
|
+
negated(): Duration {
|
|
2417
|
+
return new Duration(
|
|
2418
|
+
-this.years,
|
|
2419
|
+
-this.months,
|
|
2420
|
+
-this.weeks,
|
|
2421
|
+
-this.days,
|
|
2422
|
+
-this.hours,
|
|
2423
|
+
-this.minutes,
|
|
2424
|
+
-this.seconds,
|
|
2425
|
+
-this.milliseconds,
|
|
2426
|
+
-this.microseconds,
|
|
2427
|
+
-this.nanoseconds
|
|
2428
|
+
)
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
/**
|
|
2432
|
+
* This duration with every field made non-negative.
|
|
2433
|
+
*
|
|
2434
|
+
* @returns the absolute duration
|
|
2435
|
+
*/
|
|
2436
|
+
abs(): Duration {
|
|
2437
|
+
return this.sign < 0 ? this.negated() : this
|
|
2438
|
+
}
|
|
2439
|
+
|
|
2440
|
+
/**
|
|
2441
|
+
* The sum of this duration and `other`, balanced up to the larger of the
|
|
2442
|
+
* two's largest non-zero unit, as Temporal's `Duration.prototype.add`
|
|
2443
|
+
* balances it (`P1D` plus `PT25H` is `P2DT1H`). Panics with a `RangeError`
|
|
2444
|
+
* when either has years, months or weeks, whose length depends on a date.
|
|
2445
|
+
*
|
|
2446
|
+
* @param other the duration to add
|
|
2447
|
+
* @returns the sum
|
|
2448
|
+
*/
|
|
2449
|
+
add(other: Duration): Duration {
|
|
2450
|
+
if (
|
|
2451
|
+
this.years !== 0 ||
|
|
2452
|
+
this.months !== 0 ||
|
|
2453
|
+
this.weeks !== 0 ||
|
|
2454
|
+
other.years !== 0 ||
|
|
2455
|
+
other.months !== 0 ||
|
|
2456
|
+
other.weeks !== 0
|
|
2457
|
+
) {
|
|
2458
|
+
temporalRangeError("Duration: adding years, months or weeks needs a relativeTo date")
|
|
2459
|
+
}
|
|
2460
|
+
const a: i32 = temporalLargestUnit(this)
|
|
2461
|
+
const b: i32 = temporalLargestUnit(other)
|
|
2462
|
+
const sum: TemporalSpan = temporalSpanOfDuration(this, true).plus(temporalSpanOfDuration(other, true))
|
|
2463
|
+
return temporalBalance(sum, a > b ? a : b)
|
|
2464
|
+
}
|
|
2465
|
+
|
|
2466
|
+
/**
|
|
2467
|
+
* This duration minus `other`: `add` of its negation.
|
|
2468
|
+
*
|
|
2469
|
+
* @param other the duration to subtract
|
|
2470
|
+
* @returns the difference
|
|
2471
|
+
*/
|
|
2472
|
+
subtract(other: Duration): Duration {
|
|
2473
|
+
return this.add(other.negated())
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
/** `sign` after a field `n`: its sign, or a `RangeError` when it differs from a non-zero `sign`. */
|
|
2478
|
+
const temporalSignOf = (sign: i32, n: i64): i32 => {
|
|
2479
|
+
if (n === 0) {
|
|
2480
|
+
return sign
|
|
2481
|
+
}
|
|
2482
|
+
const s: i32 = n < 0 ? -1 : 1
|
|
2483
|
+
if (sign !== 0 && s !== sign) {
|
|
2484
|
+
temporalRangeError("Duration: the fields have mixed signs")
|
|
2485
|
+
}
|
|
2486
|
+
return s
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2489
|
+
/**
|
|
2490
|
+
* The magnitude of the whole seconds of the time fields, rounded toward
|
|
2491
|
+
* zero, for fields that share one sign and are each within the bounds the
|
|
2492
|
+
* constructor checks first, so that the sum fits an `i64`: the fields' own
|
|
2493
|
+
* whole seconds plus the seconds their remainders carry.
|
|
2494
|
+
*/
|
|
2495
|
+
const temporalWholeSeconds = (
|
|
2496
|
+
days: i64,
|
|
2497
|
+
hours: i64,
|
|
2498
|
+
minutes: i64,
|
|
2499
|
+
seconds: i64,
|
|
2500
|
+
milliseconds: i64,
|
|
2501
|
+
microseconds: i64,
|
|
2502
|
+
nanoseconds: i64
|
|
2503
|
+
): i64 => {
|
|
2504
|
+
const carry: i64 = toI64(
|
|
2505
|
+
(toI64(milliseconds % 1000) * 1000000 +
|
|
2506
|
+
toI64(microseconds % 1000000) * 1000 +
|
|
2507
|
+
(nanoseconds % 1000000000)) /
|
|
2508
|
+
1000000000
|
|
2509
|
+
)
|
|
2510
|
+
const whole: i64 =
|
|
2511
|
+
days * TEMPORAL_SECONDS_PER_DAY +
|
|
2512
|
+
hours * 3600 +
|
|
2513
|
+
minutes * 60 +
|
|
2514
|
+
seconds +
|
|
2515
|
+
toI64(milliseconds / 1000) +
|
|
2516
|
+
toI64(microseconds / 1000000) +
|
|
2517
|
+
toI64(nanoseconds / 1000000000) +
|
|
2518
|
+
carry
|
|
2519
|
+
return whole < 0 ? -whole : whole
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
/**
|
|
2523
|
+
* Why the ten fields make no valid `Duration`, or `""` when they make one,
|
|
2524
|
+
* by the constructor's own checks, for `durationFromOrNull` to answer `null`
|
|
2525
|
+
* where the constructor would panic:
|
|
2526
|
+
* two non-zero fields with different signs, `years`, `months` or `weeks` at
|
|
2527
|
+
* 2^32 or past, or the rest coming to 2^53 seconds or more (Temporal's
|
|
2528
|
+
* IsValidDuration). Every bound is tested on both sides, so the least `i64`,
|
|
2529
|
+
* whose magnitude does not fit, is refused too.
|
|
2530
|
+
*/
|
|
2531
|
+
const temporalDurationProblem = (
|
|
2532
|
+
years: i64,
|
|
2533
|
+
months: i64,
|
|
2534
|
+
weeks: i64,
|
|
2535
|
+
days: i64,
|
|
2536
|
+
hours: i64,
|
|
2537
|
+
minutes: i64,
|
|
2538
|
+
seconds: i64,
|
|
2539
|
+
milliseconds: i64,
|
|
2540
|
+
microseconds: i64,
|
|
2541
|
+
nanoseconds: i64
|
|
2542
|
+
): string => {
|
|
2543
|
+
const positive: boolean =
|
|
2544
|
+
years > 0 ||
|
|
2545
|
+
months > 0 ||
|
|
2546
|
+
weeks > 0 ||
|
|
2547
|
+
days > 0 ||
|
|
2548
|
+
hours > 0 ||
|
|
2549
|
+
minutes > 0 ||
|
|
2550
|
+
seconds > 0 ||
|
|
2551
|
+
milliseconds > 0 ||
|
|
2552
|
+
microseconds > 0 ||
|
|
2553
|
+
nanoseconds > 0
|
|
2554
|
+
const negative: boolean =
|
|
2555
|
+
years < 0 ||
|
|
2556
|
+
months < 0 ||
|
|
2557
|
+
weeks < 0 ||
|
|
2558
|
+
days < 0 ||
|
|
2559
|
+
hours < 0 ||
|
|
2560
|
+
minutes < 0 ||
|
|
2561
|
+
seconds < 0 ||
|
|
2562
|
+
milliseconds < 0 ||
|
|
2563
|
+
microseconds < 0 ||
|
|
2564
|
+
nanoseconds < 0
|
|
2565
|
+
if (positive && negative) {
|
|
2566
|
+
return "the fields have mixed signs"
|
|
2567
|
+
}
|
|
2568
|
+
const c: i64 = TEMPORAL_MAX_CALENDAR_FIELD
|
|
2569
|
+
if (years <= -c || years >= c || months <= -c || months >= c || weeks <= -c || weeks >= c) {
|
|
2570
|
+
return "years, months and weeks must each be below 2^32"
|
|
2571
|
+
}
|
|
2572
|
+
// Each field alone within the 2^53-second bound keeps the sum inside an i64.
|
|
2573
|
+
const m: i64 = TEMPORAL_MAX_DURATION_SECONDS
|
|
2574
|
+
if (
|
|
2575
|
+
days < -104249991374 ||
|
|
2576
|
+
days > 104249991374 ||
|
|
2577
|
+
hours < -2501999792983 ||
|
|
2578
|
+
hours > 2501999792983 ||
|
|
2579
|
+
minutes < -150119987579016 ||
|
|
2580
|
+
minutes > 150119987579016 ||
|
|
2581
|
+
seconds < -m ||
|
|
2582
|
+
seconds > m
|
|
2583
|
+
) {
|
|
2584
|
+
return "the time fields come to 2^53 seconds or more"
|
|
2585
|
+
}
|
|
2586
|
+
if (temporalWholeSeconds(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) > m) {
|
|
2587
|
+
return "the time fields come to 2^53 seconds or more"
|
|
2588
|
+
}
|
|
2589
|
+
return ""
|
|
2590
|
+
}
|
|
2591
|
+
|
|
2592
|
+
/** The largest unit `d` has a non-zero field in, or nanoseconds when it is blank. */
|
|
2593
|
+
const temporalLargestUnit = (d: Duration): i32 => {
|
|
2594
|
+
if (d.years !== 0) {
|
|
2595
|
+
return TEMPORAL_UNIT_YEAR
|
|
2596
|
+
}
|
|
2597
|
+
if (d.months !== 0) {
|
|
2598
|
+
return TEMPORAL_UNIT_MONTH
|
|
2599
|
+
}
|
|
2600
|
+
if (d.weeks !== 0) {
|
|
2601
|
+
return TEMPORAL_UNIT_WEEK
|
|
2602
|
+
}
|
|
2603
|
+
if (d.days !== 0) {
|
|
2604
|
+
return TEMPORAL_UNIT_DAY
|
|
2605
|
+
}
|
|
2606
|
+
if (d.hours !== 0) {
|
|
2607
|
+
return TEMPORAL_UNIT_HOUR
|
|
2608
|
+
}
|
|
2609
|
+
if (d.minutes !== 0) {
|
|
2610
|
+
return TEMPORAL_UNIT_MINUTE
|
|
2611
|
+
}
|
|
2612
|
+
if (d.seconds !== 0) {
|
|
2613
|
+
return TEMPORAL_UNIT_SECOND
|
|
2614
|
+
}
|
|
2615
|
+
if (d.milliseconds !== 0) {
|
|
2616
|
+
return TEMPORAL_UNIT_MILLISECOND
|
|
2617
|
+
}
|
|
2618
|
+
if (d.microseconds !== 0) {
|
|
2619
|
+
return TEMPORAL_UNIT_MICROSECOND
|
|
2620
|
+
}
|
|
2621
|
+
return TEMPORAL_UNIT_NANOSECOND
|
|
2622
|
+
}
|
|
2623
|
+
|
|
2624
|
+
/**
|
|
2625
|
+
* `Temporal.Duration.from(text)`: the duration an ISO 8601 string names,
|
|
2626
|
+
* `[+-]P[nY][nM][nW][nD][T[nH][nM][nS]]` in either case, where the last time
|
|
2627
|
+
* field may have a fraction of up to nine digits (`PT1.5H` is an hour and 30
|
|
2628
|
+
* minutes). Panics with a `RangeError` on anything else.
|
|
2629
|
+
*
|
|
2630
|
+
* @param text such as `P1Y2M`, `PT36H` or `-P1DT0.5S`
|
|
2631
|
+
* @returns the duration
|
|
2632
|
+
*/
|
|
2633
|
+
export const durationFrom = (text: string): Duration => {
|
|
2634
|
+
const d: Duration | null = durationFromOrNull(text)
|
|
2635
|
+
if (d === null) {
|
|
2636
|
+
temporalRangeError(`Duration: cannot parse "${text}"`)
|
|
2637
|
+
return new Duration()
|
|
2638
|
+
}
|
|
2639
|
+
return d
|
|
2640
|
+
}
|
|
2641
|
+
|
|
2642
|
+
/**
|
|
2643
|
+
* `durationFrom`, answering `null` instead of panicking on a string it
|
|
2644
|
+
* refuses. Not part of Temporal.
|
|
2645
|
+
*
|
|
2646
|
+
* @param text the string to parse
|
|
2647
|
+
* @returns the duration, or `null`
|
|
2648
|
+
*/
|
|
2649
|
+
export const durationFromOrNull = (text: string): Duration | null => {
|
|
2650
|
+
const r: TemporalReader = new TemporalReader(text)
|
|
2651
|
+
let negative: boolean = false
|
|
2652
|
+
if (r.eat(toI32(45))) {
|
|
2653
|
+
negative = true
|
|
2654
|
+
} else {
|
|
2655
|
+
r.eat(toI32(43))
|
|
2656
|
+
}
|
|
2657
|
+
if (!r.eat(toI32(80)) && !r.eat(toI32(112))) {
|
|
2658
|
+
return null
|
|
2659
|
+
}
|
|
2660
|
+
// Fields in order: Y M W D, then after T: H M S. `rank` is the last read.
|
|
2661
|
+
const values: i64[] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
2662
|
+
let rank: i32 = -1
|
|
2663
|
+
let inTime: boolean = false
|
|
2664
|
+
let any: boolean = false
|
|
2665
|
+
let fractionSeen: boolean = false
|
|
2666
|
+
let longest: i32 = 0
|
|
2667
|
+
while (!r.done()) {
|
|
2668
|
+
const c: i32 = r.peek(toI32(0))
|
|
2669
|
+
if (c === 84 || c === 116) {
|
|
2670
|
+
if (inTime) {
|
|
2671
|
+
return null
|
|
2672
|
+
}
|
|
2673
|
+
inTime = true
|
|
2674
|
+
r.at = r.at + 1
|
|
2675
|
+
if (r.done()) {
|
|
2676
|
+
return null
|
|
2677
|
+
}
|
|
2678
|
+
continue
|
|
2679
|
+
}
|
|
2680
|
+
if (fractionSeen || !r.digitAt(toI32(0))) {
|
|
2681
|
+
return null
|
|
2682
|
+
}
|
|
2683
|
+
let n: i64 = 0
|
|
2684
|
+
let count: i32 = 0
|
|
2685
|
+
while (r.digitAt(toI32(0))) {
|
|
2686
|
+
if (count === 16) {
|
|
2687
|
+
return null
|
|
2688
|
+
}
|
|
2689
|
+
n = n * 10 + toI64(r.peek(toI32(0)) - 48)
|
|
2690
|
+
count = count + 1
|
|
2691
|
+
r.at = r.at + 1
|
|
2692
|
+
}
|
|
2693
|
+
if (count > longest) {
|
|
2694
|
+
longest = count
|
|
2695
|
+
}
|
|
2696
|
+
const beforeFraction: i32 = r.at
|
|
2697
|
+
const fraction: i32 = r.fractionDigits()
|
|
2698
|
+
const sawFraction: boolean = r.at !== beforeFraction
|
|
2699
|
+
const unit: i32 = r.peek(toI32(0)) | 32 // lowercased
|
|
2700
|
+
r.at = r.at + 1
|
|
2701
|
+
const slot: i32 = temporalDurationSlot(unit, inTime)
|
|
2702
|
+
if (slot < 0 || slot >= toI32(values.length) || slot <= rank || !r.ok) {
|
|
2703
|
+
return null
|
|
2704
|
+
}
|
|
2705
|
+
rank = slot
|
|
2706
|
+
any = true
|
|
2707
|
+
values[slot] = n
|
|
2708
|
+
if (sawFraction) {
|
|
2709
|
+
if (!inTime) {
|
|
2710
|
+
return null
|
|
2711
|
+
}
|
|
2712
|
+
fractionSeen = true
|
|
2713
|
+
// The fraction of an hour, a minute or a second, spread over the smaller fields.
|
|
2714
|
+
let ns: i64 = toI64(fraction)
|
|
2715
|
+
if (slot === 4) {
|
|
2716
|
+
ns = ns * 3600
|
|
2717
|
+
} else if (slot === 5) {
|
|
2718
|
+
ns = ns * 60
|
|
2719
|
+
}
|
|
2720
|
+
if (slot === 4) {
|
|
2721
|
+
values[5] = temporalTruncDiv(ns, 60 * TEMPORAL_NS_PER_SECOND)
|
|
2722
|
+
ns = ns % (60 * TEMPORAL_NS_PER_SECOND)
|
|
2723
|
+
}
|
|
2724
|
+
if (slot <= 5) {
|
|
2725
|
+
values[6] = temporalTruncDiv(ns, TEMPORAL_NS_PER_SECOND)
|
|
2726
|
+
ns = ns % TEMPORAL_NS_PER_SECOND
|
|
2727
|
+
}
|
|
2728
|
+
values[7] = temporalTruncDiv(ns, 1000000)
|
|
2729
|
+
values[8] = temporalTruncDiv(ns, 1000) % 1000
|
|
2730
|
+
values[9] = ns % 1000
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
if (!any || !r.ok) {
|
|
2734
|
+
return null
|
|
2735
|
+
}
|
|
2736
|
+
const k: i64 = toI64(negative ? -1 : 1)
|
|
2737
|
+
// Fields of nine digits or fewer cannot reach any of the constructor's
|
|
2738
|
+
// bounds, so only a longer one is checked here first.
|
|
2739
|
+
const problem: string =
|
|
2740
|
+
longest <= 9
|
|
2741
|
+
? ""
|
|
2742
|
+
: temporalDurationProblem(
|
|
2743
|
+
values[0] * k,
|
|
2744
|
+
values[1] * k,
|
|
2745
|
+
values[2] * k,
|
|
2746
|
+
values[3] * k,
|
|
2747
|
+
values[4] * k,
|
|
2748
|
+
values[5] * k,
|
|
2749
|
+
values[6] * k,
|
|
2750
|
+
values[7] * k,
|
|
2751
|
+
values[8] * k,
|
|
2752
|
+
values[9] * k
|
|
2753
|
+
)
|
|
2754
|
+
if (problem.length > 0) {
|
|
2755
|
+
return null
|
|
2756
|
+
}
|
|
2757
|
+
return new Duration(
|
|
2758
|
+
values[0] * k,
|
|
2759
|
+
values[1] * k,
|
|
2760
|
+
values[2] * k,
|
|
2761
|
+
values[3] * k,
|
|
2762
|
+
values[4] * k,
|
|
2763
|
+
values[5] * k,
|
|
2764
|
+
values[6] * k,
|
|
2765
|
+
values[7] * k,
|
|
2766
|
+
values[8] * k,
|
|
2767
|
+
values[9] * k
|
|
2768
|
+
)
|
|
2769
|
+
}
|
|
2770
|
+
|
|
2771
|
+
/**
|
|
2772
|
+
* The field a duration designator fills: 0 to 3 for `Y`, `M`, `W` and `D`
|
|
2773
|
+
* before the `T`, 4 to 6 for `H`, `M` and `S` after it, and -1 for any other
|
|
2774
|
+
* character. `unit` is already lowercased.
|
|
2775
|
+
*/
|
|
2776
|
+
const temporalDurationSlot = (unit: i32, inTime: boolean): i32 => {
|
|
2777
|
+
const designators: string = inTime ? "hms" : "ymwd"
|
|
2778
|
+
for (let i: i32 = 0; i < toI32(designators.length); i++) {
|
|
2779
|
+
if (toI32(designators.charCodeAt(i)) === unit) {
|
|
2780
|
+
return inTime ? i + 4 : i
|
|
2781
|
+
}
|
|
2782
|
+
}
|
|
2783
|
+
return -1
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
/**
|
|
2787
|
+
* `Temporal.Duration.compare(a, b)`: -1, 0 or 1 as `a` is shorter than, as
|
|
2788
|
+
* long as, or longer than `b`, each day counted as 24 hours. Panics with a
|
|
2789
|
+
* `RangeError` when either has years, months or weeks, which Temporal
|
|
2790
|
+
* compares only against a `relativeTo` date; two durations with the same
|
|
2791
|
+
* fields compare equal whatever they hold.
|
|
2792
|
+
*
|
|
2793
|
+
* @param a the first duration
|
|
2794
|
+
* @param b the second duration
|
|
2795
|
+
* @returns the order of the two
|
|
2796
|
+
*/
|
|
2797
|
+
export const durationCompare = (a: Duration, b: Duration): i32 => {
|
|
2798
|
+
if (
|
|
2799
|
+
a.years === b.years &&
|
|
2800
|
+
a.months === b.months &&
|
|
2801
|
+
a.weeks === b.weeks &&
|
|
2802
|
+
a.days === b.days &&
|
|
2803
|
+
a.hours === b.hours &&
|
|
2804
|
+
a.minutes === b.minutes &&
|
|
2805
|
+
a.seconds === b.seconds &&
|
|
2806
|
+
a.milliseconds === b.milliseconds &&
|
|
2807
|
+
a.microseconds === b.microseconds &&
|
|
2808
|
+
a.nanoseconds === b.nanoseconds
|
|
2809
|
+
) {
|
|
2810
|
+
return 0
|
|
2811
|
+
}
|
|
2812
|
+
if (a.years !== 0 || a.months !== 0 || a.weeks !== 0 || b.years !== 0 || b.months !== 0 || b.weeks !== 0) {
|
|
2813
|
+
temporalRangeError("Duration: comparing years, months or weeks needs a relativeTo date")
|
|
2814
|
+
}
|
|
2815
|
+
return temporalSpanOfDuration(a, true).plus(temporalSpanOfDuration(b, true).negated()).sign()
|
|
2816
|
+
}
|
|
2817
|
+
|
|
2818
|
+
// ---------------------------------------------------------------------------
|
|
2819
|
+
// Instant
|
|
2820
|
+
// ---------------------------------------------------------------------------
|
|
2821
|
+
|
|
2822
|
+
/**
|
|
2823
|
+
* An exact point on the UTC timeline, to the nanosecond: `Temporal.Instant`.
|
|
2824
|
+
* It has no calendar and no time zone; `toString` writes it in UTC.
|
|
2825
|
+
*/
|
|
2826
|
+
export class Instant {
|
|
2827
|
+
/** Milliseconds since 1970-01-01T00:00:00Z, rounded toward negative infinity. */
|
|
2828
|
+
readonly epochMilliseconds: f64
|
|
2829
|
+
/** @internal Whole seconds since the epoch, rounded down; not part of Temporal. */
|
|
2830
|
+
readonly epochSeconds: i64
|
|
2831
|
+
/** @internal The nanoseconds past `epochSeconds`, 0 to 999,999,999; not part of Temporal. */
|
|
2832
|
+
readonly nanosecondOfSecond: i32
|
|
2833
|
+
|
|
2834
|
+
/**
|
|
2835
|
+
* The instant `epochSeconds` seconds and `nanoseconds` nanoseconds after the
|
|
2836
|
+
* epoch. This is this module's constructor, not Temporal's, whose
|
|
2837
|
+
* `new Temporal.Instant(epochNanoseconds)` takes a `bigint`; use
|
|
2838
|
+
* `instantFromEpochMilliseconds` or `instantFrom` for a portable program.
|
|
2839
|
+
* Panics with a `RangeError` outside ±10^8 days of the epoch.
|
|
2840
|
+
*
|
|
2841
|
+
* @param epochSeconds whole seconds since the epoch
|
|
2842
|
+
* @param nanoseconds nanoseconds to add, of any sign below 2^53
|
|
2843
|
+
*/
|
|
2844
|
+
constructor(epochSeconds: i64, nanoseconds: i64 = 0) {
|
|
2845
|
+
let seconds: i64 = epochSeconds
|
|
2846
|
+
let nanos: i64 = nanoseconds
|
|
2847
|
+
if (nanos < 0 || nanos >= TEMPORAL_NS_PER_SECOND) {
|
|
2848
|
+
seconds = seconds + temporalFloorDiv(nanos, TEMPORAL_NS_PER_SECOND)
|
|
2849
|
+
nanos = temporalFloorMod(nanos, TEMPORAL_NS_PER_SECOND)
|
|
2850
|
+
}
|
|
2851
|
+
if (!temporalInstantInRange(seconds, nanos)) {
|
|
2852
|
+
temporalRangeError("Instant: the instant is outside Temporal's range")
|
|
2853
|
+
}
|
|
2854
|
+
this.epochSeconds = seconds
|
|
2855
|
+
this.nanosecondOfSecond = toI32(nanos)
|
|
2856
|
+
this.epochMilliseconds = toF64(seconds * 1000 + toI64(nanos / 1000000))
|
|
2857
|
+
}
|
|
2858
|
+
|
|
2859
|
+
/**
|
|
2860
|
+
* The instant in UTC as Temporal writes it: `2024-01-15T09:05:00Z`, with the
|
|
2861
|
+
* fraction of the second to as many digits as it needs.
|
|
2862
|
+
*
|
|
2863
|
+
* @returns the RFC 3339 form
|
|
2864
|
+
*/
|
|
2865
|
+
toString(): string {
|
|
2866
|
+
const day: i64 = temporalFloorDiv(this.epochSeconds, TEMPORAL_SECONDS_PER_DAY)
|
|
2867
|
+
const secondOfDay: i64 = temporalFloorMod(this.epochSeconds, TEMPORAL_SECONDS_PER_DAY)
|
|
2868
|
+
const civil: i64 = temporalCivilOfEpochDay(day)
|
|
2869
|
+
const out: u8[] = new Array<u8>(TEMPORAL_TEXT_MAX)
|
|
2870
|
+
const year: i32 = temporalCivilYear(civil)
|
|
2871
|
+
const nanoOfDay: i64 = secondOfDay * TEMPORAL_NS_PER_SECOND + toI64(this.nanosecondOfSecond)
|
|
2872
|
+
let i: i32 = 0
|
|
2873
|
+
if (year >= 0 && year <= 9999) {
|
|
2874
|
+
const secs: i32 = toI32(secondOfDay)
|
|
2875
|
+
const minutes: i32 = toI32(secs / 60)
|
|
2876
|
+
i = temporalPutDateTime4(
|
|
2877
|
+
out,
|
|
2878
|
+
0,
|
|
2879
|
+
year,
|
|
2880
|
+
temporalCivilMonth(civil),
|
|
2881
|
+
temporalCivilDay(civil),
|
|
2882
|
+
toI32(minutes / 60),
|
|
2883
|
+
minutes % 60,
|
|
2884
|
+
secs - minutes * 60
|
|
2885
|
+
)
|
|
2886
|
+
i = temporalPutFraction(out, i, this.nanosecondOfSecond)
|
|
2887
|
+
} else {
|
|
2888
|
+
i = temporalPutDate(out, 0, year, temporalCivilMonth(civil), temporalCivilDay(civil))
|
|
2889
|
+
i = temporalPutNanoOfDay(out, temporalPutByte(out, i, 84), nanoOfDay)
|
|
2890
|
+
}
|
|
2891
|
+
return String.fromBytes(out, 0, temporalPutByte(out, i, 90))
|
|
2892
|
+
}
|
|
2893
|
+
|
|
2894
|
+
/**
|
|
2895
|
+
* The same string as `toString`.
|
|
2896
|
+
*
|
|
2897
|
+
* @returns the RFC 3339 form
|
|
2898
|
+
*/
|
|
2899
|
+
toJSON(): string {
|
|
2900
|
+
return this.toString()
|
|
2901
|
+
}
|
|
2902
|
+
|
|
2903
|
+
/**
|
|
2904
|
+
* Whether `other` is the same instant.
|
|
2905
|
+
*
|
|
2906
|
+
* @param other the instant to compare with
|
|
2907
|
+
* @returns whether the two are equal
|
|
2908
|
+
*/
|
|
2909
|
+
equals(other: Instant): boolean {
|
|
2910
|
+
return this.epochSeconds === other.epochSeconds && this.nanosecondOfSecond === other.nanosecondOfSecond
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2913
|
+
/**
|
|
2914
|
+
* This instant moved by `duration`, which may hold hours and smaller units
|
|
2915
|
+
* only: a day is not always 24 hours once a zone is involved, so Temporal
|
|
2916
|
+
* refuses days and larger units here, and so does this, with a `RangeError`.
|
|
2917
|
+
*
|
|
2918
|
+
* @param duration how far to move
|
|
2919
|
+
* @returns the new instant
|
|
2920
|
+
*/
|
|
2921
|
+
add(duration: Duration): Instant {
|
|
2922
|
+
if (duration.years !== 0 || duration.months !== 0 || duration.weeks !== 0 || duration.days !== 0) {
|
|
2923
|
+
temporalRangeError("Instant: a duration with days or larger units cannot be added to an instant")
|
|
2924
|
+
}
|
|
2925
|
+
const span: TemporalSpan = temporalSpanOfDuration(duration, false)
|
|
2926
|
+
return new Instant(this.epochSeconds + span.seconds, toI64(this.nanosecondOfSecond) + toI64(span.nanos))
|
|
2927
|
+
}
|
|
2928
|
+
|
|
2929
|
+
/**
|
|
2930
|
+
* This instant moved back by `duration`: `add` of its negation.
|
|
2931
|
+
*
|
|
2932
|
+
* @param duration how far to move back
|
|
2933
|
+
* @returns the new instant
|
|
2934
|
+
*/
|
|
2935
|
+
subtract(duration: Duration): Instant {
|
|
2936
|
+
return this.add(duration.negated())
|
|
2937
|
+
}
|
|
2938
|
+
|
|
2939
|
+
/**
|
|
2940
|
+
* The time from this instant to `other` in seconds and smaller units
|
|
2941
|
+
* (Temporal's default `largestUnit: "second"`).
|
|
2942
|
+
*
|
|
2943
|
+
* @param other the later instant
|
|
2944
|
+
* @returns the difference
|
|
2945
|
+
*/
|
|
2946
|
+
until(other: Instant): Duration {
|
|
2947
|
+
return temporalBalance(temporalInstantSpan(this, other), TEMPORAL_UNIT_SECOND)
|
|
2948
|
+
}
|
|
2949
|
+
|
|
2950
|
+
/**
|
|
2951
|
+
* The time from `other` to this instant: `other.until(this)`.
|
|
2952
|
+
*
|
|
2953
|
+
* @param other the earlier instant
|
|
2954
|
+
* @returns the difference
|
|
2955
|
+
*/
|
|
2956
|
+
since(other: Instant): Duration {
|
|
2957
|
+
return temporalBalance(temporalInstantSpan(other, this), TEMPORAL_UNIT_SECOND)
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
|
|
2961
|
+
/** The span from `a` to `b`. */
|
|
2962
|
+
const temporalInstantSpan = (a: Instant, b: Instant): TemporalSpan =>
|
|
2963
|
+
new TemporalSpan(b.epochSeconds - a.epochSeconds, toI64(b.nanosecondOfSecond) - toI64(a.nanosecondOfSecond))
|
|
2964
|
+
|
|
2965
|
+
/**
|
|
2966
|
+
* `Temporal.Instant.fromEpochMilliseconds(ms)`: the instant `ms` milliseconds
|
|
2967
|
+
* after 1970-01-01T00:00:00Z. Panics with a `RangeError` when `ms` is not a
|
|
2968
|
+
* whole number or is outside ±8.64 × 10^15.
|
|
2969
|
+
*
|
|
2970
|
+
* @param ms milliseconds since the epoch
|
|
2971
|
+
* @returns the instant
|
|
2972
|
+
*/
|
|
2973
|
+
export const instantFromEpochMilliseconds = (ms: f64): Instant => {
|
|
2974
|
+
if (ms !== Math.floor(ms) || ms > 8.64e15 || ms < -8.64e15) {
|
|
2975
|
+
temporalRangeError("Instant: epoch milliseconds must be a whole number within ±8.64e15")
|
|
2976
|
+
}
|
|
2977
|
+
const whole: i64 = toI64(ms)
|
|
2978
|
+
return new Instant(temporalFloorDiv(whole, 1000), temporalFloorMod(whole, 1000) * 1000000)
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2981
|
+
/**
|
|
2982
|
+
* `Temporal.Instant.from(text)`: the instant an RFC 9557 string names. It
|
|
2983
|
+
* must have a date, a time and either `Z` or a numeric offset, which is
|
|
2984
|
+
* subtracted (`2024-01-15T09:05+01:00` is `2024-01-15T08:05:00Z`); a
|
|
2985
|
+
* bracketed time zone after it is ignored. Panics with a `RangeError` on
|
|
2986
|
+
* anything else.
|
|
2987
|
+
*
|
|
2988
|
+
* @param text such as `2024-01-15T09:05:00Z`
|
|
2989
|
+
* @returns the instant
|
|
2990
|
+
*/
|
|
2991
|
+
export const instantFrom = (text: string): Instant => {
|
|
2992
|
+
const i: Instant | null = instantFromOrNull(text)
|
|
2993
|
+
if (i === null) {
|
|
2994
|
+
temporalRangeError(`Instant: cannot parse "${text}"`)
|
|
2995
|
+
return new Instant(0)
|
|
2996
|
+
}
|
|
2997
|
+
return i
|
|
2998
|
+
}
|
|
2999
|
+
|
|
3000
|
+
/**
|
|
3001
|
+
* `instantFrom`, answering `null` instead of panicking on a string it
|
|
3002
|
+
* refuses. Not part of Temporal.
|
|
3003
|
+
*
|
|
3004
|
+
* @param text the string to parse
|
|
3005
|
+
* @returns the instant, or `null`
|
|
3006
|
+
*/
|
|
3007
|
+
export const instantFromOrNull = (text: string): Instant | null => {
|
|
3008
|
+
const length: i32 = toI32(text.length)
|
|
3009
|
+
const last: i32 = temporalCharAt(text, length - 1)
|
|
3010
|
+
// `Z`, or a `±HH:MM` offset, ends the string: the two forms RFC 3339 writes.
|
|
3011
|
+
let timeEnd: i32 = -1
|
|
3012
|
+
let offsetSeconds: i64 = 0
|
|
3013
|
+
if (last === 90 || last === 122) {
|
|
3014
|
+
timeEnd = length - 1
|
|
3015
|
+
} else {
|
|
3016
|
+
const sign: i32 = temporalCharAt(text, length - 6)
|
|
3017
|
+
const hours: i32 = temporalFastTwo(text, length - 5)
|
|
3018
|
+
const minutes: i32 = temporalFastTwo(text, length - 2)
|
|
3019
|
+
if (
|
|
3020
|
+
(sign === 43 || sign === 45) &&
|
|
3021
|
+
temporalCharAt(text, length - 3) === 58 &&
|
|
3022
|
+
hours >= 0 &&
|
|
3023
|
+
hours <= 23
|
|
3024
|
+
) {
|
|
3025
|
+
if (minutes >= 0 && minutes <= 59) {
|
|
3026
|
+
timeEnd = length - 6
|
|
3027
|
+
offsetSeconds = toI64(hours * 3600 + minutes * 60) * (sign === 43 ? 1 : -1)
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
}
|
|
3031
|
+
if (timeEnd >= 16 && temporalIsTimeDesignator(temporalCharAt(text, 10))) {
|
|
3032
|
+
const civil: i64 = temporalFastDate(text)
|
|
3033
|
+
const nanoOfDay: i64 = civil < 0 ? -1 : temporalFastTime(text, 11, timeEnd)
|
|
3034
|
+
if (nanoOfDay >= 0) {
|
|
3035
|
+
const day: i64 = temporalEpochDay(
|
|
3036
|
+
toI64(temporalCivilYear(civil)),
|
|
3037
|
+
temporalCivilMonth(civil),
|
|
3038
|
+
temporalCivilDay(civil)
|
|
3039
|
+
)
|
|
3040
|
+
return new Instant(
|
|
3041
|
+
day * TEMPORAL_SECONDS_PER_DAY + toI64(nanoOfDay / TEMPORAL_NS_PER_SECOND) - offsetSeconds,
|
|
3042
|
+
nanoOfDay % TEMPORAL_NS_PER_SECOND
|
|
3043
|
+
)
|
|
3044
|
+
}
|
|
3045
|
+
}
|
|
3046
|
+
const r: TemporalReader = temporalReadDateTime(text)
|
|
3047
|
+
if (!r.ok || !r.hasTime || (!r.utc && !r.hasOffset)) {
|
|
3048
|
+
return null
|
|
3049
|
+
}
|
|
3050
|
+
const day: i64 = temporalEpochDay(toI64(r.year), r.month, r.day)
|
|
3051
|
+
const seconds: i64 =
|
|
3052
|
+
day * TEMPORAL_SECONDS_PER_DAY + toI64(r.hour) * 3600 + toI64(r.minute) * 60 + toI64(r.second)
|
|
3053
|
+
// The offset is below a day, so its nanoseconds stay below 2^53.
|
|
3054
|
+
const nanos: i64 = toI64(r.fraction) - r.offsetNanos
|
|
3055
|
+
const exactSeconds: i64 = seconds + temporalFloorDiv(nanos, TEMPORAL_NS_PER_SECOND)
|
|
3056
|
+
const exactNanos: i64 = temporalFloorMod(nanos, TEMPORAL_NS_PER_SECOND)
|
|
3057
|
+
if (!temporalInstantInRange(exactSeconds, exactNanos)) {
|
|
3058
|
+
return null
|
|
3059
|
+
}
|
|
3060
|
+
return new Instant(exactSeconds, exactNanos)
|
|
3061
|
+
}
|
|
3062
|
+
|
|
3063
|
+
/** Whether `seconds` and `nanos` (in `[0, 10^9)`) past the epoch are within Temporal's ±10^8 days. */
|
|
3064
|
+
const temporalInstantInRange = (seconds: i64, nanos: i64): boolean => {
|
|
3065
|
+
const limit: i64 = TEMPORAL_MAX_EPOCH_DAY * TEMPORAL_SECONDS_PER_DAY
|
|
3066
|
+
return seconds >= -limit && seconds <= limit && !(seconds === limit && nanos > 0)
|
|
3067
|
+
}
|
|
3068
|
+
|
|
3069
|
+
/**
|
|
3070
|
+
* `Temporal.Instant.compare(a, b)`: -1, 0 or 1 as `a` is before, the same as,
|
|
3071
|
+
* or after `b`.
|
|
3072
|
+
*
|
|
3073
|
+
* @param a the first instant
|
|
3074
|
+
* @param b the second instant
|
|
3075
|
+
* @returns the order of the two
|
|
3076
|
+
*/
|
|
3077
|
+
export const instantCompare = (a: Instant, b: Instant): i32 => temporalInstantSpan(b, a).sign()
|
|
3078
|
+
|
|
3079
|
+
/**
|
|
3080
|
+
* `Temporal.Now.instant()`: the wall clock now, to the nanosecond the
|
|
3081
|
+
* operating system reports. Capability: clock.
|
|
3082
|
+
*
|
|
3083
|
+
* @returns the current instant
|
|
3084
|
+
*/
|
|
3085
|
+
export const nowInstant = (): Instant => {
|
|
3086
|
+
const ns: i64 = wallClockNanos()
|
|
3087
|
+
return new Instant(
|
|
3088
|
+
temporalFloorDiv(ns, TEMPORAL_NS_PER_SECOND),
|
|
3089
|
+
temporalFloorMod(ns, TEMPORAL_NS_PER_SECOND)
|
|
3090
|
+
)
|
|
3091
|
+
}
|