oakscriptjs 0.15.2 → 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/drawing/registry.d.ts +3 -0
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +96 -43
- 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/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/time/timezone.d.ts +13 -1
- package/dist/cjs/time/timezone.d.ts.map +1 -1
- package/dist/cjs/time/timezone.js +52 -13
- package/dist/cjs/time/timezone.js.map +2 -2
- package/dist/esm/drawing/registry.d.ts +3 -0
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +112 -48
- 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/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/time/timezone.d.ts +13 -1
- package/dist/esm/time/timezone.d.ts.map +1 -1
- package/dist/esm/time/timezone.js +78 -17
- package/dist/esm/time/timezone.js.map +1 -1
- package/package.json +1 -1
- package/src/drawing/registry.ts +118 -47
- package/src/index.ts +1 -1
- package/src/session/bars.ts +11 -3
- package/src/session/calendar.ts +84 -21
- package/src/session/context.ts +36 -9
- package/src/time/timezone.ts +76 -15
package/src/session/calendar.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* dates (early closes, days off); holidays ("session_holidays") are dates without session
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
import {
|
|
21
|
+
import { civilFromDays, zoneOffset, zonedToUnix } from '../time/timezone.js';
|
|
22
22
|
import { parsePineSession } from '../time/session.js';
|
|
23
23
|
|
|
24
24
|
/** Session information of a symbol (hours, corrections, holidays). */
|
|
@@ -54,13 +54,40 @@ interface Version {
|
|
|
54
54
|
|
|
55
55
|
export const toDateNum = (y: number, m: number, d: number): DateNum => y * 10000 + m * 100 + d;
|
|
56
56
|
const utcOf = (date: DateNum): number => Date.UTC(Math.floor(date / 10000), Math.floor(date / 100) % 100 - 1, date % 100);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
|
|
57
|
+
|
|
58
|
+
const MS_PER_DAY = 86_400_000;
|
|
59
|
+
/** Days of a valid `Date` on each side of 01/01/1970 (8.64e15 ms) */
|
|
60
|
+
const MAX_DAY = 100_000_000;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Days since 01/01/1970 of a date, as `utcOf(date) / 1 day` but without a `Date`: the calendar functions step
|
|
64
|
+
* through days on every bar. Years before 100 and months outside 1-12 keep the `Date.UTC` reading (years 0-99 are
|
|
65
|
+
* 1900-1999 there). NaN outside the `Date` range.
|
|
66
|
+
*/
|
|
67
|
+
function dayOf(date: DateNum): number {
|
|
68
|
+
let y = Math.floor(date / 10000);
|
|
69
|
+
const m = Math.floor(date / 100) % 100;
|
|
70
|
+
if (!(y >= 100 && m >= 1 && m <= 12)) return utcOf(date) / MS_PER_DAY;
|
|
71
|
+
// proleptic Gregorian day count, years starting in March (the leap day last)
|
|
72
|
+
if (m <= 2) y--;
|
|
73
|
+
const era = Math.floor(y / 400);
|
|
74
|
+
const yearOfEra = y - era * 400;
|
|
75
|
+
const dayOfYear = Math.floor((153 * (m > 2 ? m - 3 : m + 9) + 2) / 5) + (date % 100) - 1;
|
|
76
|
+
const day = era * 146_097 + yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100) + dayOfYear - 719_468;
|
|
77
|
+
return Math.abs(day) <= MAX_DAY ? day : NaN;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Date of a day since 01/01/1970 (the inverse of `dayOf`); NaN outside the `Date` range. */
|
|
81
|
+
function dateOf(day: number): DateNum {
|
|
82
|
+
if (!(Math.abs(day) <= MAX_DAY)) return NaN;
|
|
83
|
+
const { year, month, day: d } = civilFromDays(day);
|
|
84
|
+
return toDateNum(year, month, d);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The date `n` (whole) days after `date`. */
|
|
88
|
+
export const addDays = (date: DateNum, n: number): DateNum => dateOf(dayOf(date) + n);
|
|
89
|
+
/** 1 = Sunday ... 7 = Saturday (01/01/1970 is a Thursday) */
|
|
90
|
+
export const weekdayOf = (date: DateNum): number => ((((dayOf(date) + 4) % 7) + 7) % 7) + 1;
|
|
64
91
|
/** 00:00 UTC of the date, in ms (PineScript `time_tradingday`). */
|
|
65
92
|
export const dateUtcMs = utcOf;
|
|
66
93
|
|
|
@@ -132,13 +159,25 @@ function parseVersion(text: string, from: DateNum, spec: string, rule: 'symbol'
|
|
|
132
159
|
return { from, periods, days };
|
|
133
160
|
}
|
|
134
161
|
|
|
162
|
+
/** Trading day and period of a time (see `TradingCalendar.locate`). */
|
|
163
|
+
interface Located {
|
|
164
|
+
date: DateNum;
|
|
165
|
+
index: number;
|
|
166
|
+
inside: boolean;
|
|
167
|
+
}
|
|
168
|
+
|
|
135
169
|
/** Session periods of every trading day of a symbol, as UNIX times (ms). */
|
|
136
170
|
export class TradingCalendar {
|
|
137
171
|
private readonly versions: Version[];
|
|
138
172
|
private readonly corrections = new Map<DateNum, Period[] | null>();
|
|
139
173
|
private readonly holidays: Set<DateNum>;
|
|
140
174
|
private readonly cache = new Map<DateNum, Array<[number, number]>>();
|
|
175
|
+
/** The date and periods of each day since 01/01/1970 that `search` looked at */
|
|
176
|
+
private readonly days = new Map<number, { date: DateNum; periods: Array<[number, number]> }>();
|
|
141
177
|
private readonly pine: boolean;
|
|
178
|
+
/** The last time located and its result: the functions of one bar locate the same time several times */
|
|
179
|
+
private lastTime = NaN;
|
|
180
|
+
private lastAt: Located | null = null;
|
|
142
181
|
|
|
143
182
|
/**
|
|
144
183
|
* @param timezone - Time zone of the session hours (the exchange time zone)
|
|
@@ -180,7 +219,7 @@ export class TradingCalendar {
|
|
|
180
219
|
if (correction !== undefined) {
|
|
181
220
|
periods = correction ?? [];
|
|
182
221
|
} else if (!this.holidays.has(date)) {
|
|
183
|
-
const version =
|
|
222
|
+
const version = this.versionOf(date);
|
|
184
223
|
if (version.byDay) periods = version.byDay[weekdayOf(date)]!;
|
|
185
224
|
else if (version.days[weekdayOf(date)]) periods = version.periods;
|
|
186
225
|
}
|
|
@@ -204,6 +243,13 @@ export class TradingCalendar {
|
|
|
204
243
|
return out;
|
|
205
244
|
}
|
|
206
245
|
|
|
246
|
+
/** The session version in force on `date` (the last one that starts on or before it). */
|
|
247
|
+
private versionOf(date: DateNum): Version {
|
|
248
|
+
let k = this.versions.length - 1;
|
|
249
|
+
while (k >= 0 && !(this.versions[k]!.from <= date)) k--;
|
|
250
|
+
return this.versions[k]!;
|
|
251
|
+
}
|
|
252
|
+
|
|
207
253
|
isTradingDay(date: DateNum): boolean {
|
|
208
254
|
return this.periods(date).length > 0;
|
|
209
255
|
}
|
|
@@ -215,7 +261,7 @@ export class TradingCalendar {
|
|
|
215
261
|
afterEarlyClose(time: number): boolean {
|
|
216
262
|
const at = this.locate(time);
|
|
217
263
|
if (!at || at.inside || !this.corrections.get(at.date)) return false;
|
|
218
|
-
const version =
|
|
264
|
+
const version = this.versionOf(at.date);
|
|
219
265
|
const usual = version.byDay ? version.byDay[weekdayOf(at.date)]! : version.days[weekdayOf(at.date)] ? version.periods : [];
|
|
220
266
|
const y = Math.floor(at.date / 10000);
|
|
221
267
|
const m = Math.floor(at.date / 100) % 100;
|
|
@@ -231,26 +277,43 @@ export class TradingCalendar {
|
|
|
231
277
|
* Trading day and period of a time: the period that contains it, else the last period that started
|
|
232
278
|
* before it. Null when no trading day is found near the time.
|
|
233
279
|
*/
|
|
234
|
-
locate(time: number):
|
|
235
|
-
|
|
236
|
-
|
|
280
|
+
locate(time: number): Located | null {
|
|
281
|
+
if (time !== this.lastTime) {
|
|
282
|
+
this.lastAt = this.search(time);
|
|
283
|
+
this.lastTime = time;
|
|
284
|
+
}
|
|
285
|
+
const at = this.lastAt;
|
|
286
|
+
return at && { date: at.date, index: at.index, inside: at.inside };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private search(time: number): Located | null {
|
|
290
|
+
// the local date of the time, as `Date` reads the wall clock (whole ms, NaN outside its range)
|
|
291
|
+
const wall = time + zoneOffset(time, this.timezone);
|
|
292
|
+
const local = dayOf(dateOf(Math.abs(wall) <= MAX_DAY * MS_PER_DAY ? Math.floor(Math.trunc(wall) / MS_PER_DAY) : NaN));
|
|
237
293
|
for (let k = 0; k <= 5; k++) {
|
|
238
|
-
const date =
|
|
239
|
-
const index =
|
|
294
|
+
const { date, periods } = this.dayAt(local + k);
|
|
295
|
+
const index = periods.findIndex(([s, e]) => s <= time && time < e);
|
|
240
296
|
if (index !== -1) return { date, index, inside: true };
|
|
241
297
|
}
|
|
242
298
|
for (let k = 5; k >= -7; k--) {
|
|
243
|
-
const date =
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
periods.forEach(([s], i) => {
|
|
247
|
-
if (s <= time) index = i;
|
|
248
|
-
});
|
|
299
|
+
const { date, periods } = this.dayAt(local + k);
|
|
300
|
+
let index = periods.length - 1;
|
|
301
|
+
while (index >= 0 && !(periods[index]![0] <= time)) index--;
|
|
249
302
|
if (index !== -1) return { date, index, inside: false };
|
|
250
303
|
}
|
|
251
304
|
return null;
|
|
252
305
|
}
|
|
253
306
|
|
|
307
|
+
private dayAt(day: number): { date: DateNum; periods: Array<[number, number]> } {
|
|
308
|
+
let at = this.days.get(day);
|
|
309
|
+
if (!at) {
|
|
310
|
+
const date = dateOf(day);
|
|
311
|
+
at = { date, periods: this.periods(date) };
|
|
312
|
+
this.days.set(day, at);
|
|
313
|
+
}
|
|
314
|
+
return at;
|
|
315
|
+
}
|
|
316
|
+
|
|
254
317
|
/** First trading day in [from, to] (dates), or null. */
|
|
255
318
|
firstTradingDay(from: DateNum, to: DateNum): DateNum | null {
|
|
256
319
|
for (let date = from; date <= to; date = addDays(date, 1)) if (this.isTradingDay(date)) return date;
|
package/src/session/context.ts
CHANGED
|
@@ -68,6 +68,26 @@ export interface SessionPeriods {
|
|
|
68
68
|
close: (time: number) => number;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** Calendars of the `session` / `timezone` arguments before the cache restarts */
|
|
72
|
+
const MAX_ARGUMENT_CALENDARS = 64;
|
|
73
|
+
const argumentCalendars = new Map<string, TradingCalendar>();
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Calendar of a session string read in a time zone, without corrections and holidays: it depends on these only, so
|
|
77
|
+
* the calls with other timeframes or other chart contexts share it and its periods (a script often reads
|
|
78
|
+
* `time(timeframe.period, s)` and `time("D", s)` on every bar).
|
|
79
|
+
*/
|
|
80
|
+
function argumentCalendar(timezone: string, session: string, rule: 'symbol' | 'pine', dayPlacement: boolean): TradingCalendar {
|
|
81
|
+
const key = `${rule}|${dayPlacement}|${timezone}|${session}`;
|
|
82
|
+
let calendar = argumentCalendars.get(key);
|
|
83
|
+
if (!calendar) {
|
|
84
|
+
calendar = new TradingCalendar(timezone, { session }, rule, dayPlacement);
|
|
85
|
+
if (argumentCalendars.size >= MAX_ARGUMENT_CALENDARS) argumentCalendars.clear();
|
|
86
|
+
argumentCalendars.set(key, calendar);
|
|
87
|
+
}
|
|
88
|
+
return calendar;
|
|
89
|
+
}
|
|
90
|
+
|
|
71
91
|
/**
|
|
72
92
|
* Calendar for the `session` / `timezone` arguments of `time(tf, session, timezone)`. Without both: the loaded
|
|
73
93
|
* session. As in PineScript, an explicit time zone reads the symbol session hours in that zone without corrections
|
|
@@ -87,11 +107,13 @@ function calendarFor(
|
|
|
87
107
|
if (session !== undefined && parsePineSession(session) === null) {
|
|
88
108
|
const name = session.trim();
|
|
89
109
|
const symbol = name === 'regular' && chart.regularSession ? chart.regularSession : chart.session!;
|
|
90
|
-
return { bars: new SessionBars(
|
|
110
|
+
return { bars: new SessionBars(argumentCalendar(tz, toSpec(symbol).session, 'symbol', false), bars.chart.period), test: true };
|
|
91
111
|
}
|
|
92
|
-
const spec = session ? { session } : { session: toSpec(chart.session!).session };
|
|
93
112
|
return {
|
|
94
|
-
bars: new SessionBars(
|
|
113
|
+
bars: new SessionBars(
|
|
114
|
+
argumentCalendar(tz, session || toSpec(chart.session!).session, session ? 'pine' : 'symbol', dayPlacement),
|
|
115
|
+
bars.chart.period
|
|
116
|
+
),
|
|
95
117
|
test: true,
|
|
96
118
|
// a PineScript session string: the rules of time.inSession (runs ending at 0000, days around a clock change)
|
|
97
119
|
inside: session ? (time) => sessionRun(time, session, tz, in_seconds(bars.chart.period) * 1000) !== null : undefined,
|
|
@@ -138,15 +160,20 @@ export function sessionPeriods(
|
|
|
138
160
|
return { open, close };
|
|
139
161
|
}
|
|
140
162
|
|
|
141
|
-
|
|
163
|
+
/** Periods per chart, timeframe, session and time zone (no session / time zone is ""): maps of the given strings, no
|
|
164
|
+
* key to build on every bar */
|
|
165
|
+
const periodCache = new WeakMap<object, Map<string, Map<string, Map<string, SessionPeriods>>>>();
|
|
142
166
|
|
|
143
167
|
function periodsOfChart(chart: SessionContext, timeframe: string, session?: string, timezone?: string): { s: ChartSessions; p: SessionPeriods } {
|
|
144
168
|
const s = chartSessions(chart);
|
|
145
|
-
let
|
|
146
|
-
if (!
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
169
|
+
let byTimeframe = periodCache.get(chart);
|
|
170
|
+
if (!byTimeframe) periodCache.set(chart, (byTimeframe = new Map()));
|
|
171
|
+
let bySession = byTimeframe.get(timeframe);
|
|
172
|
+
if (!bySession) byTimeframe.set(timeframe, (bySession = new Map()));
|
|
173
|
+
let byZone = bySession.get(session ?? '');
|
|
174
|
+
if (!byZone) bySession.set(session ?? '', (byZone = new Map()));
|
|
175
|
+
let p = byZone.get(timezone ?? '');
|
|
176
|
+
if (!p) byZone.set(timezone ?? '', (p = sessionPeriods(s.bars, chart, timeframe, session, timezone)));
|
|
150
177
|
return { s, p };
|
|
151
178
|
}
|
|
152
179
|
|
package/src/time/timezone.ts
CHANGED
|
@@ -91,17 +91,39 @@ function resolveZone(timezone: string): Zone {
|
|
|
91
91
|
return zone;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
/**
|
|
95
|
-
|
|
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 {
|
|
96
101
|
let offset = zone.dayStarts.get(day);
|
|
97
102
|
if (offset === undefined) {
|
|
98
103
|
offset = formattedOffset(zone.format, day * MS_PER_DAY);
|
|
99
|
-
if (zone.dayStarts.size >= MAX_CACHED_DAYS) zone.dayStarts.clear();
|
|
100
104
|
zone.dayStarts.set(day, offset);
|
|
101
105
|
}
|
|
102
106
|
return offset;
|
|
103
107
|
}
|
|
104
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
|
+
|
|
105
127
|
/**
|
|
106
128
|
* Offset of the zone from UTC at instant `time`, in milliseconds (local = UTC + offset).
|
|
107
129
|
*
|
|
@@ -157,17 +179,48 @@ export function zoneOffset(time: number, timezone: string): number {
|
|
|
157
179
|
return offsetAt(resolveZone(timezone), time);
|
|
158
180
|
}
|
|
159
181
|
|
|
160
|
-
/**
|
|
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
|
+
*/
|
|
161
206
|
export function zonedFields(time: number, timezone: string): ZonedFields {
|
|
162
|
-
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);
|
|
163
215
|
return {
|
|
164
|
-
year
|
|
165
|
-
month
|
|
166
|
-
day
|
|
167
|
-
hour:
|
|
168
|
-
minute:
|
|
169
|
-
second:
|
|
170
|
-
|
|
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,
|
|
171
224
|
};
|
|
172
225
|
}
|
|
173
226
|
|
|
@@ -185,6 +238,9 @@ export function zonedFields(time: number, timezone: string): ZonedFields {
|
|
|
185
238
|
*/
|
|
186
239
|
export function civilToUnix(year: number, month: number, day: number, hour = 0, minute = 0, second = 0): number {
|
|
187
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;
|
|
188
244
|
// normalized year / month / day of the Gregorian reading decides the calendar
|
|
189
245
|
const d = new Date(Date.UTC(year, month - 1, 1));
|
|
190
246
|
const y = d.getUTCFullYear();
|
|
@@ -222,8 +278,13 @@ export function zonedToUnix(
|
|
|
222
278
|
// one day before and one day after are the two candidates.
|
|
223
279
|
const before = offsetAt(zone, local - MS_PER_DAY);
|
|
224
280
|
const after = offsetAt(zone, local + MS_PER_DAY);
|
|
225
|
-
|
|
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;
|
|
226
287
|
// a wall time that occurs twice: the later instant, or the earlier one with `earlier`
|
|
227
|
-
if (
|
|
228
|
-
return
|
|
288
|
+
if (beforeFits && afterFits) return earlier ? Math.min(withBefore, withAfter) : Math.max(withBefore, withAfter);
|
|
289
|
+
return afterFits ? withAfter : withBefore;
|
|
229
290
|
}
|