bizdays-toolkit 0.32.5 → 0.32.7
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/CHANGELOG.md +18 -1
- package/dist/core/calendar.js +22 -14
- package/dist/core/diagnose.js +134 -54
- package/dist/core/input.js +3 -1
- package/dist/core/schedule.js +104 -32
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,21 @@ Entries from 0.31.0 are in English. Earlier entries remain in Japanese (their **
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.32.7] - 2026-11-21
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- The option diagnostics of a schedule are faster when a business-day rule counts on a calendar with a holiday provider and the scan reaches years outside the provider's supported years (for example, a schedule without a provider counting on the Japanese holiday calendar). A month in which that calendar throws is now evaluated once per rule in a diagnosis, instead of every time the scan looks at it. On the maintainer's machine, such a schedule is diagnosed in about 40% of the time of 0.32.6. Results, exceptions and diagnostics are unchanged
|
|
14
|
+
- Judging days and the option diagnostics are faster: a date that was just converted to a day number (or built from one) is not parsed again, and the rules for the nth business day of a week, the days before and after holidays (`dayBeforeHoliday`, `dayAfterHoliday`) and holiday shifts (`holidayOverlapPolicy: "after-holiday"`) step through day numbers instead of building date strings. On the maintainer's machine, calling `isBusinessDay` or `valueOn` for the days of 2020–2029 in random order takes about 85–100% of the time of 0.32.6 depending on the settings (about 85–90% for a calendar with a holiday-shift rule and for `valueOn`; for a store calendar without such rules the difference was sometimes not measurable; measured alternately with 0.32.6 in one process), and, together with the change above, diagnosing a schedule counted on the Japanese holiday calendar takes about 30% of the time of 0.32.6. Results, exceptions and diagnostics are unchanged
|
|
15
|
+
- Creating a calendar or a schedule, and `toDateString`, are faster when the time zone is a name the runtime returned (for example the time zone of a holiday provider): the name is no longer normalized again on every call. On the maintainer's machine, `createCalendar({ holidayProvider: jp, closedWeekdays: [0] })` takes about 80–85% of the time of 0.32.6. Results are unchanged
|
|
16
|
+
- Judging days of a calendar or a schedule with many individual dates (`closedDates`, `openDates`, `dates`) is faster when no two of them overlap: the dates are looked up by binary search in start order, instead of being checked one by one for every day. Dates already written in start order are used as they are; otherwise they are sorted once the number of judgments reaches about twice the base-2 logarithm of the number of dates (for example, 18 judgments for 500 dates), so creating a calendar and judging only a few days does not pay for sorting. When some dates overlap, they are still checked in the order written, so the entry written first still decides. On the maintainer's machine (measured alternately with 0.32.6 in one process), `diffBusinessDays` over one year for a calendar with 500 closed dates takes about 30% of the time of 0.32.6, diagnosing a schedule counted on a calendar with 300 closed dates about 35%, and creating a calendar with 100–2,000 closed dates in mixed order and judging one day about the same time (100–106%). When the dates are not written in start order, creating a calendar and judging a few dozen days, around the point where the dates get sorted, can take up to about 20% longer than 0.32.6 (for example, about 25 days for 2,000 dates, or about 8 days for 10 dates); judging fewer days takes about the same time, and judging more days is faster. Results, exceptions and diagnostics are unchanged
|
|
17
|
+
|
|
18
|
+
## [0.32.6] - 2026-11-20
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
- The option diagnostics could report as `redundant` a rule or date whose removal changed the results, breaking the promise that the reported rules can be removed all together. This happened for an item that takes effect only outside the effective period of another rule (after its `effectiveTo` or before its `effectiveFrom`), for an item that takes effect only outside the supported years of a holiday provider used with `outOfRangePolicy: "ignore"`, and, in a schedule without a holiday provider (or with `"ignore"`), for an item whose days depend on how the counting calendar (`calendar`) of an nth-business-day rule changes outside the years checked: effective periods, specific dates, `interval` or `cycle` rules and holiday providers inside that calendar, including combined calendars and calendars made from schedules (a schedule counted on a calendar with Japan's holidays is one such setup), and days on which the counting calendar throws outside its supported years. The diagnostics now also check one cycle of the rules on each side of each such boundary, so the years in the messages can be wider (for example `checked 1963-2028`). When they cannot read the counting calendar (an object shaped like a calendar but not made by `createCalendar`, `combineCalendars` or `createCalendarFromSchedule`) or the range would be too long, they no longer report the annual dates and recurring rules of those options as `redundant`, or the rules counted on that calendar as `never-matches`. For the same reason, `no-business-day` is no longer reported for options that have business days after an effective period ends. A date range in `closedDates`, `openDates` or a schedule's `dates` that is longer than 200 years is no longer reported as `redundant`, because the diagnostics do not check all of its days, and options in which such a range closes every remaining day (for example up to 9999-12-31) no longer get `no-business-day`. The results of calendars and schedules do not change. Most options get fewer items; in some, another rule is now reported as `redundant` in place of the incorrect item (it can be removed together with the other reported rules). If you removed rules because an earlier version reported them as `redundant`, check that the results you rely on have not changed
|
|
23
|
+
|
|
9
24
|
## [0.32.5] - 2026-11-18
|
|
10
25
|
|
|
11
26
|
### Changed
|
|
@@ -1038,7 +1053,9 @@ This version rebuilds the `redundant` diagnostic so that the reported rules can
|
|
|
1038
1053
|
|
|
1039
1054
|
- 日本: 2026 年 2 月の官報(2027 年の春分・秋分の日)まで反映(`dataVersion: "2026-02"`、`confirmedThrough: 2027`)
|
|
1040
1055
|
|
|
1041
|
-
[Unreleased]: https://github.com/inohan77/bizdays-toolkit/compare/v0.32.
|
|
1056
|
+
[Unreleased]: https://github.com/inohan77/bizdays-toolkit/compare/v0.32.7...HEAD
|
|
1057
|
+
[0.32.7]: https://github.com/inohan77/bizdays-toolkit/compare/v0.32.6...v0.32.7
|
|
1058
|
+
[0.32.6]: https://github.com/inohan77/bizdays-toolkit/compare/v0.32.5...v0.32.6
|
|
1042
1059
|
[0.32.5]: https://github.com/inohan77/bizdays-toolkit/compare/v0.32.4...v0.32.5
|
|
1043
1060
|
[0.32.4]: https://github.com/inohan77/bizdays-toolkit/compare/v0.32.3...v0.32.4
|
|
1044
1061
|
[0.32.3]: https://github.com/inohan77/bizdays-toolkit/compare/v0.32.2...v0.32.3
|
package/dist/core/calendar.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { addDays, lengthOfMonth, formatDate
|
|
1
|
+
import { addDays, lengthOfMonth, formatDate } from "./date.js";
|
|
2
2
|
import { checkUnknownKeys, internalError, IssueCollector, describeValue, rangeError, withoutUndefined, } from "./errors.js";
|
|
3
3
|
import { isOptionsObject } from "./options.js";
|
|
4
4
|
import { renameKeys, writtenKey } from "./renamed.js";
|
|
5
5
|
import { distinctTimeZones, parseTimeOfDaySeconds, toCivil } from "./input.js";
|
|
6
6
|
import { checkClosingDay, closingDayIn, closingMonth as calendarClosingMonth, dayOfMonth, formatMonth, isDayOfMonthValue, parseMonth, readsAsDate, shiftMonth, } from "./month.js";
|
|
7
|
-
import { buildSchedule, compileAnnualRules, compileDateRules, compileHolidaySource, compileRecurringRules, createHolidayLookup, infoOfSchedule, valuesOfSchedule, isWeekday, mapDays, RECURRING_RULE_KEYS, jpUsage, } from "./schedule.js";
|
|
7
|
+
import { buildSchedule, compileAnnualRules, compileDateRules, compileHolidaySource, compileRecurringRules, createHolidayLookup, infoOfSchedule, PARTS, partsHolder, partsOf, valuesOfSchedule, isWeekday, mapDays, RECURRING_RULE_KEYS, jpUsage, dateOfDayNumber, dayNumberOf, } from "./schedule.js";
|
|
8
8
|
export const CALENDAR_OPTION_KEYS = [
|
|
9
9
|
"holidayProvider",
|
|
10
10
|
"timeZone",
|
|
@@ -128,9 +128,9 @@ function dayInfoFromScheduleDay(day, open, fromSchedule) {
|
|
|
128
128
|
function buildCalendar(lookup, isOpen, info, searchDayLimit) {
|
|
129
129
|
const dateOf = lookup.dateOf;
|
|
130
130
|
function search(start, step, open = true) {
|
|
131
|
-
let n =
|
|
131
|
+
let n = dayNumberOf(start);
|
|
132
132
|
for (let i = 0; i < searchDayLimit; i++, n += step) {
|
|
133
|
-
const date =
|
|
133
|
+
const date = dateOfDayNumber(n);
|
|
134
134
|
if (isOpen(date) === open)
|
|
135
135
|
return date;
|
|
136
136
|
}
|
|
@@ -144,7 +144,7 @@ function buildCalendar(lookup, isOpen, info, searchDayLimit) {
|
|
|
144
144
|
function countOpen(from, toExclusive) {
|
|
145
145
|
let count = 0;
|
|
146
146
|
for (let n = from; n < toExclusive; n++)
|
|
147
|
-
if (isOpen(
|
|
147
|
+
if (isOpen(dateOfDayNumber(n)))
|
|
148
148
|
count++;
|
|
149
149
|
return count;
|
|
150
150
|
}
|
|
@@ -189,13 +189,13 @@ function buildCalendar(lookup, isOpen, info, searchDayLimit) {
|
|
|
189
189
|
}
|
|
190
190
|
function searchInMonth(start, step, limited) {
|
|
191
191
|
const { year, month } = parseMonth(start.slice(0, 7));
|
|
192
|
-
const first =
|
|
192
|
+
const first = dayNumberOf(formatDate(year, month, 1));
|
|
193
193
|
const last = first + lengthOfMonth(year, month) - 1;
|
|
194
|
-
let n =
|
|
194
|
+
let n = dayNumberOf(start);
|
|
195
195
|
for (let i = 0; n >= first && n <= last; i++, n += step) {
|
|
196
196
|
if (limited && i >= searchDayLimit)
|
|
197
197
|
throw noBusinessDay(start);
|
|
198
|
-
const date =
|
|
198
|
+
const date = dateOfDayNumber(n);
|
|
199
199
|
if (isOpen(date))
|
|
200
200
|
return date;
|
|
201
201
|
}
|
|
@@ -222,7 +222,9 @@ function buildCalendar(lookup, isOpen, info, searchDayLimit) {
|
|
|
222
222
|
}
|
|
223
223
|
return found;
|
|
224
224
|
}
|
|
225
|
+
const parts = partsHolder(partsOf(lookup));
|
|
225
226
|
const calendar = {
|
|
227
|
+
[PARTS]: parts,
|
|
226
228
|
timeZone: lookup.timeZone,
|
|
227
229
|
providers: lookup.providers,
|
|
228
230
|
outOfRangePolicy: lookup.outOfRangePolicy,
|
|
@@ -274,8 +276,8 @@ function buildCalendar(lookup, isOpen, info, searchDayLimit) {
|
|
|
274
276
|
return date;
|
|
275
277
|
},
|
|
276
278
|
diffBusinessDays(from, to) {
|
|
277
|
-
const a =
|
|
278
|
-
const b =
|
|
279
|
+
const a = dayNumberOf(dateOf(from));
|
|
280
|
+
const b = dayNumberOf(dateOf(to));
|
|
279
281
|
return a <= b ? countOpen(a, b) : 0 - countOpen(b, a);
|
|
280
282
|
},
|
|
281
283
|
businessDatesBetween(from, to) {
|
|
@@ -284,8 +286,8 @@ function buildCalendar(lookup, isOpen, info, searchDayLimit) {
|
|
|
284
286
|
if (a > b)
|
|
285
287
|
throw rangeError("invalid-range", `"from" must not be after "to" (${a} > ${b})`);
|
|
286
288
|
const result = [];
|
|
287
|
-
for (let n =
|
|
288
|
-
const date =
|
|
289
|
+
for (let n = dayNumberOf(a); n <= dayNumberOf(b); n++) {
|
|
290
|
+
const date = dateOfDayNumber(n);
|
|
289
291
|
if (isOpen(date))
|
|
290
292
|
result.push(date);
|
|
291
293
|
}
|
|
@@ -324,6 +326,8 @@ function buildCalendar(lookup, isOpen, info, searchDayLimit) {
|
|
|
324
326
|
businessDaysBetween: (from, to) => calendar.diffBusinessDays(from, to),
|
|
325
327
|
listBusinessDays: (from, to) => calendar.businessDatesBetween(from, to),
|
|
326
328
|
};
|
|
329
|
+
parts.owner = calendar;
|
|
330
|
+
Object.freeze(parts);
|
|
327
331
|
return Object.freeze(calendar);
|
|
328
332
|
}
|
|
329
333
|
function optionsObject(options, what, rename = (v) => v, required = false, code = "invalid-argument") {
|
|
@@ -381,15 +385,17 @@ export function combineCalendars(calendars, options) {
|
|
|
381
385
|
});
|
|
382
386
|
const merge = (lists) => {
|
|
383
387
|
const byDate = new Map();
|
|
384
|
-
for (const
|
|
385
|
-
for (const h of
|
|
388
|
+
for (const holidays of lists)
|
|
389
|
+
for (const h of holidays)
|
|
386
390
|
if (!byDate.has(h.date))
|
|
387
391
|
byDate.set(h.date, h);
|
|
388
392
|
return Object.freeze([...byDate.values()].sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : 0)));
|
|
389
393
|
};
|
|
390
394
|
const holidayOn = (input) => list.map((c) => c.holidayOn(input)).find((h) => h !== undefined);
|
|
395
|
+
const parts = partsHolder(list);
|
|
391
396
|
const lookup = {
|
|
392
397
|
...base,
|
|
398
|
+
[PARTS]: parts,
|
|
393
399
|
isHoliday: (input) => holidayOn(input) !== undefined,
|
|
394
400
|
holidayOn,
|
|
395
401
|
getHoliday: holidayOn,
|
|
@@ -425,6 +431,8 @@ export function combineCalendars(calendars, options) {
|
|
|
425
431
|
result.decidedByPath = Object.freeze([decidedBy, ...(decided.decidedByPath ?? [])]);
|
|
426
432
|
return Object.freeze(result);
|
|
427
433
|
}
|
|
434
|
+
parts.owner = lookup;
|
|
435
|
+
Object.freeze(parts);
|
|
428
436
|
return buildCalendar(lookup, isOpen, info, searchDayLimit);
|
|
429
437
|
}
|
|
430
438
|
export function createCalendarFromSchedule(schedule, isOpen, options) {
|
package/dist/core/diagnose.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { formatDate, lengthOfMonth,
|
|
1
|
+
import { formatDate, lengthOfMonth, isValidDate, parseDate, weekdayOf, } from "./date.js";
|
|
2
2
|
import { compileCalendarOptions } from "./calendar.js";
|
|
3
3
|
import { internalError, InternalError, describeValue, IssueCollector, rangeError, } from "./errors.js";
|
|
4
4
|
import { isOptionsObject } from "./options.js";
|
|
5
5
|
import { RENAMED_KEYS, RENAMED_VALUES } from "./renamed.js";
|
|
6
|
-
import { compileScheduleOptions, createBasis, createHolidayLookup, createResolver, lostShiftMessage, throwLostShift, inPeriod, MAX_HOLIDAY_RUN, jpUsage, matchMonthDay, matchPattern, isHolidayBasedRule, resultIgnoresHolidays, } from "./schedule.js";
|
|
6
|
+
import { compileScheduleOptions, createBasis, createHolidayLookup, createResolver, lostShiftMessage, throwLostShift, inPeriod, MAX_HOLIDAY_RUN, jpUsage, matchMonthDay, matchPattern, partsOf, isHolidayBasedRule, resultIgnoresHolidays, dateOfDayNumber, dayNumberOf, } from "./schedule.js";
|
|
7
7
|
function editDistance(a, b) {
|
|
8
8
|
const d = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
9
9
|
for (let i = 1; i <= a.length; i++) {
|
|
@@ -45,10 +45,10 @@ function closestOf(key, known) {
|
|
|
45
45
|
}
|
|
46
46
|
return best;
|
|
47
47
|
}
|
|
48
|
-
const PATTERN_FROM =
|
|
49
|
-
const PATTERN_DAYS =
|
|
50
|
-
const MIN_DAY =
|
|
51
|
-
const MAX_DAY =
|
|
48
|
+
const PATTERN_FROM = dayNumberOf("2001-01-01");
|
|
49
|
+
const PATTERN_DAYS = dayNumberOf("2028-12-31") - PATTERN_FROM + 1;
|
|
50
|
+
const MIN_DAY = dayNumberOf("1000-01-01");
|
|
51
|
+
const MAX_DAY = dayNumberOf("9999-12-31");
|
|
52
52
|
const MAX_WINDOW_YEARS = 200;
|
|
53
53
|
const MAX_DATE_RULE_DAYS = 366 * MAX_WINDOW_YEARS;
|
|
54
54
|
const isLostShift = (error) => error?.code === "holiday-run-too-long";
|
|
@@ -64,17 +64,18 @@ const atEdge = (n, error, window) => {
|
|
|
64
64
|
const last = window.ranges[window.ranges.length - 1].to;
|
|
65
65
|
return n < first + PROVIDER_EDGE_DAYS || n > last - PROVIDER_EDGE_DAYS;
|
|
66
66
|
};
|
|
67
|
-
function
|
|
67
|
+
function crossesShortCentury(from, to) {
|
|
68
68
|
const lo = Math.max(from, MIN_DAY);
|
|
69
|
-
const hi = Math.min(
|
|
69
|
+
const hi = Math.min(to, MAX_DAY);
|
|
70
70
|
if (lo > hi)
|
|
71
|
-
return
|
|
71
|
+
return false;
|
|
72
72
|
const last = yearOf(hi);
|
|
73
73
|
for (let y = Math.ceil(yearOf(lo) / 100) * 100; y <= last; y += 100)
|
|
74
74
|
if (y % 400 !== 0)
|
|
75
|
-
return
|
|
76
|
-
return
|
|
75
|
+
return true;
|
|
76
|
+
return false;
|
|
77
77
|
}
|
|
78
|
+
const openEndedDays = (from, days) => crossesShortCentury(from, from + days - 1) ? Math.max(days, 2 * PATTERN_DAYS) : days;
|
|
78
79
|
function periodSpan(rule, cycle) {
|
|
79
80
|
const { effectiveFrom, effectiveTo } = rule;
|
|
80
81
|
if (effectiveFrom === null) {
|
|
@@ -99,6 +100,7 @@ function patternCycle(rules) {
|
|
|
99
100
|
}
|
|
100
101
|
return Math.min(lcm(PATTERN_DAYS, days), max);
|
|
101
102
|
}
|
|
103
|
+
const MAX_SCAN_DAYS = 366 * MAX_WINDOW_YEARS * 3;
|
|
102
104
|
function clipYears(from, to) {
|
|
103
105
|
if (to - from + 1 <= MAX_WINDOW_YEARS)
|
|
104
106
|
return [from, to];
|
|
@@ -106,9 +108,35 @@ function clipYears(from, to) {
|
|
|
106
108
|
const b = Math.min(to, 2099);
|
|
107
109
|
return a <= b ? [a, b] : [from, from + MAX_WINDOW_YEARS - 1];
|
|
108
110
|
}
|
|
109
|
-
const dayOfYear = (year, last) =>
|
|
110
|
-
function
|
|
111
|
+
const dayOfYear = (year, last) => dayNumberOf(`${String(year).padStart(4, "0")}-${last ? "12-31" : "01-01"}`);
|
|
112
|
+
function collectBasis(calendar, inside) {
|
|
113
|
+
const parts = partsOf(calendar);
|
|
114
|
+
if (parts === undefined)
|
|
115
|
+
return false;
|
|
116
|
+
if (Array.isArray(parts))
|
|
117
|
+
return parts.every((c) => collectBasis(c, inside));
|
|
118
|
+
const { source, annual, recurring, dates } = parts;
|
|
119
|
+
for (const { supportedYears } of source.providers) {
|
|
120
|
+
const [from, to] = clipYears(supportedYears.from, supportedYears.to);
|
|
121
|
+
inside.spans.push({ from: dayOfYear(from, false), to: dayOfYear(to, true) });
|
|
122
|
+
inside.edges.push(dayOfYear(supportedYears.from, false), dayOfYear(supportedYears.to, true) + 1);
|
|
123
|
+
}
|
|
124
|
+
for (const r of [...annual, ...recurring]) {
|
|
125
|
+
if (r.effectiveFrom !== null)
|
|
126
|
+
inside.edges.push(r.effectiveFrom);
|
|
127
|
+
if (r.effectiveTo !== null)
|
|
128
|
+
inside.edges.push(r.effectiveTo + 1);
|
|
129
|
+
}
|
|
130
|
+
for (const r of dates)
|
|
131
|
+
inside.edges.push(r.from, r.to + 1);
|
|
132
|
+
inside.rules.push(...recurring);
|
|
133
|
+
return recurring.every((r) => r.calendar === null || collectBasis(r.calendar, inside));
|
|
134
|
+
}
|
|
135
|
+
function scanWindow(source, rules, cycle, inside) {
|
|
111
136
|
const ranges = [];
|
|
137
|
+
const side = (day, before) => before
|
|
138
|
+
? { from: day - openEndedDays(day - cycle + 1, cycle) + 1, to: day }
|
|
139
|
+
: { from: day, to: day + openEndedDays(day, cycle) - 1 };
|
|
112
140
|
let extend = true;
|
|
113
141
|
if (source.providers.length === 0) {
|
|
114
142
|
ranges.push({ from: PATTERN_FROM, to: PATTERN_FROM + cycle - 1 });
|
|
@@ -128,6 +156,8 @@ function scanWindow(source, rules, cycle) {
|
|
|
128
156
|
}
|
|
129
157
|
if (from > to)
|
|
130
158
|
return undefined;
|
|
159
|
+
if (extend)
|
|
160
|
+
ranges.push(side(dayOfYear(from, false) - 1, true), side(dayOfYear(to, true) + 1, false));
|
|
131
161
|
[from, to] = clipYears(from, to);
|
|
132
162
|
ranges.push({ from: dayOfYear(from, false), to: dayOfYear(to, true) });
|
|
133
163
|
}
|
|
@@ -136,10 +166,26 @@ function scanWindow(source, rules, cycle) {
|
|
|
136
166
|
if (rule.effectiveFrom === null && rule.effectiveTo === null)
|
|
137
167
|
continue;
|
|
138
168
|
const span = periodSpan(rule, cycle);
|
|
139
|
-
|
|
140
|
-
|
|
169
|
+
ranges.push({
|
|
170
|
+
from: span.from,
|
|
171
|
+
to: Math.min(span.to, span.from + 366 * MAX_WINDOW_YEARS - 1),
|
|
172
|
+
});
|
|
173
|
+
if (rule.effectiveTo !== null)
|
|
174
|
+
ranges.push(side(rule.effectiveTo + 1, false));
|
|
175
|
+
if (rule.effectiveFrom !== null)
|
|
176
|
+
ranges.push(side(rule.effectiveFrom - 1, true));
|
|
177
|
+
}
|
|
178
|
+
if (inside) {
|
|
179
|
+
for (const e of inside.edges)
|
|
180
|
+
ranges.push(side(e - 1, true), side(e, false));
|
|
181
|
+
ranges.push(...inside.spans);
|
|
141
182
|
}
|
|
142
183
|
}
|
|
184
|
+
for (let i = 0; i < ranges.length; i++)
|
|
185
|
+
ranges[i] = { from: Math.max(ranges[i].from, MIN_DAY), to: Math.min(ranges[i].to, MAX_DAY) };
|
|
186
|
+
for (let i = ranges.length - 1; i >= 0; i--)
|
|
187
|
+
if (ranges[i].from > ranges[i].to)
|
|
188
|
+
ranges.splice(i, 1);
|
|
143
189
|
ranges.sort((a, b) => a.from - b.from);
|
|
144
190
|
const merged = [];
|
|
145
191
|
for (const r of ranges) {
|
|
@@ -149,12 +195,30 @@ function scanWindow(source, rules, cycle) {
|
|
|
149
195
|
else
|
|
150
196
|
merged.push(r);
|
|
151
197
|
}
|
|
152
|
-
const yearOf = (n) => parseDate(
|
|
198
|
+
const yearOf = (n) => parseDate(dateOfDayNumber(n)).year;
|
|
153
199
|
const label = merged.map((r) => `${yearOf(r.from)}-${yearOf(r.to)}`).join(", ");
|
|
154
|
-
return { ranges: merged, label };
|
|
200
|
+
return { ranges: merged, label, blind: false };
|
|
155
201
|
}
|
|
156
|
-
export function rulesScanWindow(compiled
|
|
157
|
-
|
|
202
|
+
export function rulesScanWindow(compiled) {
|
|
203
|
+
const source = compiled.source;
|
|
204
|
+
const rules = [...compiled.annual, ...compiled.recurring];
|
|
205
|
+
const inside = { edges: [], spans: [], rules: [] };
|
|
206
|
+
const known = compiled.recurring.every((r) => r.calendar === null || collectBasis(r.calendar, inside));
|
|
207
|
+
const own = patternCycle(compiled.recurring);
|
|
208
|
+
const cycle = patternCycle([...compiled.recurring, ...inside.rules]);
|
|
209
|
+
const window = known ? scanWindow(source, rules, cycle, inside) : scanWindow(source, rules, own);
|
|
210
|
+
if (!window)
|
|
211
|
+
return window;
|
|
212
|
+
const max = 366 * MAX_WINDOW_YEARS;
|
|
213
|
+
const days = window.ranges.reduce((sum, r) => sum + r.to - r.from + 1, 0);
|
|
214
|
+
const blind = !known ||
|
|
215
|
+
(cycle >= max && own < max) ||
|
|
216
|
+
days > MAX_SCAN_DAYS ||
|
|
217
|
+
(inside.rules.some((r) => r.interval !== null || r.cycle !== null) &&
|
|
218
|
+
window.ranges.some((r) => crossesShortCentury(r.from, r.to)));
|
|
219
|
+
if (!blind)
|
|
220
|
+
return window;
|
|
221
|
+
return { ...(known ? scanWindow(source, rules, own) : window), blind };
|
|
158
222
|
}
|
|
159
223
|
function eachDay(window, fn) {
|
|
160
224
|
for (const { from, to } of window.ranges) {
|
|
@@ -167,7 +231,7 @@ function patternBits(rule) {
|
|
|
167
231
|
const bits = new Uint8Array(PATTERN_DAYS);
|
|
168
232
|
const weekly = rule.interval === null ? rule : { ...rule, interval: null, anchor: null };
|
|
169
233
|
for (let i = 0; i < PATTERN_DAYS; i++) {
|
|
170
|
-
if (matchPattern(weekly,
|
|
234
|
+
if (matchPattern(weekly, dateOfDayNumber(PATTERN_FROM + i)))
|
|
171
235
|
bits[i] = 1;
|
|
172
236
|
}
|
|
173
237
|
return bits;
|
|
@@ -180,7 +244,7 @@ function matchesAnyDay(rule, cycle) {
|
|
|
180
244
|
? Math.min(span.to, span.from + openEndedDays(span.from, cycle) - 1)
|
|
181
245
|
: span.to;
|
|
182
246
|
for (let n = Math.max(span.from, MIN_DAY); n <= Math.min(to, MAX_DAY); n++)
|
|
183
|
-
if (matchPattern(rule,
|
|
247
|
+
if (matchPattern(rule, dateOfDayNumber(n)))
|
|
184
248
|
return true;
|
|
185
249
|
return false;
|
|
186
250
|
}
|
|
@@ -274,7 +338,7 @@ function sameValueIn(a, b, seen) {
|
|
|
274
338
|
ka.every((k) => Object.hasOwn(b, k) &&
|
|
275
339
|
sameValueIn(a[k], b[k], seen)));
|
|
276
340
|
}
|
|
277
|
-
const yearOf = (dayNumber) => parseDate(
|
|
341
|
+
const yearOf = (dayNumber) => parseDate(dateOfDayNumber(dayNumber)).year;
|
|
278
342
|
function without(compiled, rule, dropDates) {
|
|
279
343
|
return {
|
|
280
344
|
...compiled,
|
|
@@ -317,16 +381,21 @@ function guardBases(compiled, unknownMonths) {
|
|
|
317
381
|
const basis = r.basis;
|
|
318
382
|
if (basis === null || r.calendar === null)
|
|
319
383
|
return r;
|
|
384
|
+
const thrown = new Set();
|
|
320
385
|
return {
|
|
321
386
|
...r,
|
|
322
387
|
basis: (year, month) => {
|
|
388
|
+
const m = year * 12 + month - 1;
|
|
389
|
+
if (thrown.has(m))
|
|
390
|
+
return [];
|
|
323
391
|
try {
|
|
324
392
|
return basis(year, month);
|
|
325
393
|
}
|
|
326
394
|
catch (error) {
|
|
327
395
|
if (!UNJUDGEABLE.has(String(error.code)))
|
|
328
396
|
throw error;
|
|
329
|
-
unknownMonths.add(
|
|
397
|
+
unknownMonths.add(m);
|
|
398
|
+
thrown.add(m);
|
|
330
399
|
return [];
|
|
331
400
|
}
|
|
332
401
|
},
|
|
@@ -342,8 +411,12 @@ export function unjudgeableDays(compiled, window) {
|
|
|
342
411
|
if (!window || bases.length === 0)
|
|
343
412
|
return { days, months };
|
|
344
413
|
const monthOf = (day) => {
|
|
345
|
-
|
|
346
|
-
|
|
414
|
+
if (day < MIN_DAY || day > MAX_DAY) {
|
|
415
|
+
const { year, month } = parseDate(dateOfDayNumber(day));
|
|
416
|
+
return year * 12 + month - 1;
|
|
417
|
+
}
|
|
418
|
+
const d = new Date(day * 86_400_000);
|
|
419
|
+
return d.getUTCFullYear() * 12 + d.getUTCMonth();
|
|
347
420
|
};
|
|
348
421
|
for (const r of bases) {
|
|
349
422
|
const thrown = new Map();
|
|
@@ -370,12 +443,14 @@ export function unjudgeableDays(compiled, window) {
|
|
|
370
443
|
if (from > to)
|
|
371
444
|
continue;
|
|
372
445
|
if (r.weekStart === null) {
|
|
373
|
-
for (let n = from; n <= to; n++)
|
|
374
|
-
|
|
446
|
+
for (let n = from; n <= to; n++) {
|
|
447
|
+
const m = monthOf(n);
|
|
448
|
+
if (inMonths(m) && throws(m))
|
|
375
449
|
days.add(n);
|
|
450
|
+
}
|
|
376
451
|
continue;
|
|
377
452
|
}
|
|
378
|
-
const start = from - ((weekdayOf(
|
|
453
|
+
const start = from - ((weekdayOf(dateOfDayNumber(from)) - r.weekStart + 7) % 7);
|
|
379
454
|
for (let w = start; w <= to; w += 7) {
|
|
380
455
|
const all = [0, 1, 2, 3, 4, 5, 6].map((i) => w + i);
|
|
381
456
|
const week = all.filter((n) => n >= MIN_DAY && n <= MAX_DAY);
|
|
@@ -395,8 +470,8 @@ export function unjudgeableDays(compiled, window) {
|
|
|
395
470
|
}
|
|
396
471
|
return { days, months };
|
|
397
472
|
}
|
|
398
|
-
export function unsureOf(compiled, unknown) {
|
|
399
|
-
if (unknown.days.size === 0)
|
|
473
|
+
export function unsureOf(compiled, unknown, blind = false) {
|
|
474
|
+
if (unknown.days.size === 0 && !blind)
|
|
400
475
|
return () => false;
|
|
401
476
|
const sorted = [...unknown.days].sort((a, b) => a - b);
|
|
402
477
|
const overlaps = (start, end) => {
|
|
@@ -432,6 +507,8 @@ export function unsureOf(compiled, unknown) {
|
|
|
432
507
|
return false;
|
|
433
508
|
if (isDate(item))
|
|
434
509
|
return overlaps(item.from, item.to);
|
|
510
|
+
if (blind)
|
|
511
|
+
return true;
|
|
435
512
|
return overlaps(item.effectiveFrom, item.effectiveTo);
|
|
436
513
|
};
|
|
437
514
|
}
|
|
@@ -450,7 +527,7 @@ function checkLostShifts(c, compiled, lookup, window) {
|
|
|
450
527
|
const days = [];
|
|
451
528
|
for (let y = yearOf(from); y <= yearOf(to); y++)
|
|
452
529
|
for (const h of lookup.holidaysInYear(y))
|
|
453
|
-
days.push(
|
|
530
|
+
days.push(dayNumberOf(h.date));
|
|
454
531
|
for (let i = 0; i < days.length; i++) {
|
|
455
532
|
const back = MAX_HOLIDAY_RUN - 1;
|
|
456
533
|
if (!skipping && days[i - back] !== days[i] - back)
|
|
@@ -458,7 +535,7 @@ function checkLostShifts(c, compiled, lookup, window) {
|
|
|
458
535
|
const last = Math.min(days[i] + 7, (days[i + 1] ?? Infinity) - 1, to, MAX_DAY);
|
|
459
536
|
for (let n = days[i] + 1; n <= last; n++) {
|
|
460
537
|
try {
|
|
461
|
-
resolve(
|
|
538
|
+
resolve(dateOfDayNumber(n), false);
|
|
462
539
|
}
|
|
463
540
|
catch (error) {
|
|
464
541
|
if (!atEdge(n, error, window))
|
|
@@ -479,10 +556,11 @@ export function dropUnsure(c, from, unsure, recorded, unknown) {
|
|
|
479
556
|
throw internalError(`a diagnostic about an unjudgeable month was left: ${left.code} ${left.path}`);
|
|
480
557
|
}
|
|
481
558
|
function analyze(c, original, isCalendar) {
|
|
482
|
-
const
|
|
559
|
+
const window = rulesScanWindow(original);
|
|
560
|
+
const unknown = unjudgeableDays(original, window);
|
|
483
561
|
const recorded = new Set();
|
|
484
562
|
const compiled = guardBases(original, recorded);
|
|
485
|
-
const unsure = unsureOf(original, unknown);
|
|
563
|
+
const unsure = unsureOf(original, unknown, window?.blind);
|
|
486
564
|
const from = c.issues.length;
|
|
487
565
|
analyzeRules(c, compiled, isCalendar, unsure);
|
|
488
566
|
dropUnsure(c, from, unsure, recorded, unknown);
|
|
@@ -497,7 +575,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
497
575
|
});
|
|
498
576
|
const basis = createBasis(compiled, lookup);
|
|
499
577
|
const cycle = patternCycle(recurring);
|
|
500
|
-
const window = rulesScanWindow(compiled
|
|
578
|
+
const window = rulesScanWindow(compiled);
|
|
501
579
|
const unused = new Set();
|
|
502
580
|
checkLostShifts(c, compiled, lookup, window);
|
|
503
581
|
dates.forEach((rule, j) => {
|
|
@@ -564,7 +642,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
564
642
|
if (!inPeriod(rule, n))
|
|
565
643
|
return false;
|
|
566
644
|
try {
|
|
567
|
-
found = matchPattern(rule,
|
|
645
|
+
found = matchPattern(rule, dateOfDayNumber(n), holidayOfSafe, undefined, lookup.fromJp);
|
|
568
646
|
}
|
|
569
647
|
catch (error) {
|
|
570
648
|
if (!atCalendarEdge(n, error))
|
|
@@ -583,7 +661,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
583
661
|
if (!inPeriod(rule, n))
|
|
584
662
|
return false;
|
|
585
663
|
try {
|
|
586
|
-
found = matchPattern(rule,
|
|
664
|
+
found = matchPattern(rule, dateOfDayNumber(n), holidayOfSafe, basis, lookup.fromJp);
|
|
587
665
|
}
|
|
588
666
|
catch {
|
|
589
667
|
found = false;
|
|
@@ -601,7 +679,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
601
679
|
if (nationalAsRegular === undefined) {
|
|
602
680
|
let found = false;
|
|
603
681
|
if (window && jpUsage(source) === "only") {
|
|
604
|
-
eachDay(window, (n) => (found = holidayOfSafe(
|
|
682
|
+
eachDay(window, (n) => (found = holidayOfSafe(dateOfDayNumber(n))?.kind === renamedKind.from));
|
|
605
683
|
nationalAsRegular = !found;
|
|
606
684
|
}
|
|
607
685
|
else
|
|
@@ -716,7 +794,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
716
794
|
let found = false;
|
|
717
795
|
eachDay(window, (n) => {
|
|
718
796
|
try {
|
|
719
|
-
found = resolve(
|
|
797
|
+
found = resolve(dateOfDayNumber(n)).day.value === true;
|
|
720
798
|
}
|
|
721
799
|
catch (error) {
|
|
722
800
|
if (!atEdge(n, error, window) && !isLostShift(error))
|
|
@@ -740,12 +818,12 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
740
818
|
for (const n of days) {
|
|
741
819
|
out.add(n);
|
|
742
820
|
if (countsMonth) {
|
|
743
|
-
const { year, month, day } = parseDate(
|
|
821
|
+
const { year, month, day } = parseDate(dateOfDayNumber(n));
|
|
744
822
|
for (let k = 0; k < lengthOfMonth(year, month); k++)
|
|
745
823
|
out.add(n - day + 1 + k);
|
|
746
824
|
}
|
|
747
825
|
for (const start of weekStarts) {
|
|
748
|
-
const first = n - ((weekdayOf(
|
|
826
|
+
const first = n - ((weekdayOf(dateOfDayNumber(n)) - start + 7) % 7);
|
|
749
827
|
for (let k = 0; k < 7; k++)
|
|
750
828
|
out.add(first + k);
|
|
751
829
|
}
|
|
@@ -759,7 +837,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
759
837
|
if (!values.has(n)) {
|
|
760
838
|
let value;
|
|
761
839
|
try {
|
|
762
|
-
value = resolve(
|
|
840
|
+
value = resolve(dateOfDayNumber(n)).day.value;
|
|
763
841
|
}
|
|
764
842
|
catch {
|
|
765
843
|
value = FAILED;
|
|
@@ -774,7 +852,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
774
852
|
for (const n of days) {
|
|
775
853
|
let value;
|
|
776
854
|
try {
|
|
777
|
-
value = resolve(
|
|
855
|
+
value = resolve(dateOfDayNumber(n)).day.value;
|
|
778
856
|
}
|
|
779
857
|
catch {
|
|
780
858
|
value = FAILED;
|
|
@@ -803,12 +881,14 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
803
881
|
const decided = [];
|
|
804
882
|
const span = [];
|
|
805
883
|
let checked = 0;
|
|
806
|
-
|
|
884
|
+
if (rule.to - rule.from + 1 > MAX_DATE_RULE_DAYS)
|
|
885
|
+
return false;
|
|
886
|
+
const last = rule.to;
|
|
807
887
|
for (let n = rule.from; n <= last; n++) {
|
|
808
888
|
span.push(n);
|
|
809
889
|
let decidedBy;
|
|
810
890
|
try {
|
|
811
|
-
decidedBy = resolve(
|
|
891
|
+
decidedBy = resolve(dateOfDayNumber(n)).rule;
|
|
812
892
|
}
|
|
813
893
|
catch {
|
|
814
894
|
continue;
|
|
@@ -844,7 +924,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
844
924
|
eachDay(window, (n) => {
|
|
845
925
|
let by;
|
|
846
926
|
try {
|
|
847
|
-
by = resolveNow(
|
|
927
|
+
by = resolveNow(dateOfDayNumber(n)).rule;
|
|
848
928
|
}
|
|
849
929
|
catch {
|
|
850
930
|
unjudged.add(n);
|
|
@@ -866,7 +946,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
866
946
|
continue;
|
|
867
947
|
for (const days of decided.values())
|
|
868
948
|
days.delete(n);
|
|
869
|
-
const by = resolveNow(
|
|
949
|
+
const by = resolveNow(dateOfDayNumber(n)).rule;
|
|
870
950
|
if (by)
|
|
871
951
|
decided.get(by)?.add(n);
|
|
872
952
|
}
|
|
@@ -880,7 +960,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
880
960
|
const matched = [];
|
|
881
961
|
if (window)
|
|
882
962
|
eachDay(window, (n) => {
|
|
883
|
-
if (matchMonthDay(rule,
|
|
963
|
+
if (matchMonthDay(rule, dateOfDayNumber(n).slice(5)))
|
|
884
964
|
matched.push(n);
|
|
885
965
|
});
|
|
886
966
|
const units = countingUnits(matched);
|
|
@@ -939,7 +1019,7 @@ function analyzeRules(c, compiled, isCalendar, unsure = () => false) {
|
|
|
939
1019
|
const paths = new Set();
|
|
940
1020
|
for (const n of days) {
|
|
941
1021
|
try {
|
|
942
|
-
paths.add(resolveFinal(
|
|
1022
|
+
paths.add(resolveFinal(dateOfDayNumber(n)).rule?.path ?? null);
|
|
943
1023
|
}
|
|
944
1024
|
catch {
|
|
945
1025
|
paths.add(null);
|
|
@@ -966,13 +1046,13 @@ function compileDiagnoseOptions(options) {
|
|
|
966
1046
|
if (!isValidDate(asOf)) {
|
|
967
1047
|
throw rangeError("invalid-date", `asOf must be a date string "YYYY-MM-DD": ${describeValue(asOf)}`);
|
|
968
1048
|
}
|
|
969
|
-
return { asOf:
|
|
1049
|
+
return { asOf: dayNumberOf(asOf) };
|
|
970
1050
|
}
|
|
971
1051
|
function checkExpired(c, compiled, asOf) {
|
|
972
1052
|
const ended = (path, last, what, reach = last, why = "") => {
|
|
973
1053
|
if (reach < asOf) {
|
|
974
|
-
const until = reach > last ? ` (it could affect results until ${
|
|
975
|
-
c.info("expired", path, `This ${what} ended on ${
|
|
1054
|
+
const until = reach > last ? ` (it could affect results until ${dateOfDayNumber(reach)}: ${why})` : "";
|
|
1055
|
+
c.info("expired", path, `This ${what} ended on ${dateOfDayNumber(last)}${until}, before ${dateOfDayNumber(asOf)}; it no longer affects results and can be removed if the past need not be reproduced`);
|
|
976
1056
|
}
|
|
977
1057
|
};
|
|
978
1058
|
const counting = compiled.recurring.filter((r) => r.businessDays !== null && r.basis === null);
|
|
@@ -981,8 +1061,8 @@ function checkExpired(c, compiled, asOf) {
|
|
|
981
1061
|
const countedReach = (last) => {
|
|
982
1062
|
let reach = last;
|
|
983
1063
|
if (byMonth) {
|
|
984
|
-
const { year, month } = parseDate(
|
|
985
|
-
reach =
|
|
1064
|
+
const { year, month } = parseDate(dateOfDayNumber(last));
|
|
1065
|
+
reach = dayNumberOf(formatDate(year, month, lengthOfMonth(year, month)));
|
|
986
1066
|
}
|
|
987
1067
|
return byWeek ? Math.max(reach, last + 6) : reach;
|
|
988
1068
|
};
|
package/dist/core/input.js
CHANGED
|
@@ -21,6 +21,8 @@ function formatterFor(timeZone) {
|
|
|
21
21
|
return f;
|
|
22
22
|
}
|
|
23
23
|
function zoneNameOf(timeZone) {
|
|
24
|
+
if (zoneNames.get(timeZone) === timeZone)
|
|
25
|
+
return timeZone;
|
|
24
26
|
const key = timeZone.replace(/[A-Z]+/g, (s) => s.toLowerCase());
|
|
25
27
|
let name = zoneNames.get(key);
|
|
26
28
|
if (name === undefined) {
|
|
@@ -64,7 +66,7 @@ export function distinctTimeZones(zones) {
|
|
|
64
66
|
return out;
|
|
65
67
|
}
|
|
66
68
|
function wallParts(epochMilliseconds, timeZone) {
|
|
67
|
-
const zone =
|
|
69
|
+
const zone = zoneNameOf(timeZone);
|
|
68
70
|
const offset = OFFSET_ZONE_RE.exec(zone);
|
|
69
71
|
const parts = formatterFor(offset ? "UTC" : zone).formatToParts(new Date(epochMilliseconds +
|
|
70
72
|
(offset
|
package/dist/core/schedule.js
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { lengthOfMonth, formatDate, fromDayNumber, isValidDate, parseDate, toDayNumber, } from "./date.js";
|
|
2
2
|
import { checkUnknownKeys, describeValue, IssueCollector, rangeError, withoutUndefined, } from "./errors.js";
|
|
3
3
|
import { isOptionsObject } from "./options.js";
|
|
4
4
|
import { checkNames } from "./names.js";
|
|
5
5
|
import { RENAMED_VALUES, renameKeys, renameValue, writtenKey } from "./renamed.js";
|
|
6
6
|
import { distinctTimeZones, instantToCivil, isDate, sameTimeZone, toCivil, toDateTimeParts, toInstant, validateTimeZone, } from "./input.js";
|
|
7
7
|
import { HOLIDAY_KEY_PATTERN } from "./types.js";
|
|
8
|
+
let memoDate = "1970-01-01";
|
|
9
|
+
let memoDay = 0;
|
|
10
|
+
export function dayNumberOf(date) {
|
|
11
|
+
if (date !== memoDate) {
|
|
12
|
+
memoDay = toDayNumber(date);
|
|
13
|
+
memoDate = date;
|
|
14
|
+
}
|
|
15
|
+
return memoDay;
|
|
16
|
+
}
|
|
17
|
+
export function dateOfDayNumber(dayNumber) {
|
|
18
|
+
const date = fromDayNumber(dayNumber);
|
|
19
|
+
memoDate = date;
|
|
20
|
+
memoDay = Math.floor(dayNumber);
|
|
21
|
+
return date;
|
|
22
|
+
}
|
|
23
|
+
const weekdayOfDayNumber = (dayNumber) => ((((dayNumber + 4) % 7) + 7) % 7);
|
|
24
|
+
const weekdayOfDate = (date) => weekdayOfDayNumber(dayNumberOf(date));
|
|
8
25
|
const MONTH_DAY_RE = /^(\d{2})-(\d{2})$/;
|
|
9
26
|
const MAX_DAYS_IN_MONTH = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
10
27
|
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -56,7 +73,7 @@ function checkDate(c, path, value) {
|
|
|
56
73
|
c.error("invalid-date", path, `Invalid date ${describeValue(value)} (expected YYYY-MM-DD)`);
|
|
57
74
|
return undefined;
|
|
58
75
|
}
|
|
59
|
-
return
|
|
76
|
+
return dayNumberOf(value);
|
|
60
77
|
}
|
|
61
78
|
function checkMonthDay(c, path, value) {
|
|
62
79
|
if (!isValidMonthDay(value)) {
|
|
@@ -179,7 +196,7 @@ export function basisFromPredicate(isOpen) {
|
|
|
179
196
|
const list = [];
|
|
180
197
|
const dim = lengthOfMonth(year, month);
|
|
181
198
|
for (let d = 1; d <= dim; d++)
|
|
182
|
-
if (isOpen(formatDate(year, month, d)))
|
|
199
|
+
if (isOpen(formatDate(year, month, d, true)))
|
|
183
200
|
list.push(d);
|
|
184
201
|
days = Object.freeze(list);
|
|
185
202
|
if (cache.size >= 240)
|
|
@@ -296,8 +313,8 @@ export function compileRecurringRules(c, path, entries, valueOf, keys, context,
|
|
|
296
313
|
const a = checkDate(c, `${p}.anchor`, entry["anchor"]);
|
|
297
314
|
if (a === undefined)
|
|
298
315
|
ok = false;
|
|
299
|
-
else if (weekday !== null &&
|
|
300
|
-
c.error("invalid-option", `${p}.anchor`, `must be on the same weekday as "weekday" (${String(entry["anchor"])} is weekday ${
|
|
316
|
+
else if (weekday !== null && weekdayOfDate(entry["anchor"]) !== weekday) {
|
|
317
|
+
c.error("invalid-option", `${p}.anchor`, `must be on the same weekday as "weekday" (${String(entry["anchor"])} is weekday ${weekdayOfDate(entry["anchor"])})`);
|
|
301
318
|
ok = false;
|
|
302
319
|
}
|
|
303
320
|
else
|
|
@@ -581,15 +598,15 @@ export function matchPattern(rule, date, holidayOf, basis, fromJp) {
|
|
|
581
598
|
if (b === undefined)
|
|
582
599
|
return false;
|
|
583
600
|
if (rule.weekStart !== null) {
|
|
584
|
-
const
|
|
601
|
+
const self = dayNumberOf(date);
|
|
602
|
+
const start = self - ((weekdayOfDayNumber(self) - rule.weekStart + 7) % 7);
|
|
585
603
|
const week = [];
|
|
586
|
-
for (let
|
|
587
|
-
const
|
|
588
|
-
const at = parseDate(d);
|
|
604
|
+
for (let n = start; n < start + 7; n++) {
|
|
605
|
+
const at = parseDate(dateOfDayNumber(n));
|
|
589
606
|
if (b(at.year, at.month).includes(at.day))
|
|
590
|
-
week.push(
|
|
607
|
+
week.push(n);
|
|
591
608
|
}
|
|
592
|
-
const at = week.indexOf(
|
|
609
|
+
const at = week.indexOf(self);
|
|
593
610
|
if (at < 0)
|
|
594
611
|
return false;
|
|
595
612
|
return rule.businessDays.has(at + 1) || rule.businessDays.has(at - week.length);
|
|
@@ -603,7 +620,7 @@ export function matchPattern(rule, date, holidayOf, basis, fromJp) {
|
|
|
603
620
|
if (rule.holiday || rule.dayAfterHoliday || rule.dayBeforeHoliday) {
|
|
604
621
|
if (holidayOf === undefined)
|
|
605
622
|
return false;
|
|
606
|
-
if (rule.weekdays && !rule.weekdays.has(
|
|
623
|
+
if (rule.weekdays && !rule.weekdays.has(weekdayOfDate(date)))
|
|
607
624
|
return false;
|
|
608
625
|
const selected = (h) => (rule.kinds === null ||
|
|
609
626
|
rule.kinds.has(h.kind) ||
|
|
@@ -614,29 +631,29 @@ export function matchPattern(rule, date, holidayOf, basis, fromJp) {
|
|
|
614
631
|
return h !== undefined && selected(h);
|
|
615
632
|
}
|
|
616
633
|
const skip = rule.skipWeekdays;
|
|
617
|
-
if (holidayOf(date) !== undefined || skip?.has(
|
|
634
|
+
if (holidayOf(date) !== undefined || skip?.has(weekdayOfDate(date)))
|
|
618
635
|
return false;
|
|
619
636
|
const step = rule.dayAfterHoliday ? -1 : 1;
|
|
637
|
+
const self = dayNumberOf(date);
|
|
620
638
|
for (let k = 1; k <= MAX_HOLIDAY_RUN; k++) {
|
|
621
|
-
const
|
|
622
|
-
const h = holidayOf(next);
|
|
639
|
+
const h = holidayOf(dateOfDayNumber(self + step * k));
|
|
623
640
|
if (h !== undefined)
|
|
624
641
|
return selected(h);
|
|
625
|
-
if (!skip?.has(
|
|
642
|
+
if (!skip?.has(weekdayOfDayNumber(self + step * k)))
|
|
626
643
|
return false;
|
|
627
644
|
}
|
|
628
645
|
return false;
|
|
629
646
|
}
|
|
630
647
|
if (rule.cycle !== null) {
|
|
631
|
-
const offset = (
|
|
648
|
+
const offset = (dayNumberOf(date) - rule.anchor) % rule.cycle;
|
|
632
649
|
return rule.positions.has(((offset + rule.cycle) % rule.cycle) + 1);
|
|
633
650
|
}
|
|
634
651
|
const dim = lengthOfMonth(year, month);
|
|
635
652
|
if (rule.weekday !== null) {
|
|
636
|
-
if (
|
|
653
|
+
if (weekdayOfDate(date) !== rule.weekday)
|
|
637
654
|
return false;
|
|
638
655
|
if (rule.interval !== null)
|
|
639
|
-
return (
|
|
656
|
+
return (dayNumberOf(date) - rule.anchor) % (7 * rule.interval) === 0;
|
|
640
657
|
if (rule.nth === null)
|
|
641
658
|
return true;
|
|
642
659
|
return rule.nth.has(Math.ceil(day / 7)) || (rule.nth.has(-1) && day + 7 > dim);
|
|
@@ -644,7 +661,7 @@ export function matchPattern(rule, date, holidayOf, basis, fromJp) {
|
|
|
644
661
|
return rule.days.has(day) || (rule.days.has(-1) && day === dim);
|
|
645
662
|
}
|
|
646
663
|
export function matchRecurring(rule, date, holidayOf, basis, fromJp) {
|
|
647
|
-
return inPeriod(rule,
|
|
664
|
+
return inPeriod(rule, dayNumberOf(date)) && matchPattern(rule, date, holidayOf, basis, fromJp);
|
|
648
665
|
}
|
|
649
666
|
export function createBasis(compiled, lookup) {
|
|
650
667
|
if (!compiled.recurring.some((r) => r.businessDays !== null && r.basis === null))
|
|
@@ -1016,7 +1033,7 @@ export function matchMonthDay(rule, monthDay) {
|
|
|
1016
1033
|
: monthDay >= rule.from || monthDay <= rule.to;
|
|
1017
1034
|
}
|
|
1018
1035
|
export function matchAnnual(rule, date) {
|
|
1019
|
-
return inPeriod(rule,
|
|
1036
|
+
return inPeriod(rule, dayNumberOf(date)) && matchMonthDay(rule, date.slice(5));
|
|
1020
1037
|
}
|
|
1021
1038
|
export const isHolidayBasedRule = (rule) => rule.holiday === true || rule.dayAfterHoliday === true || rule.dayBeforeHoliday === true;
|
|
1022
1039
|
export function resultIgnoresHolidays(compiled) {
|
|
@@ -1028,26 +1045,68 @@ export function resultIgnoresHolidays(compiled) {
|
|
|
1028
1045
|
export function createResolver(compiled, lookup, onLostShift) {
|
|
1029
1046
|
const { weekly, holiday: holidayRule, annual, dates, recurring } = compiled;
|
|
1030
1047
|
const hasShift = recurring.some((r) => r.holidayOverlapPolicy === "after-holiday");
|
|
1048
|
+
let sorted;
|
|
1049
|
+
let lookups = 0;
|
|
1050
|
+
const dateRuleOf = (n) => {
|
|
1051
|
+
if (sorted === undefined) {
|
|
1052
|
+
const count = dates.length;
|
|
1053
|
+
if (lookups++ === 0) {
|
|
1054
|
+
let ordered = 1;
|
|
1055
|
+
while (ordered < count && dates[ordered].from > dates[ordered - 1].to)
|
|
1056
|
+
ordered++;
|
|
1057
|
+
if (ordered >= count)
|
|
1058
|
+
sorted = dates;
|
|
1059
|
+
}
|
|
1060
|
+
else if (lookups > 2 * Math.log2(count + 1)) {
|
|
1061
|
+
const keys = new Float64Array(count);
|
|
1062
|
+
for (let i = 0; i < count; i++)
|
|
1063
|
+
keys[i] = (dates[i].from - MIN_DAY_NUMBER) * count + i;
|
|
1064
|
+
keys.sort();
|
|
1065
|
+
const s = [];
|
|
1066
|
+
for (let i = 0; i < count; i++) {
|
|
1067
|
+
const r = dates[keys[i] % count];
|
|
1068
|
+
if (i > 0 && r.from <= s[i - 1].to)
|
|
1069
|
+
break;
|
|
1070
|
+
s.push(r);
|
|
1071
|
+
}
|
|
1072
|
+
sorted = s.length === count ? s : null;
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
if (!sorted)
|
|
1076
|
+
return dates.find((r) => n >= r.from && n <= r.to);
|
|
1077
|
+
let lo = 0;
|
|
1078
|
+
let hi = sorted.length;
|
|
1079
|
+
while (lo < hi) {
|
|
1080
|
+
const mid = (lo + hi) >> 1;
|
|
1081
|
+
if (sorted[mid].from <= n)
|
|
1082
|
+
lo = mid + 1;
|
|
1083
|
+
else
|
|
1084
|
+
hi = mid;
|
|
1085
|
+
}
|
|
1086
|
+
const r = lo > 0 ? sorted[lo - 1] : undefined;
|
|
1087
|
+
return r !== undefined && n <= r.to ? r : undefined;
|
|
1088
|
+
};
|
|
1031
1089
|
const holidayUnused = resultIgnoresHolidays(compiled);
|
|
1032
1090
|
const basis = createBasis(compiled, lookup);
|
|
1033
1091
|
const holidayOfNeighbour = (date) => lookup.holidayOfDate(date);
|
|
1034
1092
|
const isHolidayOfNeighbour = (date) => holidayOfNeighbour(date) !== undefined;
|
|
1035
1093
|
function shiftedOrigin(rule, date) {
|
|
1094
|
+
const self = dayNumberOf(date);
|
|
1036
1095
|
for (let k = 1; k <= MAX_HOLIDAY_RUN; k++) {
|
|
1037
|
-
const prev =
|
|
1038
|
-
if (!isHolidayOfNeighbour(prev) && !rule.skipWeekdays?.has(
|
|
1096
|
+
const prev = dateOfDayNumber(self - k);
|
|
1097
|
+
if (!isHolidayOfNeighbour(prev) && !rule.skipWeekdays?.has(weekdayOfDayNumber(self - k)))
|
|
1039
1098
|
return undefined;
|
|
1040
1099
|
if (matchRecurring(rule, prev, holidayOfNeighbour, undefined, lookup.fromJp))
|
|
1041
1100
|
return prev;
|
|
1042
1101
|
}
|
|
1043
|
-
const first =
|
|
1044
|
-
for (let n =
|
|
1102
|
+
const first = self - MAX_HOLIDAY_RUN_SCAN;
|
|
1103
|
+
for (let n = self - MAX_HOLIDAY_RUN - 1; n >= MIN_DAY_NUMBER; n--) {
|
|
1045
1104
|
if (n < first) {
|
|
1046
1105
|
onLostShift(rule, date, undefined);
|
|
1047
1106
|
return undefined;
|
|
1048
1107
|
}
|
|
1049
|
-
const prev =
|
|
1050
|
-
if (!isHolidayOfNeighbour(prev) && !rule.skipWeekdays?.has(
|
|
1108
|
+
const prev = dateOfDayNumber(n);
|
|
1109
|
+
if (!isHolidayOfNeighbour(prev) && !rule.skipWeekdays?.has(weekdayOfDayNumber(n)))
|
|
1051
1110
|
return undefined;
|
|
1052
1111
|
if (matchRecurring(rule, prev, holidayOfNeighbour, undefined, lookup.fromJp)) {
|
|
1053
1112
|
onLostShift(rule, date, prev);
|
|
@@ -1057,8 +1116,8 @@ export function createResolver(compiled, lookup, onLostShift) {
|
|
|
1057
1116
|
return undefined;
|
|
1058
1117
|
}
|
|
1059
1118
|
return function resolve(date, withHoliday = true) {
|
|
1060
|
-
const dayNumber =
|
|
1061
|
-
const weekday =
|
|
1119
|
+
const dayNumber = dayNumberOf(date);
|
|
1120
|
+
const weekday = weekdayOfDate(date);
|
|
1062
1121
|
const holiday = lookup.holidayOfDate(date, holidayUnused && !withHoliday);
|
|
1063
1122
|
const make = (value, reason, rule, name, shiftedFrom) => {
|
|
1064
1123
|
const result = {
|
|
@@ -1077,7 +1136,7 @@ export function createResolver(compiled, lookup, onLostShift) {
|
|
|
1077
1136
|
result.optionPath = result.rulePath = rule.path;
|
|
1078
1137
|
return { day: Object.freeze(result), rule };
|
|
1079
1138
|
};
|
|
1080
|
-
const byDate =
|
|
1139
|
+
const byDate = dateRuleOf(dayNumber);
|
|
1081
1140
|
if (byDate)
|
|
1082
1141
|
return make(byDate.value, "date", byDate, byDate.name);
|
|
1083
1142
|
const byAnnual = annual.find((r) => matchAnnual(r, date));
|
|
@@ -1117,11 +1176,20 @@ export function mapDays(from, to, fn) {
|
|
|
1117
1176
|
throw rangeError("invalid-range", `"from" must not be after "to" (${from} > ${to})`);
|
|
1118
1177
|
}
|
|
1119
1178
|
const result = [];
|
|
1120
|
-
for (let n =
|
|
1121
|
-
result.push(fn(
|
|
1179
|
+
for (let n = dayNumberOf(from); n <= dayNumberOf(to); n++)
|
|
1180
|
+
result.push(fn(dateOfDayNumber(n)));
|
|
1122
1181
|
return Object.freeze(result);
|
|
1123
1182
|
}
|
|
1124
1183
|
const INTERNAL_INFO = Symbol("bizdays-toolkit.scheduleInfo");
|
|
1184
|
+
export const PARTS = Symbol("bizdays-toolkit.parts");
|
|
1185
|
+
export const partsHolder = (value) => ({
|
|
1186
|
+
owner: undefined,
|
|
1187
|
+
value,
|
|
1188
|
+
});
|
|
1189
|
+
export function partsOf(object) {
|
|
1190
|
+
const holder = object[PARTS];
|
|
1191
|
+
return holder?.owner === object ? holder.value : undefined;
|
|
1192
|
+
}
|
|
1125
1193
|
const INTERNAL_VALUES = Symbol("bizdays-toolkit.scheduleValues");
|
|
1126
1194
|
export function valuesOfSchedule(schedule) {
|
|
1127
1195
|
return schedule[INTERNAL_VALUES] ?? [];
|
|
@@ -1136,9 +1204,11 @@ export function buildSchedule(compiled) {
|
|
|
1136
1204
|
const values = Object.freeze(typeof compiled.holiday === "object" && "value" in compiled.holiday
|
|
1137
1205
|
? [...compiled.weekly, compiled.holiday.value]
|
|
1138
1206
|
: [...compiled.weekly]);
|
|
1207
|
+
const parts = partsHolder(compiled);
|
|
1139
1208
|
const schedule = {
|
|
1140
1209
|
[INTERNAL_INFO]: info,
|
|
1141
1210
|
[INTERNAL_VALUES]: values,
|
|
1211
|
+
[PARTS]: parts,
|
|
1142
1212
|
timeZone: lookup.timeZone,
|
|
1143
1213
|
providers: lookup.providers,
|
|
1144
1214
|
outOfRangePolicy: lookup.outOfRangePolicy,
|
|
@@ -1159,6 +1229,8 @@ export function buildSchedule(compiled) {
|
|
|
1159
1229
|
dayInfo: (input) => info(lookup.dateOf(input)),
|
|
1160
1230
|
daysBetween: (from, to) => mapDays(lookup.dateOf(from), lookup.dateOf(to), info),
|
|
1161
1231
|
};
|
|
1232
|
+
parts.owner = schedule;
|
|
1233
|
+
Object.freeze(parts);
|
|
1162
1234
|
return Object.freeze(schedule);
|
|
1163
1235
|
}
|
|
1164
1236
|
export function createSchedule(options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bizdays-toolkit",
|
|
3
|
-
"version": "0.32.
|
|
3
|
+
"version": "0.32.7",
|
|
4
4
|
"description": "Holidays, business-day calendar and day-by-day schedules (e.g. store hours) with time zone support. Japanese holidays verified against the Cabinet Office data; UK bank holidays (England, Wales, Scotland, Northern Ireland) verified against GOV.UK and The Gazette; French public holidays (metropole, Alsace-Moselle) verified against data.gouv.fr; Singapore public holidays verified against data.gov.sg. / 祝日と営業日のカレンダー(タイムゾーン対応・日本の祝日は内閣府、英国の bank holidays は GOV.UK と The Gazette、フランスの祝日は data.gouv.fr、シンガポールの祝日は data.gov.sg のデータで全日照合)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bank-holidays",
|