@first-ch/tools-mcp 0.28.0 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/cidr.mjs +826 -0
- package/date-calc.mjs +734 -0
- package/date-holidays.json +957 -0
- package/package.json +25 -3
- package/server.mjs +85 -0
package/date-calc.mjs
ADDED
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
/* 日数・営業日計算(date_calc)
|
|
2
|
+
* 計算コア(dcToDays / dcCountRange / dcAddBusinessDays / dcRun ほか)は
|
|
3
|
+
* FirstCHTools の site/date-calc/app.js と同一
|
|
4
|
+
* (2箇所ルール: site側が正本・片方を直したらもう片方も同じ内容で反映する)。
|
|
5
|
+
* 両ファイルからコアの開始/終了コメントに挟まれた範囲を sed で切り出し
|
|
6
|
+
* (site側は字下げ2文字を落とす・こちらは行頭の export を落とす)、
|
|
7
|
+
* diff が空になることで同期を機械的に確認できる。
|
|
8
|
+
*
|
|
9
|
+
* 日付は Date を使わず「1970-01-01 からの通算日」の整数で扱うので、
|
|
10
|
+
* サーバーのタイムゾーン設定に結果が左右されない。
|
|
11
|
+
* 祝日は date-holidays.json(内閣府「国民の祝日について」由来)を読む。
|
|
12
|
+
* 完全ローカル処理・ネットワーク送信なし。 */
|
|
13
|
+
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
|
|
17
|
+
/* ==================== ここから計算コア(site / MCP で同一) ==================== */
|
|
18
|
+
|
|
19
|
+
export class DateCalcError extends Error {
|
|
20
|
+
constructor(code, info) {
|
|
21
|
+
super(code);
|
|
22
|
+
this.name = 'DateCalcError';
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.info = info || {};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 週の休みのプリセット(0=日曜 … 6=土曜) */
|
|
29
|
+
export const DC_WEEKEND = {
|
|
30
|
+
'sat-sun': [0, 6],
|
|
31
|
+
sun: [0],
|
|
32
|
+
'fri-sat': [5, 6],
|
|
33
|
+
none: [],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** 扱える年の範囲。暦(グレゴリオ暦の先行適用)が意味を持つ範囲に限る */
|
|
37
|
+
export const DC_MIN_YEAR = 1583;
|
|
38
|
+
export const DC_MAX_YEAR = 9999;
|
|
39
|
+
/** 1回の計算で走査する日数の上限(暴走を止めるための安全弁) */
|
|
40
|
+
export const DC_MAX_SPAN = 200000;
|
|
41
|
+
/** 営業日を探して進める日数の上限(全部が休みの設定だと見つからないため) */
|
|
42
|
+
export const DC_MAX_SEEK = 20000;
|
|
43
|
+
|
|
44
|
+
export function dcIsLeap(y) {
|
|
45
|
+
return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const DC_MDAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
49
|
+
|
|
50
|
+
export function dcDaysInMonth(y, m) {
|
|
51
|
+
return m === 2 && dcIsLeap(y) ? 29 : DC_MDAYS[m - 1];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 年月日 → 1970-01-01 からの通算日(Howard Hinnant の days_from_civil)。Date を経由しない */
|
|
55
|
+
export function dcToDays(y, m, d) {
|
|
56
|
+
const yy = m <= 2 ? y - 1 : y;
|
|
57
|
+
const era = Math.floor(yy / 400);
|
|
58
|
+
const yoe = yy - era * 400; // 0..399
|
|
59
|
+
const doy = Math.floor((153 * (m + (m > 2 ? -3 : 9)) + 2) / 5) + d - 1; // 0..365
|
|
60
|
+
const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy; // 0..146096
|
|
61
|
+
return era * 146097 + doe - 719468;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 通算日 → 年月日(civil_from_days) */
|
|
65
|
+
export function dcFromDays(z) {
|
|
66
|
+
const zz = z + 719468;
|
|
67
|
+
const era = Math.floor(zz / 146097);
|
|
68
|
+
const doe = zz - era * 146097; // 0..146096
|
|
69
|
+
const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);
|
|
70
|
+
const y = yoe + era * 400;
|
|
71
|
+
const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));
|
|
72
|
+
const mp = Math.floor((5 * doy + 2) / 153); // 0..11(3月始まり)
|
|
73
|
+
const d = doy - Math.floor((153 * mp + 2) / 5) + 1;
|
|
74
|
+
const m = mp + (mp < 10 ? 3 : -9);
|
|
75
|
+
return { y: m <= 2 ? y + 1 : y, m, d };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 曜日(0=日曜 … 6=土曜)。1970-01-01 は木曜 */
|
|
79
|
+
export function dcWeekday(z) {
|
|
80
|
+
return ((z % 7) + 11) % 7;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function dcPad(n, w) {
|
|
84
|
+
let s = String(Math.abs(n));
|
|
85
|
+
while (s.length < w) s = '0' + s;
|
|
86
|
+
return (n < 0 ? '-' : '') + s;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function dcIso(y, m, d) {
|
|
90
|
+
return dcPad(y, 4) + '-' + dcPad(m, 2) + '-' + dcPad(d, 2);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function dcIsoFromDays(z) {
|
|
94
|
+
const c = dcFromDays(z);
|
|
95
|
+
return dcIso(c.y, c.m, c.d);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 文字列 → { y, m, d, days }。
|
|
100
|
+
* `2026-09-20` `2026/9/20` `2026.9.20` `20260920` `2026年9月20日` を受け取る。
|
|
101
|
+
*/
|
|
102
|
+
export function dcParseDate(input, field) {
|
|
103
|
+
const s = String(input == null ? '' : input).trim();
|
|
104
|
+
if (s === '') throw new DateCalcError('EMPTY_DATE', { field });
|
|
105
|
+
let m = /^(\d{4})[-/.年](\d{1,2})[-/.月](\d{1,2})日?$/.exec(s);
|
|
106
|
+
if (!m) m = /^(\d{4})(\d{2})(\d{2})$/.exec(s);
|
|
107
|
+
if (!m) throw new DateCalcError('BAD_DATE', { field, text: s });
|
|
108
|
+
const y = Number(m[1]);
|
|
109
|
+
const mo = Number(m[2]);
|
|
110
|
+
const d = Number(m[3]);
|
|
111
|
+
if (y < DC_MIN_YEAR || y > DC_MAX_YEAR) throw new DateCalcError('YEAR_RANGE', { field, text: s, year: y, min: DC_MIN_YEAR, max: DC_MAX_YEAR });
|
|
112
|
+
if (mo < 1 || mo > 12) throw new DateCalcError('BAD_DATE', { field, text: s });
|
|
113
|
+
if (d < 1 || d > dcDaysInMonth(y, mo)) throw new DateCalcError('NO_SUCH_DATE', { field, text: s, last: dcDaysInMonth(y, mo) });
|
|
114
|
+
return { y, m: mo, d, days: dcToDays(y, mo, d) };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 日付の並び(改行・カンマ・空白区切り)を読む。読めた日付と、読めなかった行を返す */
|
|
118
|
+
export function dcParseDateList(text) {
|
|
119
|
+
const dates = [];
|
|
120
|
+
const errors = [];
|
|
121
|
+
const seen = Object.create(null);
|
|
122
|
+
String(text == null ? '' : text)
|
|
123
|
+
.split(/[\s,、;;]+/)
|
|
124
|
+
.forEach((raw) => {
|
|
125
|
+
const s = raw.trim();
|
|
126
|
+
if (s === '' || s.charAt(0) === '#') return;
|
|
127
|
+
try {
|
|
128
|
+
const p = dcParseDate(s);
|
|
129
|
+
const iso = dcIso(p.y, p.m, p.d);
|
|
130
|
+
if (!seen[iso]) {
|
|
131
|
+
seen[iso] = true;
|
|
132
|
+
dates.push(iso);
|
|
133
|
+
}
|
|
134
|
+
} catch (e) {
|
|
135
|
+
if (!(e instanceof DateCalcError)) throw e;
|
|
136
|
+
errors.push({ text: s, code: e.code });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
dates.sort();
|
|
140
|
+
return { dates, errors };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 入力の指定 → 判定に使う設定。holidays には holidays.json をそのまま渡す */
|
|
144
|
+
export function dcOptions(o) {
|
|
145
|
+
const src = o || {};
|
|
146
|
+
const weekendKey = Object.prototype.hasOwnProperty.call(DC_WEEKEND, src.weekend) ? src.weekend : 'sat-sun';
|
|
147
|
+
const table = src.holidays && typeof src.holidays === 'object' ? src.holidays : null;
|
|
148
|
+
const useHolidays = src.useHolidays === undefined ? true : !!src.useHolidays;
|
|
149
|
+
const closed = dcParseDateList(src.closedDates);
|
|
150
|
+
const work = dcParseDateList(src.workDates);
|
|
151
|
+
const closedSet = Object.create(null);
|
|
152
|
+
closed.dates.forEach((d) => { closedSet[d] = true; });
|
|
153
|
+
const workSet = Object.create(null);
|
|
154
|
+
work.dates.forEach((d) => { workSet[d] = true; });
|
|
155
|
+
return {
|
|
156
|
+
weekendKey,
|
|
157
|
+
weekend: DC_WEEKEND[weekendKey],
|
|
158
|
+
useHolidays: useHolidays && !!(table && table.days),
|
|
159
|
+
hasTable: !!(table && table.days),
|
|
160
|
+
yearEnd: !!src.yearEnd,
|
|
161
|
+
table: table,
|
|
162
|
+
closed: closedSet,
|
|
163
|
+
work: workSet,
|
|
164
|
+
closedList: closed.dates,
|
|
165
|
+
workList: work.dates,
|
|
166
|
+
badDates: closed.errors.concat(work.errors),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 年末年始(12/29〜1/3)か */
|
|
171
|
+
export function dcIsYearEnd(c) {
|
|
172
|
+
return (c.m === 12 && c.d >= 29) || (c.m === 1 && c.d <= 3);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* その日が何の日か。
|
|
177
|
+
* type は work / weekend / holiday / year-end / closed のいずれか(優先度はこの逆順)。
|
|
178
|
+
*/
|
|
179
|
+
export function dcDayInfo(z, opt) {
|
|
180
|
+
const c = dcFromDays(z);
|
|
181
|
+
const date = dcIso(c.y, c.m, c.d);
|
|
182
|
+
const wd = dcWeekday(z);
|
|
183
|
+
const holidayName = opt.useHolidays && opt.table.days[date] ? opt.table.days[date] : null;
|
|
184
|
+
const flags = {
|
|
185
|
+
weekend: opt.weekend.indexOf(wd) >= 0,
|
|
186
|
+
holiday: !!holidayName,
|
|
187
|
+
yearEnd: opt.yearEnd && dcIsYearEnd(c),
|
|
188
|
+
closed: !!opt.closed[date],
|
|
189
|
+
forcedWork: !!opt.work[date],
|
|
190
|
+
};
|
|
191
|
+
let type = 'work';
|
|
192
|
+
if (flags.weekend) type = 'weekend';
|
|
193
|
+
if (flags.yearEnd) type = 'year-end';
|
|
194
|
+
if (flags.holiday) type = 'holiday';
|
|
195
|
+
if (flags.closed) type = 'closed';
|
|
196
|
+
if (flags.forcedWork) type = 'work';
|
|
197
|
+
const business = type === 'work';
|
|
198
|
+
return { days: z, date, y: c.y, m: c.m, d: c.d, weekday: wd, type, business, holiday: holidayName, flags };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function dcIsBusiness(z, opt) {
|
|
202
|
+
return dcDayInfo(z, opt).business;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** 祝日データの範囲外の年を含むか */
|
|
206
|
+
export function dcOutOfTable(fromZ, toZ, opt) {
|
|
207
|
+
if (!opt.useHolidays) return null;
|
|
208
|
+
const a = dcFromDays(fromZ).y;
|
|
209
|
+
const b = dcFromDays(toZ).y;
|
|
210
|
+
const lo = Math.min(a, b);
|
|
211
|
+
const hi = Math.max(a, b);
|
|
212
|
+
if (lo >= opt.table.minYear && hi <= opt.table.maxYear) return null;
|
|
213
|
+
return { from: lo, to: hi, min: opt.table.minYear, max: opt.table.maxYear };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** fromZ 〜 toZ(両端を含む)を1日ずつ数える */
|
|
217
|
+
export function dcCountRange(fromZ, toZ, opt) {
|
|
218
|
+
if (toZ < fromZ) return dcCountRange(toZ, fromZ, opt);
|
|
219
|
+
const span = toZ - fromZ + 1;
|
|
220
|
+
if (span > DC_MAX_SPAN) throw new DateCalcError('RANGE_TOO_LONG', { days: span, max: DC_MAX_SPAN });
|
|
221
|
+
const r = {
|
|
222
|
+
calendar: span,
|
|
223
|
+
business: 0,
|
|
224
|
+
nonBusiness: 0,
|
|
225
|
+
weekend: 0,
|
|
226
|
+
holiday: 0,
|
|
227
|
+
holidayOnWorkday: 0,
|
|
228
|
+
yearEnd: 0,
|
|
229
|
+
closed: 0,
|
|
230
|
+
forcedWork: 0,
|
|
231
|
+
weekdays: [0, 0, 0, 0, 0, 0, 0],
|
|
232
|
+
holidayList: [],
|
|
233
|
+
};
|
|
234
|
+
for (let z = fromZ; z <= toZ; z++) {
|
|
235
|
+
const info = dcDayInfo(z, opt);
|
|
236
|
+
r.weekdays[info.weekday]++;
|
|
237
|
+
if (info.business) r.business++;
|
|
238
|
+
else r.nonBusiness++;
|
|
239
|
+
if (info.flags.weekend) r.weekend++;
|
|
240
|
+
if (info.flags.holiday) {
|
|
241
|
+
r.holiday++;
|
|
242
|
+
if (!info.flags.weekend) r.holidayOnWorkday++;
|
|
243
|
+
if (r.holidayList.length < 400) r.holidayList.push({ date: info.date, name: info.holiday, weekday: info.weekday, business: info.business });
|
|
244
|
+
}
|
|
245
|
+
if (info.flags.yearEnd) r.yearEnd++;
|
|
246
|
+
if (info.flags.closed) r.closed++;
|
|
247
|
+
if (info.flags.forcedWork) r.forcedWork++;
|
|
248
|
+
}
|
|
249
|
+
return r;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** 年・月・日に分けた差(1/31 → 3/1 は「1ヶ月1日」のように月末を丸めて数える) */
|
|
253
|
+
export function dcDiffParts(fromZ, toZ) {
|
|
254
|
+
if (toZ < fromZ) {
|
|
255
|
+
const r = dcDiffParts(toZ, fromZ);
|
|
256
|
+
r.negative = true;
|
|
257
|
+
return r;
|
|
258
|
+
}
|
|
259
|
+
const a = dcFromDays(fromZ);
|
|
260
|
+
const b = dcFromDays(toZ);
|
|
261
|
+
let months = (b.y - a.y) * 12 + (b.m - a.m);
|
|
262
|
+
let anchor = dcAddMonths(fromZ, months);
|
|
263
|
+
if (anchor > toZ) {
|
|
264
|
+
months--;
|
|
265
|
+
anchor = dcAddMonths(fromZ, months);
|
|
266
|
+
}
|
|
267
|
+
return { years: Math.floor(months / 12), months: months % 12, days: toZ - anchor, totalMonths: months, negative: false };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** nヶ月後(月末は丸める。1/31 の1ヶ月後は 2/28 または 2/29) */
|
|
271
|
+
export function dcAddMonths(z, n) {
|
|
272
|
+
const c = dcFromDays(z);
|
|
273
|
+
const total = c.y * 12 + (c.m - 1) + n;
|
|
274
|
+
const y = Math.floor(total / 12);
|
|
275
|
+
const m = total - y * 12 + 1;
|
|
276
|
+
const d = Math.min(c.d, dcDaysInMonth(y, m));
|
|
277
|
+
return dcToDays(y, m, d);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** 休みなら前後の営業日へ寄せる。mode: next / prev / none */
|
|
281
|
+
export function dcAdjust(z, opt, mode) {
|
|
282
|
+
if (mode !== 'next' && mode !== 'prev') return { days: z, moved: 0 };
|
|
283
|
+
const step = mode === 'next' ? 1 : -1;
|
|
284
|
+
let cur = z;
|
|
285
|
+
let moved = 0;
|
|
286
|
+
while (!dcIsBusiness(cur, opt)) {
|
|
287
|
+
cur += step;
|
|
288
|
+
moved++;
|
|
289
|
+
if (moved > DC_MAX_SEEK) throw new DateCalcError('NO_BUSINESS_DAY', { max: DC_MAX_SEEK });
|
|
290
|
+
}
|
|
291
|
+
return { days: cur, moved: moved * step };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 起点日から n 営業日。
|
|
296
|
+
* includeStart が true なら起点日(それが営業日なら)を1営業日目として数える。
|
|
297
|
+
* n が負なら過去へ数える。飛ばした休みの一覧も返す。
|
|
298
|
+
*/
|
|
299
|
+
export function dcAddBusinessDays(z, n, opt, includeStart) {
|
|
300
|
+
const skipped = [];
|
|
301
|
+
let count = 0;
|
|
302
|
+
let cur = z;
|
|
303
|
+
let steps = 0;
|
|
304
|
+
const step = n < 0 ? -1 : 1;
|
|
305
|
+
const target = Math.abs(n);
|
|
306
|
+
if (target === 0) return { days: z, skipped, count: 0 };
|
|
307
|
+
if (includeStart && dcIsBusiness(z, opt)) count = 1;
|
|
308
|
+
else if (includeStart) {
|
|
309
|
+
// 起点日が休みのときは、数え始めの営業日まで進めてからそれを1日目とする
|
|
310
|
+
while (!dcIsBusiness(cur, opt)) {
|
|
311
|
+
const info = dcDayInfo(cur, opt);
|
|
312
|
+
if (skipped.length < 200) skipped.push({ date: info.date, type: info.type, name: info.holiday, weekday: info.weekday });
|
|
313
|
+
cur += step;
|
|
314
|
+
if (++steps > DC_MAX_SEEK) throw new DateCalcError('NO_BUSINESS_DAY', { max: DC_MAX_SEEK });
|
|
315
|
+
}
|
|
316
|
+
count = 1;
|
|
317
|
+
}
|
|
318
|
+
while (count < target) {
|
|
319
|
+
cur += step;
|
|
320
|
+
if (++steps > DC_MAX_SEEK) throw new DateCalcError('NO_BUSINESS_DAY', { max: DC_MAX_SEEK });
|
|
321
|
+
const info = dcDayInfo(cur, opt);
|
|
322
|
+
if (info.business) count++;
|
|
323
|
+
else if (skipped.length < 200) skipped.push({ date: info.date, type: info.type, name: info.holiday, weekday: info.weekday });
|
|
324
|
+
}
|
|
325
|
+
return { days: cur, skipped, count: count };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** 起点日以降(同日を含む)で最初に来る締め日。day は 1〜31 か 'eom'(月末) */
|
|
329
|
+
export function dcClosingDate(z, day) {
|
|
330
|
+
const c = dcFromDays(z);
|
|
331
|
+
if (day === 'eom') {
|
|
332
|
+
const last = dcToDays(c.y, c.m, dcDaysInMonth(c.y, c.m));
|
|
333
|
+
return last >= z ? last : dcAddMonths(last, 1);
|
|
334
|
+
}
|
|
335
|
+
const n = Number(day);
|
|
336
|
+
if (!(n >= 1 && n <= 31)) throw new DateCalcError('BAD_CLOSING', { day: day });
|
|
337
|
+
// その月に無い日(2月31日など)は月末として扱う
|
|
338
|
+
const inMonth = dcToDays(c.y, c.m, Math.min(n, dcDaysInMonth(c.y, c.m)));
|
|
339
|
+
if (inMonth >= z) return inMonth;
|
|
340
|
+
const next = dcAddMonths(dcToDays(c.y, c.m, 1), 1);
|
|
341
|
+
const nc = dcFromDays(next);
|
|
342
|
+
return dcToDays(nc.y, nc.m, Math.min(n, dcDaysInMonth(nc.y, nc.m)));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** 締め日の n ヶ月後の、指定した日('eom' は月末) */
|
|
346
|
+
export function dcPayDate(closingZ, monthOffset, day) {
|
|
347
|
+
const base = dcAddMonths(dcToDays(dcFromDays(closingZ).y, dcFromDays(closingZ).m, 1), monthOffset);
|
|
348
|
+
const c = dcFromDays(base);
|
|
349
|
+
if (day === 'eom') return dcToDays(c.y, c.m, dcDaysInMonth(c.y, c.m));
|
|
350
|
+
const n = Number(day);
|
|
351
|
+
if (!(n >= 1 && n <= 31)) throw new DateCalcError('BAD_PAYDAY', { day: day });
|
|
352
|
+
return dcToDays(c.y, c.m, Math.min(n, dcDaysInMonth(c.y, c.m)));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export function dcDayOut(z, opt) {
|
|
356
|
+
const info = dcDayInfo(z, opt);
|
|
357
|
+
return { date: info.date, weekday: info.weekday, type: info.type, business: info.business, holiday: info.holiday };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 計算の入口。site と MCP が同じ結果オブジェクトを使う。
|
|
362
|
+
* mode: between(2日付の間) / add(起点日+N) / payment(締め日と支払サイト)
|
|
363
|
+
*/
|
|
364
|
+
export function dcRun(input) {
|
|
365
|
+
const src = input || {};
|
|
366
|
+
const opt = dcOptions(src);
|
|
367
|
+
const mode = src.mode === 'add' || src.mode === 'payment' ? src.mode : 'between';
|
|
368
|
+
const warnings = [];
|
|
369
|
+
if (opt.badDates.length) warnings.push({ code: 'BAD_EXTRA_DATE', info: { list: opt.badDates.map((e) => e.text) } });
|
|
370
|
+
if (src.useHolidays !== false && !opt.hasTable) warnings.push({ code: 'NO_HOLIDAY_TABLE', info: {} });
|
|
371
|
+
|
|
372
|
+
const settings = {
|
|
373
|
+
weekend: opt.weekendKey,
|
|
374
|
+
holidays: opt.useHolidays,
|
|
375
|
+
year_end: opt.yearEnd,
|
|
376
|
+
closed_dates: opt.closedList,
|
|
377
|
+
work_dates: opt.workList,
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
if (mode === 'between') {
|
|
381
|
+
const a = dcParseDate(src.start, 'start');
|
|
382
|
+
const b = dcParseDate(src.end, 'end');
|
|
383
|
+
const swapped = b.days < a.days;
|
|
384
|
+
const fromZ = swapped ? b.days : a.days;
|
|
385
|
+
const toZ = swapped ? a.days : b.days;
|
|
386
|
+
if (swapped) warnings.push({ code: 'SWAPPED', info: {} });
|
|
387
|
+
const outside = dcOutOfTable(fromZ, toZ, opt);
|
|
388
|
+
if (outside) warnings.push({ code: 'OUT_OF_TABLE', info: outside });
|
|
389
|
+
const includeStart = src.includeStart === undefined ? true : !!src.includeStart;
|
|
390
|
+
const countFrom = includeStart ? fromZ : Math.min(fromZ + 1, toZ + 1);
|
|
391
|
+
const counts = countFrom > toZ
|
|
392
|
+
? { calendar: 0, business: 0, nonBusiness: 0, weekend: 0, holiday: 0, holidayOnWorkday: 0, yearEnd: 0, closed: 0, forcedWork: 0, weekdays: [0, 0, 0, 0, 0, 0, 0], holidayList: [] }
|
|
393
|
+
: dcCountRange(countFrom, toZ, opt);
|
|
394
|
+
const parts = dcDiffParts(fromZ, toZ);
|
|
395
|
+
const diff = toZ - fromZ;
|
|
396
|
+
return {
|
|
397
|
+
mode: 'between',
|
|
398
|
+
settings: settings,
|
|
399
|
+
start: dcDayOut(fromZ, opt),
|
|
400
|
+
end: dcDayOut(toZ, opt),
|
|
401
|
+
include_start: includeStart,
|
|
402
|
+
calendar_days: counts.calendar,
|
|
403
|
+
days_between: diff,
|
|
404
|
+
days_inclusive: diff + 1,
|
|
405
|
+
business_days: counts.business,
|
|
406
|
+
non_business_days: counts.nonBusiness,
|
|
407
|
+
weekend_days: counts.weekend,
|
|
408
|
+
holiday_days: counts.holiday,
|
|
409
|
+
holidays_on_weekdays: counts.holidayOnWorkday,
|
|
410
|
+
year_end_days: counts.yearEnd,
|
|
411
|
+
extra_closed_days: counts.closed,
|
|
412
|
+
extra_work_days: counts.forcedWork,
|
|
413
|
+
weeks: { weeks: Math.floor(diff / 7), days: diff % 7 },
|
|
414
|
+
breakdown: { years: parts.years, months: parts.months, days: parts.days, total_months: parts.totalMonths },
|
|
415
|
+
weekday_counts: counts.weekdays,
|
|
416
|
+
holiday_list: counts.holidayList,
|
|
417
|
+
warnings: warnings,
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (mode === 'add') {
|
|
422
|
+
const base = dcParseDate(src.base !== undefined ? src.base : src.start, 'base');
|
|
423
|
+
const unit = ['days', 'business', 'weeks', 'months', 'years'].indexOf(src.unit) >= 0 ? src.unit : 'business';
|
|
424
|
+
if (String(src.n === undefined ? '' : src.n).trim() === '') throw new DateCalcError('BAD_NUMBER', { text: '' });
|
|
425
|
+
const n = Math.trunc(Number(src.n));
|
|
426
|
+
if (!isFinite(n)) throw new DateCalcError('BAD_NUMBER', { text: String(src.n) });
|
|
427
|
+
if (Math.abs(n) > 100000) throw new DateCalcError('NUMBER_RANGE', { max: 100000 });
|
|
428
|
+
const includeStart = !!src.includeStart;
|
|
429
|
+
const adjustMode = ['next', 'prev', 'none'].indexOf(src.adjust) >= 0 ? src.adjust : 'none';
|
|
430
|
+
let rawZ;
|
|
431
|
+
let skipped = [];
|
|
432
|
+
if (unit === 'business') {
|
|
433
|
+
const r = dcAddBusinessDays(base.days, n, opt, includeStart);
|
|
434
|
+
rawZ = r.days;
|
|
435
|
+
skipped = r.skipped;
|
|
436
|
+
} else if (unit === 'days') {
|
|
437
|
+
rawZ = base.days + (includeStart && n !== 0 ? (n > 0 ? n - 1 : n + 1) : n);
|
|
438
|
+
} else if (unit === 'weeks') {
|
|
439
|
+
rawZ = base.days + n * 7;
|
|
440
|
+
} else if (unit === 'months') {
|
|
441
|
+
rawZ = dcAddMonths(base.days, n);
|
|
442
|
+
} else {
|
|
443
|
+
rawZ = dcAddMonths(base.days, n * 12);
|
|
444
|
+
}
|
|
445
|
+
const outside = dcOutOfTable(Math.min(base.days, rawZ), Math.max(base.days, rawZ), opt);
|
|
446
|
+
if (outside) warnings.push({ code: 'OUT_OF_TABLE', info: outside });
|
|
447
|
+
const adj = unit === 'business' ? { days: rawZ, moved: 0 } : dcAdjust(rawZ, opt, adjustMode);
|
|
448
|
+
const resultZ = adj.days;
|
|
449
|
+
const lo = Math.min(base.days, resultZ);
|
|
450
|
+
const hi = Math.max(base.days, resultZ);
|
|
451
|
+
const counts = dcCountRange(lo, hi, opt);
|
|
452
|
+
return {
|
|
453
|
+
mode: 'add',
|
|
454
|
+
settings: settings,
|
|
455
|
+
base: dcDayOut(base.days, opt),
|
|
456
|
+
amount: n,
|
|
457
|
+
unit: unit,
|
|
458
|
+
include_start: includeStart,
|
|
459
|
+
adjust: unit === 'business' ? 'none' : adjustMode,
|
|
460
|
+
result: dcDayOut(resultZ, opt),
|
|
461
|
+
raw_result: adj.moved === 0 ? null : dcDayOut(rawZ, opt),
|
|
462
|
+
moved_days: adj.moved,
|
|
463
|
+
calendar_days: resultZ - base.days,
|
|
464
|
+
business_days_between: counts.business,
|
|
465
|
+
non_business_days_between: counts.nonBusiness,
|
|
466
|
+
skipped_days: skipped,
|
|
467
|
+
warnings: warnings,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// mode === 'payment'
|
|
472
|
+
const inv = dcParseDate(src.invoice !== undefined ? src.invoice : src.base, 'invoice');
|
|
473
|
+
const closingDay = src.closing === undefined || src.closing === '' ? 'eom' : src.closing;
|
|
474
|
+
const payDay = src.payDay === undefined || src.payDay === '' ? 'eom' : src.payDay;
|
|
475
|
+
const monthOffset = Math.trunc(Number(src.payMonths === undefined ? 1 : src.payMonths));
|
|
476
|
+
if (!isFinite(monthOffset) || monthOffset < 0 || monthOffset > 12) throw new DateCalcError('BAD_PAY_MONTHS', { max: 12 });
|
|
477
|
+
const payAdjust = ['next', 'prev', 'none'].indexOf(src.payAdjust) >= 0 ? src.payAdjust : 'prev';
|
|
478
|
+
const closingZ = closingDay === 'none' ? inv.days : dcClosingDate(inv.days, closingDay);
|
|
479
|
+
const rawPayZ = dcPayDate(closingZ, monthOffset, payDay);
|
|
480
|
+
const outside = dcOutOfTable(inv.days, rawPayZ, opt);
|
|
481
|
+
if (outside) warnings.push({ code: 'OUT_OF_TABLE', info: outside });
|
|
482
|
+
const adj = dcAdjust(rawPayZ, opt, payAdjust);
|
|
483
|
+
if (adj.days < closingZ) warnings.push({ code: 'PAY_BEFORE_CLOSING', info: {} });
|
|
484
|
+
const counts = dcCountRange(Math.min(inv.days, adj.days), Math.max(inv.days, adj.days), opt);
|
|
485
|
+
return {
|
|
486
|
+
mode: 'payment',
|
|
487
|
+
settings: settings,
|
|
488
|
+
invoice: dcDayOut(inv.days, opt),
|
|
489
|
+
closing_rule: { day: closingDay, month_offset: monthOffset, pay_day: payDay, adjust: payAdjust },
|
|
490
|
+
closing: dcDayOut(closingZ, opt),
|
|
491
|
+
payment: dcDayOut(adj.days, opt),
|
|
492
|
+
raw_payment: adj.moved === 0 ? null : dcDayOut(rawPayZ, opt),
|
|
493
|
+
moved_days: adj.moved,
|
|
494
|
+
days_from_invoice: adj.days - inv.days,
|
|
495
|
+
days_from_closing: adj.days - closingZ,
|
|
496
|
+
business_days_from_closing: adj.days > closingZ ? dcCountRange(closingZ + 1, adj.days, opt).business : 0,
|
|
497
|
+
calendar_days_total: counts.calendar,
|
|
498
|
+
warnings: warnings,
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/* ==================== ここまで計算コア ==================== */
|
|
503
|
+
|
|
504
|
+
/* ---- ここから下はMCP側だけの層(入出力・文言)。site側の app.js には UI 用の同じ文言表がある ---- */
|
|
505
|
+
|
|
506
|
+
export class DateCalcToolError extends Error {}
|
|
507
|
+
|
|
508
|
+
/** 内閣府「国民の祝日について」由来の祝日表(site/date-calc/holidays.json と同じ内容) */
|
|
509
|
+
export const HOLIDAYS = JSON.parse(readFileSync(fileURLToPath(new URL('./date-holidays.json', import.meta.url)), 'utf8'));
|
|
510
|
+
|
|
511
|
+
const WD_JA = ['日', '月', '火', '水', '木', '金', '土'];
|
|
512
|
+
const WD_EN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
513
|
+
|
|
514
|
+
const TYPE_JA = {
|
|
515
|
+
work: '営業日',
|
|
516
|
+
weekend: '週の休み',
|
|
517
|
+
holiday: '祝日',
|
|
518
|
+
'year-end': '年末年始',
|
|
519
|
+
closed: '休業日(指定)',
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
const TYPE_EN = {
|
|
523
|
+
work: 'Business day',
|
|
524
|
+
weekend: 'Weekend',
|
|
525
|
+
holiday: 'Public holiday',
|
|
526
|
+
'year-end': 'Year-end break',
|
|
527
|
+
closed: 'Closed (added)',
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const ERR_JA = {
|
|
531
|
+
EMPTY_DATE: (i) => `日付を入れてください(${i.field || 'date'})。`,
|
|
532
|
+
BAD_DATE: (i) => `日付として読めません: ${i.text} — 2026-09-20 のように書きます。`,
|
|
533
|
+
NO_SUCH_DATE: (i) => `その日は存在しません: ${i.text}(この月は ${i.last} 日までです)。`,
|
|
534
|
+
YEAR_RANGE: (i) => `扱えるのは ${i.min}年〜${i.max}年です: ${i.text}`,
|
|
535
|
+
BAD_NUMBER: () => 'n には数字を入れてください。',
|
|
536
|
+
NUMBER_RANGE: (i) => `n は ${i.max} までにしてください。`,
|
|
537
|
+
RANGE_TOO_LONG: (i) => `期間が長すぎます(${i.days}日)。${i.max}日までで指定してください。`,
|
|
538
|
+
NO_BUSINESS_DAY: () => '営業日が1日も見つかりません。休みの決め方(weekend・holidays・closed_dates)を見直してください。',
|
|
539
|
+
BAD_CLOSING: (i) => `closing は 1〜31 か eom か none です: ${i.day}`,
|
|
540
|
+
BAD_PAYDAY: (i) => `pay_day は 1〜31 か eom です: ${i.day}`,
|
|
541
|
+
BAD_PAY_MONTHS: (i) => `pay_months は 0〜${i.max} の整数です。`,
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
const ERR_EN = {
|
|
545
|
+
EMPTY_DATE: (i) => `A date is required (${i.field || 'date'}).`,
|
|
546
|
+
BAD_DATE: (i) => `Not a valid date: ${i.text} — write it as 2026-09-20.`,
|
|
547
|
+
NO_SUCH_DATE: (i) => `That day does not exist: ${i.text} (the month ends on the ${i.last}).`,
|
|
548
|
+
YEAR_RANGE: (i) => `Only years ${i.min}–${i.max} are supported: ${i.text}`,
|
|
549
|
+
BAD_NUMBER: () => 'n must be a number.',
|
|
550
|
+
NUMBER_RANGE: (i) => `Keep n at or below ${i.max}.`,
|
|
551
|
+
RANGE_TOO_LONG: (i) => `That range is too long (${i.days} days). Keep it within ${i.max} days.`,
|
|
552
|
+
NO_BUSINESS_DAY: () => 'No business day could be found. Check weekend / holidays / closed_dates.',
|
|
553
|
+
BAD_CLOSING: (i) => `closing must be 1-31, eom or none: ${i.day}`,
|
|
554
|
+
BAD_PAYDAY: (i) => `pay_day must be 1-31 or eom: ${i.day}`,
|
|
555
|
+
BAD_PAY_MONTHS: (i) => `pay_months must be an integer between 0 and ${i.max}.`,
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
const WARN_JA = {
|
|
559
|
+
SWAPPED: () => '開始日と終了日が逆だったので入れ替えて計算しました。',
|
|
560
|
+
OUT_OF_TABLE: (i) => `祝日データは ${i.min}年〜${i.max}年ぶんです。${i.from}年〜${i.to}年のうち範囲の外は、祝日を考えずに数えています。`,
|
|
561
|
+
NO_HOLIDAY_TABLE: () => '祝日データを読み込めなかったため、土日と指定した休業日だけで数えています。',
|
|
562
|
+
BAD_EXTRA_DATE: (i) => `読み取れなかった日付があります: ${i.list.join(' / ')}`,
|
|
563
|
+
PAY_BEFORE_CLOSING: () => '支払日が締め日より前になっています。pay_months・pay_day の指定を見直してください。',
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
const WARN_EN = {
|
|
567
|
+
SWAPPED: () => 'The start and end dates were the wrong way round, so they were swapped.',
|
|
568
|
+
OUT_OF_TABLE: (i) => `The holiday table covers ${i.min}-${i.max}. Years outside that range (${i.from}-${i.to}) are counted without public holidays.`,
|
|
569
|
+
NO_HOLIDAY_TABLE: () => 'The holiday table could not be loaded, so only weekends and the days you listed are treated as days off.',
|
|
570
|
+
BAD_EXTRA_DATE: (i) => `Some dates could not be read: ${i.list.join(' / ')}`,
|
|
571
|
+
PAY_BEFORE_CLOSING: () => 'The payment date falls before the closing date. Check pay_months and pay_day.',
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
const errText = (lang, code, info) => {
|
|
575
|
+
const table = lang === 'en' ? ERR_EN : ERR_JA;
|
|
576
|
+
return table[code] ? table[code](info || {}) : code;
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
const warnText = (lang, w) => {
|
|
580
|
+
const table = lang === 'en' ? WARN_EN : WARN_JA;
|
|
581
|
+
return { code: w.code, message: table[w.code] ? table[w.code](w.info || {}) : w.code };
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
/** 祝日名。英語では holidays.json の names 表で英名へ置き換える */
|
|
585
|
+
const holidayLabel = (lang, name) => (!name ? null : lang === 'en' ? HOLIDAYS.names[name] || name : name);
|
|
586
|
+
|
|
587
|
+
/** 日付1件に曜日・種別の表示名を足す */
|
|
588
|
+
function shapeDay(day, lang) {
|
|
589
|
+
if (!day) return null;
|
|
590
|
+
return {
|
|
591
|
+
date: day.date,
|
|
592
|
+
weekday: (lang === 'en' ? WD_EN : WD_JA)[day.weekday],
|
|
593
|
+
weekday_index: day.weekday,
|
|
594
|
+
type: day.type,
|
|
595
|
+
type_label: (lang === 'en' ? TYPE_EN : TYPE_JA)[day.type] || day.type,
|
|
596
|
+
business: day.business,
|
|
597
|
+
holiday: holidayLabel(lang, day.holiday),
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** 引数の日付リスト(配列でも改行区切りの文字列でも受ける) */
|
|
602
|
+
const asDateText = (v) => (Array.isArray(v) ? v.join('\n') : v === undefined || v === null ? '' : String(v));
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* MCPツール date_calc の本体。2日付間の日数/営業日数・起点日+Nの期日・支払サイトの3つを受ける。
|
|
606
|
+
* 計算そのものは計算コア(site版と同一)に任せ、ここは入出力と文言だけを持つ。
|
|
607
|
+
*/
|
|
608
|
+
export async function dateCalcTool(opts = {}) {
|
|
609
|
+
const o = opts || {};
|
|
610
|
+
const lang = o.lang === undefined ? 'ja' : o.lang;
|
|
611
|
+
if (lang !== 'ja' && lang !== 'en') throw new DateCalcToolError(`lang は ja か en: ${o.lang}`);
|
|
612
|
+
|
|
613
|
+
const hasStart = typeof o.start === 'string' && o.start.trim() !== '';
|
|
614
|
+
const hasEnd = typeof o.end === 'string' && o.end.trim() !== '';
|
|
615
|
+
const hasBase = typeof o.base === 'string' && o.base.trim() !== '';
|
|
616
|
+
const hasInvoice = typeof o.invoice === 'string' && o.invoice.trim() !== '';
|
|
617
|
+
let mode = o.mode;
|
|
618
|
+
if (mode === undefined) {
|
|
619
|
+
if (hasInvoice) mode = 'payment';
|
|
620
|
+
else if (o.n !== undefined) mode = 'add';
|
|
621
|
+
else mode = 'between';
|
|
622
|
+
}
|
|
623
|
+
if (mode !== 'between' && mode !== 'add' && mode !== 'payment') {
|
|
624
|
+
throw new DateCalcToolError(`mode は between / add / payment のどれかです: ${o.mode}`);
|
|
625
|
+
}
|
|
626
|
+
if (mode === 'between' && !(hasStart && hasEnd)) throw new DateCalcToolError('mode=between には start と end が要ります');
|
|
627
|
+
if (mode === 'add' && !(hasBase || hasStart)) throw new DateCalcToolError('mode=add には base(起点日)が要ります');
|
|
628
|
+
if (mode === 'add' && o.n === undefined) throw new DateCalcToolError('mode=add には n(加える量)が要ります');
|
|
629
|
+
if (mode === 'payment' && !(hasInvoice || hasBase)) throw new DateCalcToolError('mode=payment には invoice(請求・発生日)が要ります');
|
|
630
|
+
if (o.weekend !== undefined && !Object.prototype.hasOwnProperty.call(DC_WEEKEND, o.weekend)) {
|
|
631
|
+
throw new DateCalcToolError(`weekend は ${Object.keys(DC_WEEKEND).join(' / ')} のどれかです: ${o.weekend}`);
|
|
632
|
+
}
|
|
633
|
+
if (o.unit !== undefined && ['days', 'business', 'weeks', 'months', 'years'].indexOf(o.unit) < 0) {
|
|
634
|
+
throw new DateCalcToolError(`unit は days / business / weeks / months / years のどれかです: ${o.unit}`);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const input = {
|
|
638
|
+
mode: mode,
|
|
639
|
+
start: o.start,
|
|
640
|
+
end: o.end,
|
|
641
|
+
base: hasBase ? o.base : o.start,
|
|
642
|
+
n: o.n,
|
|
643
|
+
unit: o.unit,
|
|
644
|
+
includeStart: mode === 'between' ? (o.include_start === undefined ? true : !!o.include_start) : !!o.include_start,
|
|
645
|
+
adjust: o.adjust,
|
|
646
|
+
invoice: hasInvoice ? o.invoice : o.base,
|
|
647
|
+
closing: o.closing,
|
|
648
|
+
payMonths: o.pay_months,
|
|
649
|
+
payDay: o.pay_day,
|
|
650
|
+
payAdjust: o.pay_adjust,
|
|
651
|
+
weekend: o.weekend,
|
|
652
|
+
useHolidays: o.holidays === undefined ? true : !!o.holidays,
|
|
653
|
+
yearEnd: !!o.year_end,
|
|
654
|
+
closedDates: asDateText(o.closed_dates),
|
|
655
|
+
workDates: asDateText(o.work_dates),
|
|
656
|
+
holidays: HOLIDAYS,
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
let r;
|
|
660
|
+
try {
|
|
661
|
+
r = dcRun(input);
|
|
662
|
+
} catch (err) {
|
|
663
|
+
if (!(err instanceof DateCalcError)) throw err;
|
|
664
|
+
// 入力の日付が壊れているのは「呼び出し方の誤り」ではないので、例外ではなく結果として返す
|
|
665
|
+
return { ok: false, mode: mode, error: { code: err.code, message: errText(lang, err.code, err.info) } };
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
const out = { ok: true, mode: r.mode, settings: r.settings };
|
|
669
|
+
const warn = r.warnings.map((w) => warnText(lang, w));
|
|
670
|
+
|
|
671
|
+
if (r.mode === 'between') {
|
|
672
|
+
Object.assign(out, {
|
|
673
|
+
start: shapeDay(r.start, lang),
|
|
674
|
+
end: shapeDay(r.end, lang),
|
|
675
|
+
include_start: r.include_start,
|
|
676
|
+
calendar_days: r.calendar_days,
|
|
677
|
+
days_between: r.days_between,
|
|
678
|
+
days_inclusive: r.days_inclusive,
|
|
679
|
+
business_days: r.business_days,
|
|
680
|
+
non_business_days: r.non_business_days,
|
|
681
|
+
weekend_days: r.weekend_days,
|
|
682
|
+
holiday_days: r.holiday_days,
|
|
683
|
+
holidays_on_weekdays: r.holidays_on_weekdays,
|
|
684
|
+
year_end_days: r.year_end_days,
|
|
685
|
+
extra_closed_days: r.extra_closed_days,
|
|
686
|
+
extra_work_days: r.extra_work_days,
|
|
687
|
+
weeks: r.weeks,
|
|
688
|
+
breakdown: r.breakdown,
|
|
689
|
+
weekday_counts: r.weekday_counts,
|
|
690
|
+
holidays: r.holiday_list.map((h) => ({
|
|
691
|
+
date: h.date,
|
|
692
|
+
name: holidayLabel(lang, h.name),
|
|
693
|
+
weekday: (lang === 'en' ? WD_EN : WD_JA)[h.weekday],
|
|
694
|
+
})),
|
|
695
|
+
});
|
|
696
|
+
} else if (r.mode === 'add') {
|
|
697
|
+
Object.assign(out, {
|
|
698
|
+
base: shapeDay(r.base, lang),
|
|
699
|
+
amount: r.amount,
|
|
700
|
+
unit: r.unit,
|
|
701
|
+
include_start: r.include_start,
|
|
702
|
+
adjust: r.adjust,
|
|
703
|
+
result: shapeDay(r.result, lang),
|
|
704
|
+
before_adjust: shapeDay(r.raw_result, lang),
|
|
705
|
+
moved_days: r.moved_days,
|
|
706
|
+
calendar_days: r.calendar_days,
|
|
707
|
+
business_days_between: r.business_days_between,
|
|
708
|
+
non_business_days_between: r.non_business_days_between,
|
|
709
|
+
skipped_days: r.skipped_days.map((d) => ({
|
|
710
|
+
date: d.date,
|
|
711
|
+
weekday: (lang === 'en' ? WD_EN : WD_JA)[d.weekday],
|
|
712
|
+
type: d.type,
|
|
713
|
+
type_label: (lang === 'en' ? TYPE_EN : TYPE_JA)[d.type] || d.type,
|
|
714
|
+
name: holidayLabel(lang, d.name),
|
|
715
|
+
})),
|
|
716
|
+
});
|
|
717
|
+
} else {
|
|
718
|
+
Object.assign(out, {
|
|
719
|
+
invoice: shapeDay(r.invoice, lang),
|
|
720
|
+
rule: r.closing_rule,
|
|
721
|
+
closing: shapeDay(r.closing, lang),
|
|
722
|
+
payment: shapeDay(r.payment, lang),
|
|
723
|
+
before_adjust: shapeDay(r.raw_payment, lang),
|
|
724
|
+
moved_days: r.moved_days,
|
|
725
|
+
days_from_invoice: r.days_from_invoice,
|
|
726
|
+
days_from_closing: r.days_from_closing,
|
|
727
|
+
business_days_from_closing: r.business_days_from_closing,
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
out.holiday_table = { source: HOLIDAYS.source, min_year: HOLIDAYS.minYear, max_year: HOLIDAYS.maxYear, retrieved: HOLIDAYS.retrieved };
|
|
732
|
+
if (warn.length) out.notes = warn;
|
|
733
|
+
return out;
|
|
734
|
+
}
|