oakscriptjs 0.15.1 → 0.15.3
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/dist/cjs/callsite/index.d.ts.map +1 -1
- package/dist/cjs/callsite/index.js +61 -7
- package/dist/cjs/callsite/index.js.map +2 -2
- package/dist/cjs/drawing/registry.d.ts +31 -3
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +188 -68
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/linefill/index.d.ts.map +1 -1
- package/dist/cjs/linefill/index.js +14 -5
- package/dist/cjs/linefill/index.js.map +2 -2
- package/dist/cjs/session/bars.d.ts +3 -0
- package/dist/cjs/session/bars.d.ts.map +1 -1
- package/dist/cjs/session/bars.js +10 -3
- package/dist/cjs/session/bars.js.map +2 -2
- package/dist/cjs/session/calendar.d.ts +19 -6
- package/dist/cjs/session/calendar.d.ts.map +1 -1
- package/dist/cjs/session/calendar.js +57 -18
- package/dist/cjs/session/calendar.js.map +2 -2
- package/dist/cjs/session/context.d.ts.map +1 -1
- package/dist/cjs/session/context.js +25 -8
- package/dist/cjs/session/context.js.map +2 -2
- package/dist/cjs/str/floatingdecimal.d.ts +3 -0
- package/dist/cjs/str/floatingdecimal.d.ts.map +1 -1
- package/dist/cjs/str/floatingdecimal.js +25 -12
- package/dist/cjs/str/floatingdecimal.js.map +2 -2
- package/dist/cjs/str/messageformat.d.ts.map +1 -1
- package/dist/cjs/str/messageformat.js +27 -8
- package/dist/cjs/str/messageformat.js.map +2 -2
- package/dist/cjs/str/numberformat.d.ts.map +1 -1
- package/dist/cjs/str/numberformat.js +9 -16
- package/dist/cjs/str/numberformat.js.map +3 -3
- package/dist/cjs/ta/index.d.ts.map +1 -1
- package/dist/cjs/ta/index.js +27 -0
- package/dist/cjs/ta/index.js.map +2 -2
- package/dist/cjs/ta/running-sum.d.ts +8 -1
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +13 -1
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/time/timezone.d.ts +13 -1
- package/dist/cjs/time/timezone.d.ts.map +1 -1
- package/dist/cjs/time/timezone.js +64 -12
- package/dist/cjs/time/timezone.js.map +2 -2
- package/dist/esm/callsite/index.d.ts.map +1 -1
- package/dist/esm/callsite/index.js +76 -7
- package/dist/esm/callsite/index.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +31 -3
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +237 -80
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/linefill/index.d.ts.map +1 -1
- package/dist/esm/linefill/index.js +17 -7
- package/dist/esm/linefill/index.js.map +1 -1
- package/dist/esm/session/bars.d.ts +3 -0
- package/dist/esm/session/bars.d.ts.map +1 -1
- package/dist/esm/session/bars.js +11 -3
- package/dist/esm/session/bars.js.map +1 -1
- package/dist/esm/session/calendar.d.ts +19 -6
- package/dist/esm/session/calendar.d.ts.map +1 -1
- package/dist/esm/session/calendar.js +74 -21
- package/dist/esm/session/calendar.js.map +1 -1
- package/dist/esm/session/context.d.ts.map +1 -1
- package/dist/esm/session/context.js +34 -9
- package/dist/esm/session/context.js.map +1 -1
- package/dist/esm/str/floatingdecimal.d.ts +3 -0
- package/dist/esm/str/floatingdecimal.d.ts.map +1 -1
- package/dist/esm/str/floatingdecimal.js +27 -11
- package/dist/esm/str/floatingdecimal.js.map +1 -1
- package/dist/esm/str/messageformat.d.ts.map +1 -1
- package/dist/esm/str/messageformat.js +35 -9
- package/dist/esm/str/messageformat.js.map +1 -1
- package/dist/esm/str/numberformat.d.ts.map +1 -1
- package/dist/esm/str/numberformat.js +12 -18
- package/dist/esm/str/numberformat.js.map +1 -1
- package/dist/esm/ta/index.d.ts.map +1 -1
- package/dist/esm/ta/index.js +35 -0
- package/dist/esm/ta/index.js.map +1 -1
- package/dist/esm/ta/running-sum.d.ts +8 -1
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +16 -1
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/time/timezone.d.ts +13 -1
- package/dist/esm/time/timezone.d.ts.map +1 -1
- package/dist/esm/time/timezone.js +97 -16
- package/dist/esm/time/timezone.js.map +1 -1
- package/package.json +1 -1
- package/src/callsite/index.ts +65 -7
- package/src/drawing/registry.ts +249 -79
- package/src/index.ts +1 -1
- package/src/linefill/index.ts +16 -7
- package/src/session/bars.ts +11 -3
- package/src/session/calendar.ts +84 -21
- package/src/session/context.ts +36 -9
- package/src/str/floatingdecimal.ts +27 -11
- package/src/str/messageformat.ts +40 -9
- package/src/str/numberformat.ts +13 -17
- package/src/ta/index.ts +41 -0
- package/src/ta/running-sum.ts +13 -1
- package/src/time/timezone.ts +99 -16
package/src/time/timezone.ts
CHANGED
|
@@ -36,13 +36,18 @@ type Zone =
|
|
|
36
36
|
| {
|
|
37
37
|
readonly kind: 'iana';
|
|
38
38
|
readonly format: Intl.DateTimeFormat;
|
|
39
|
-
/** Offset of each UTC
|
|
39
|
+
/** Offset at the start of each UTC day (`floor(time / 1 day)`) */
|
|
40
|
+
readonly dayStarts: Map<number, number>;
|
|
41
|
+
/** Offset of each UTC hour (`floor(time / 1 h)`) of a day with an offset change; null for an hour with the
|
|
42
|
+
* change inside */
|
|
40
43
|
readonly hours: Map<number, number | null>;
|
|
41
44
|
};
|
|
42
45
|
|
|
43
46
|
const MS_PER_HOUR = 3_600_000;
|
|
44
47
|
/** Cached hours per zone before the cache restarts (about 11 years of hours) */
|
|
45
48
|
const MAX_CACHED_HOURS = 100_000;
|
|
49
|
+
/** Cached days per zone before the cache restarts (about 270 years) */
|
|
50
|
+
const MAX_CACHED_DAYS = 100_000;
|
|
46
51
|
|
|
47
52
|
const zones = new Map<string, Zone>();
|
|
48
53
|
|
|
@@ -80,19 +85,58 @@ function resolveZone(timezone: string): Zone {
|
|
|
80
85
|
} catch {
|
|
81
86
|
throw invalid();
|
|
82
87
|
}
|
|
83
|
-
zone = { kind: 'iana', format, hours: new Map() };
|
|
88
|
+
zone = { kind: 'iana', format, dayStarts: new Map(), hours: new Map() };
|
|
84
89
|
}
|
|
85
90
|
zones.set(timezone, zone);
|
|
86
91
|
return zone;
|
|
87
92
|
}
|
|
88
93
|
|
|
94
|
+
/** Days whose starts are read together (see `dayStartOffset`) */
|
|
95
|
+
const DAYS_PER_BLOCK = 3;
|
|
96
|
+
/** Last day whose block and the next one are inside the `Date` range (8.64e15 ms on each side of 1970) */
|
|
97
|
+
const LAST_BLOCK_DAY = 100_000_000 - DAYS_PER_BLOCK;
|
|
98
|
+
|
|
99
|
+
/** Offset of the zone at the start of UTC day `day`, from `Intl`, cached. */
|
|
100
|
+
function formattedDayStart(zone: Zone & { kind: 'iana' }, day: number): number {
|
|
101
|
+
let offset = zone.dayStarts.get(day);
|
|
102
|
+
if (offset === undefined) {
|
|
103
|
+
offset = formattedOffset(zone.format, day * MS_PER_DAY);
|
|
104
|
+
zone.dayStarts.set(day, offset);
|
|
105
|
+
}
|
|
106
|
+
return offset;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Offset of the zone at the start of UTC day `day`, cached. The days are read by blocks of three: two offset changes
|
|
111
|
+
* are always more than three days apart (167 hours at least, see `offsetAt`), so when the starts of a block and of
|
|
112
|
+
* the next one have the same offset, no change happens in between and the days of the block have it: one `Intl`
|
|
113
|
+
* call for three days instead of one per day.
|
|
114
|
+
*/
|
|
115
|
+
function dayStartOffset(zone: Zone & { kind: 'iana' }, day: number): number {
|
|
116
|
+
const cached = zone.dayStarts.get(day);
|
|
117
|
+
if (cached !== undefined) return cached;
|
|
118
|
+
if (zone.dayStarts.size >= MAX_CACHED_DAYS) zone.dayStarts.clear();
|
|
119
|
+
if (!(Math.abs(day) <= LAST_BLOCK_DAY)) return formattedDayStart(zone, day);
|
|
120
|
+
const first = Math.floor(day / DAYS_PER_BLOCK) * DAYS_PER_BLOCK;
|
|
121
|
+
const offset = formattedDayStart(zone, first);
|
|
122
|
+
if (offset !== formattedDayStart(zone, first + DAYS_PER_BLOCK)) return formattedDayStart(zone, day);
|
|
123
|
+
for (let d = first + 1; d < first + DAYS_PER_BLOCK; d++) zone.dayStarts.set(d, offset);
|
|
124
|
+
return offset;
|
|
125
|
+
}
|
|
126
|
+
|
|
89
127
|
/**
|
|
90
128
|
* Offset of the zone from UTC at instant `time`, in milliseconds (local = UTC + offset).
|
|
91
|
-
*
|
|
92
|
-
*
|
|
129
|
+
*
|
|
130
|
+
* Two offset changes of a zone are always more than a day apart (the closest ones of the time zone database are 167
|
|
131
|
+
* hours apart), so when a UTC day starts with the offset of the next day, the whole day has it: daily bars cost one
|
|
132
|
+
* lookup per day. In a day with a change, an hour whose first and last second have the same offset has it (an offset
|
|
133
|
+
* change happens at most once in an hour): it is computed once and cached.
|
|
93
134
|
*/
|
|
94
135
|
function offsetAt(zone: Zone, time: number): number {
|
|
95
136
|
if (zone.kind === 'fixed') return zone.offsetMs;
|
|
137
|
+
const day = Math.floor(time / MS_PER_DAY);
|
|
138
|
+
const start = dayStartOffset(zone, day);
|
|
139
|
+
if (start === dayStartOffset(zone, day + 1)) return start;
|
|
96
140
|
const hour = Math.floor(time / MS_PER_HOUR);
|
|
97
141
|
let offset = zone.hours.get(hour);
|
|
98
142
|
if (offset === undefined) {
|
|
@@ -135,17 +179,48 @@ export function zoneOffset(time: number, timezone: string): number {
|
|
|
135
179
|
return offsetAt(resolveZone(timezone), time);
|
|
136
180
|
}
|
|
137
181
|
|
|
138
|
-
/**
|
|
182
|
+
/** Last valid `Date` time on each side of 01/01/1970 (ms) */
|
|
183
|
+
const MAX_DATE_MS = 8.64e15;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Year, month (1-12) and day of the month of a day count since 01/01/1970, in the proleptic Gregorian calendar as
|
|
187
|
+
* `Date` reads it, by integer arithmetic (years counted from March, the leap day last).
|
|
188
|
+
*/
|
|
189
|
+
export function civilFromDays(days: number): { year: number; month: number; day: number } {
|
|
190
|
+
const z = days + 719_468;
|
|
191
|
+
const era = Math.floor(z / 146_097);
|
|
192
|
+
const dayOfEra = z - era * 146_097;
|
|
193
|
+
const yearOfEra = Math.floor(
|
|
194
|
+
(dayOfEra - Math.floor(dayOfEra / 1460) + Math.floor(dayOfEra / 36_524) - Math.floor(dayOfEra / 146_096)) / 365
|
|
195
|
+
);
|
|
196
|
+
const dayOfYear = dayOfEra - (yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100));
|
|
197
|
+
const mp = Math.floor((5 * dayOfYear + 2) / 153);
|
|
198
|
+
const month = mp < 10 ? mp + 3 : mp - 9;
|
|
199
|
+
return { year: yearOfEra + era * 400 + (month <= 2 ? 1 : 0), month, day: dayOfYear - Math.floor((153 * mp + 2) / 5) + 1 };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Splits a UNIX time (ms) into calendar fields in `timezone`: the fields `Date` gives for the wall clock (whole ms,
|
|
204
|
+
* NaN outside its range), computed without one as every bar reads them.
|
|
205
|
+
*/
|
|
139
206
|
export function zonedFields(time: number, timezone: string): ZonedFields {
|
|
140
|
-
const
|
|
207
|
+
const wall = time + offsetAt(resolveZone(timezone), time);
|
|
208
|
+
if (!(Math.abs(wall) <= MAX_DATE_MS)) {
|
|
209
|
+
return { year: NaN, month: NaN, day: NaN, hour: NaN, minute: NaN, second: NaN, dayOfWeek: NaN };
|
|
210
|
+
}
|
|
211
|
+
const ms = Math.trunc(wall);
|
|
212
|
+
const days = Math.floor(ms / MS_PER_DAY);
|
|
213
|
+
const inDay = ms - days * MS_PER_DAY;
|
|
214
|
+
const { year, month, day } = civilFromDays(days);
|
|
141
215
|
return {
|
|
142
|
-
year
|
|
143
|
-
month
|
|
144
|
-
day
|
|
145
|
-
hour:
|
|
146
|
-
minute:
|
|
147
|
-
second:
|
|
148
|
-
|
|
216
|
+
year,
|
|
217
|
+
month,
|
|
218
|
+
day,
|
|
219
|
+
hour: Math.floor(inDay / MS_PER_HOUR),
|
|
220
|
+
minute: Math.floor(inDay / 60_000) % 60,
|
|
221
|
+
second: Math.floor(inDay / MS_PER_SECOND) % 60,
|
|
222
|
+
// 01/01/1970 is a Thursday (5)
|
|
223
|
+
dayOfWeek: ((((days + 4) % 7) + 7) % 7) + 1,
|
|
149
224
|
};
|
|
150
225
|
}
|
|
151
226
|
|
|
@@ -163,6 +238,9 @@ export function zonedFields(time: number, timezone: string): ZonedFields {
|
|
|
163
238
|
*/
|
|
164
239
|
export function civilToUnix(year: number, month: number, day: number, hour = 0, minute = 0, second = 0): number {
|
|
165
240
|
const gregorian = Date.UTC(year, month - 1, day, hour, minute, second);
|
|
241
|
+
// a month of a year after 1582 (and before the end of the Date range) is Gregorian whatever the day: no need to
|
|
242
|
+
// normalize the date
|
|
243
|
+
if (year >= 1583 && year < 275_760 && month >= 1 && month < 13) return gregorian;
|
|
166
244
|
// normalized year / month / day of the Gregorian reading decides the calendar
|
|
167
245
|
const d = new Date(Date.UTC(year, month - 1, 1));
|
|
168
246
|
const y = d.getUTCFullYear();
|
|
@@ -200,8 +278,13 @@ export function zonedToUnix(
|
|
|
200
278
|
// one day before and one day after are the two candidates.
|
|
201
279
|
const before = offsetAt(zone, local - MS_PER_DAY);
|
|
202
280
|
const after = offsetAt(zone, local + MS_PER_DAY);
|
|
203
|
-
|
|
281
|
+
// the same offset on both sides: no change near `local`, the wall time is read with it
|
|
282
|
+
if (before === after) return local - before;
|
|
283
|
+
const withBefore = local - before;
|
|
284
|
+
const withAfter = local - after;
|
|
285
|
+
const beforeFits = local - offsetAt(zone, withBefore) === withBefore;
|
|
286
|
+
const afterFits = local - offsetAt(zone, withAfter) === withAfter;
|
|
204
287
|
// a wall time that occurs twice: the later instant, or the earlier one with `earlier`
|
|
205
|
-
if (
|
|
206
|
-
return
|
|
288
|
+
if (beforeFits && afterFits) return earlier ? Math.min(withBefore, withAfter) : Math.max(withBefore, withAfter);
|
|
289
|
+
return afterFits ? withAfter : withBefore;
|
|
207
290
|
}
|