工作日推算
灵感:能力工具箱 workday-calc(轻量、本地推算)。
何时使用
- 从某日起加/减 N 个工作日得到约定跟进日。
- 计算两日之间剩余工作日(跟进 SLA 前置能力)。
- 需要跳过周末,并可挂接法定假/调休样本。
硬约束
- 工作日定义:默认周一~周五;周末不算;假日集合内日期不算;调休上班日算工作日。
- 输入合法日期:非法日期直接报错,勿静默落到 Invalid Date。
- 节假日来源:可内置小样本或对接
chinese-days类库;须标明「以国务院公布为准」。 - 与日历 Skill 区分:本 Skill 做推算;完整节假日查询可另用节假日 Skill。
- 交付:给出可运行 TS/JS;
addWorkdays/countWorkdays至少其一完整。
推荐实现要点
function isWeekend(d: Date) {
const w = d.getDay()
return w === 0 || w === 6
}
function ymd(d: Date) {
const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0')
return `${y}-${m}-${day}`
}
export function addWorkdays(
start: Date,
n: number,
holidays = new Set<string>(),
workdaysOnWeekend = new Set<string>(), // 调休上班
) {
if (!Number.isFinite(n) || Math.trunc(n) !== n) throw new Error('n 须为整数')
const step = n >= 0 ? 1 : -1
let left = Math.abs(n)
const cur = new Date(start.getFullYear(), start.getMonth(), start.getDate())
while (left > 0) {
cur.setDate(cur.getDate() + step)
const key = ymd(cur)
const work =
workdaysOnWeekend.has(key) ||
(!isWeekend(cur) && !holidays.has(key))
if (work) left -= 1
}
return cur
}
Agent 执行步骤
- 实现
addWorkdays;可选countWorkdays(from, to)。 - 准备至少一年常见假日样本或说明如何注入。
- 自测:周五 +1 → 下周一;跨国庆样本。
- UI 文案注明样本局限。
附加
- 说明见 README.md
- 排错见 reference.md
- 模板见 templates/usage.md