UNPKG

nz-holidays

Version:

Lightweight New Zealand public holidays library

291 lines (290 loc) 7.85 kB
function B(e) { const a = e % 19, t = Math.floor(e / 100), n = e % 100, s = Math.floor(t / 4), o = t % 4, i = Math.floor((t + 8) / 25), c = Math.floor((t - i + 1) / 3), r = (19 * a + t - s - c + 15) % 30, m = Math.floor(n / 4), g = n % 4, y = (32 + 2 * o + 2 * m - r - g) % 7, l = Math.floor((a + 11 * r + 22 * y) / 451), f = Math.floor((r + y - 7 * l + 114) / 31), h = (r + y - 7 * l + 114) % 31 + 1; return new Date(e, f - 1, h); } function x(e) { const t = { 2022: [5, 24], 2023: [6, 14], 2024: [5, 28], 2025: [5, 20], 2026: [6, 10], 2027: [5, 25], 2028: [6, 14], 2029: [6, 6], 2030: [5, 21], 2031: [6, 11], 2032: [6, 2], 2033: [5, 24], 2034: [6, 7], 2035: [5, 29], 2036: [6, 18], 2037: [6, 10], 2038: [5, 25], 2039: [6, 15], 2040: [6, 6], 2041: [6, 19], 2042: [6, 11], 2043: [6, 3], 2044: [5, 24], 2045: [6, 7], 2046: [5, 29], 2047: [6, 19], 2048: [6, 3], 2049: [5, 25], 2050: [6, 15], 2051: [5, 30], 2052: [5, 21] }[e]; return t ? new Date(e, t[0], t[1]) : new Date(e, 6, 1); } function u(e, a, t, n) { const o = new Date(e, a, 1).getDay(), r = 1 + (t - o + 7) % 7 + (n - 1) * 7; return new Date(e, a, r); } function M(e, a) { const t = e.getDay(), n = a.getDay(); if (t !== 0 && t !== 6 && n !== 0 && n !== 6) return { first: e, second: a }; let s = e, o = a; return t === 0 && (s = new Date(e.getTime() + 24 * 60 * 60 * 1e3)), t === 6 && (s = new Date(e.getTime() + 2 * 24 * 60 * 60 * 1e3)), n === 0 && (s.getDay() === 1 && s.getTime() === a.getTime() + 24 * 60 * 60 * 1e3 ? o = new Date(a.getTime() + 2 * 24 * 60 * 60 * 1e3) : o = new Date(a.getTime() + 24 * 60 * 60 * 1e3)), n === 6 && (o = new Date(a.getTime() + 2 * 24 * 60 * 60 * 1e3)), { first: s, second: o }; } function F(e) { const a = e.getDay(); return a === 0 ? new Date(e.getTime() + 24 * 60 * 60 * 1e3) : a === 6 ? new Date(e.getTime() + 2 * 24 * 60 * 60 * 1e3) : e; } function b(e, a = {}) { const t = [], { includeObserved: n = !0, region: s } = a, o = new Date(e, 0, 1), i = new Date(e, 0, 2), c = M(o, i), r = { name: "New Year's Day", date: o, type: "national" }; c.first.getTime() !== o.getTime() && (r.observed = c.first), t.push(r); const m = { name: "Day after New Year's Day", date: i, type: "national" }; c.second.getTime() !== i.getTime() && (m.observed = c.second), t.push(m); const g = new Date(e, 11, 25), y = new Date(e, 11, 26), l = M(g, y), f = { name: "Christmas Day", date: g, type: "national" }; l.first.getTime() !== g.getTime() && (f.observed = l.first), t.push(f); const h = { name: "Boxing Day", date: y, type: "national" }; l.second.getTime() !== y.getTime() && (h.observed = l.second), t.push(h); const H = [ { name: "Waitangi Day", month: 1, day: 6 }, { name: "ANZAC Day", month: 3, day: 25 } ]; for (const { name: d, month: w, day: W } of H) { const p = new Date(e, w, W), v = { name: d, date: p, type: "national" }; if (n) { const A = F(p); A.getTime() !== p.getTime() && (v.observed = A); } t.push(v); } const T = B(e), Y = new Date(T.getTime() - 2 * 24 * 60 * 60 * 1e3), N = new Date(T.getTime() + 24 * 60 * 60 * 1e3); t.push( { name: "Good Friday", date: Y, type: "national" }, { name: "Easter Monday", date: N, type: "national" } ); const C = u(e, 5, 1, 1); let k = "Queen's Birthday"; if (e >= 2023 && (k = "King's Birthday"), t.push({ name: k, date: C, type: "national" }), e >= 2022) { const d = x(e); t.push({ name: "Matariki", date: d, type: "national" }); } const O = u(e, 9, 1, 4); if (t.push({ name: "Labour Day", date: O, type: "national" }), s) { const d = S(e, s); t.push(...d); } return t.sort((d, w) => d.date.getTime() - w.date.getTime()); } function S(e, a) { const t = []; if (a === "auckland") { const n = new Date(e, 0, 29), s = D(n); t.push({ name: "Auckland Anniversary Day", date: s, type: "regional" }); } if (a === "wellington") { const n = new Date(e, 0, 22), s = D(n); t.push({ name: "Wellington Anniversary Day", date: s, type: "regional" }); } if (a === "canterbury") { const n = u(e, 10, 2, 1), s = new Date(n.getTime() + 10 * 24 * 60 * 60 * 1e3); t.push({ name: "Canterbury Anniversary Day", date: s, type: "regional" }); } if (a === "otago") { const n = new Date(e, 2, 23), s = D(n); t.push({ name: "Otago Anniversary Day", date: s, type: "regional" }); } if (a === "southland") { const n = B(e), s = new Date(n.getTime() + 2 * 24 * 60 * 60 * 1e3); t.push({ name: "Southland Anniversary Day", date: s, type: "regional" }); } if (a === "westland") { const n = new Date(e, 11, 1), s = D(n); t.push({ name: "Westland Anniversary Day", date: s, type: "regional" }); } if (a === "nelson") { const n = new Date(e, 1, 1), s = D(n); t.push({ name: "Nelson Anniversary Day", date: s, type: "regional" }); } if (a === "marlborough") { const n = u(e, 9, 1, 4), s = new Date(n.getTime() + 7 * 24 * 60 * 60 * 1e3); t.push({ name: "Marlborough Anniversary Day", date: s, type: "regional" }); } if (a === "taranaki") { const n = u(e, 2, 1, 2); t.push({ name: "Taranaki Anniversary Day", date: n, type: "regional" }); } if (a === "hawkes-bay") { const n = u(e, 9, 1, 4), s = new Date(n.getTime() - 3 * 24 * 60 * 60 * 1e3); t.push({ name: "Hawke's Bay Anniversary Day", date: s, type: "regional" }); } if (a === "south-canterbury") { const n = u(e, 8, 1, 4); t.push({ name: "South Canterbury Anniversary Day", date: n, type: "regional" }); } if (a === "chatham-islands") { const n = new Date(e, 10, 30), s = D(n); t.push({ name: "Chatham Islands Anniversary Day", date: s, type: "regional" }); } return t; } function D(e) { const a = e.getDay(); let t; switch (a) { case 1: t = e; break; case 0: t = new Date(e.getTime() + 24 * 60 * 60 * 1e3); break; case 2: t = new Date(e.getTime() - 24 * 60 * 60 * 1e3); break; case 6: t = new Date(e.getTime() + 2 * 24 * 60 * 60 * 1e3); break; case 3: t = new Date(e.getTime() - 2 * 24 * 60 * 60 * 1e3); break; case 5: t = new Date(e.getTime() + 3 * 24 * 60 * 60 * 1e3); break; case 4: // Thursday - Monday is 3 days earlier default: t = new Date(e.getTime() - 3 * 24 * 60 * 60 * 1e3); break; } if (t.getDay() !== 1) throw new Error(`Expected Monday (1), got ${t.getDay()}`); return t; } function E(e, a = {}) { const t = e.getFullYear(); return b(t, a).some((s) => (s.observed || s.date).toDateString() === e.toDateString()); } function G(e = /* @__PURE__ */ new Date(), a = {}) { const t = e.getFullYear(); let n = b(t, a); n.some((i) => (i.observed || i.date) > e) || (n = [...n, ...b(t + 1, a)]); const o = n.filter((i) => (i.observed || i.date) > e); return o.length > 0 ? o[0] : null; } function I(e, a = {}) { const t = e.getDay(); return t === 0 || t === 6 ? !1 : !E(e, a); } function K(e, a, t = {}) { if (e >= a) return 0; let n = 0; const s = new Date(e); for (s.setDate(s.getDate() + 1); s < a; ) I(s, t) && n++, s.setDate(s.getDate() + 1); return n; } export { K as countBusinessDays, b as getHolidays, G as getNextHoliday, I as isBusinessDay, E as isHoliday }; //# sourceMappingURL=index.es.js.map