UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

53 lines (51 loc) • 2.83 kB
import { plainDateOf } from "../../internal/inputChecks.js"; import { isBusinessDate, resolveBusinessCalendar } from "../../internal/businessCalendar.js"; // src/plain/compare/isBusinessDay.ts /** * Return true when `value` is a working day in `calendar`. * * - `calendar` names the weekend days and holidays that make a date non-working. Omit it and * GMT keeps its original fixed Monday–Friday week with no holidays. * - **Pass a calendar anywhere the weekend is not Saturday–Sunday.** Much of the Middle East * is Friday–Saturday and some markets keep a one-day weekend; the no-calendar form assumes * Saturday–Sunday and is wrong for them. * - `value` and `calendar.holidays` are local dates. `calendar.timeZone` is not read: it * records which locality the calendar describes, so that a caller holding an *instant* knows * which zone to reduce it in — with `floorToZone` or `getZonedDateTimeFields` — before * asking this question. * - Returns false if `value` is not a valid ISO PlainDate, or `calendar` is not a valid * `BusinessCalendar`. Narrow a calendar first with `isValidBusinessCalendar` to tell a * misconfigured calendar apart from a genuine non-working day. * * This is the locale-agnostic complement to the locale-aware `isWeekend`: `isWeekend` resolves * weekend days per locale via `Intl.Locale.prototype.weekInfo`, whereas `isBusinessDay` uses * the weekend the caller states, or Monday–Friday when they state none. * * @param value ISO PlainDate string * @param calendar optional BusinessCalendar; defaults to Monday–Friday with no holidays * @returns true if `value` is a working day, false on invalid input * * @example isBusinessDay("2024-02-05") // true (Monday) * @example isBusinessDay("2024-02-10") // false (Saturday) * @example isBusinessDay("2024-07-04", { weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "Africa/Kigali" }) // false (holiday) * @example isBusinessDay("2024-07-05", { weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "Africa/Kigali" }) // true * @example isBusinessDay("2024-07-07", { weekend: [5, 6], holidays: [], timeZone: "Asia/Riyadh" }) // true (Sunday is a working day there) * @example isBusinessDay("2024-07-05", { weekend: [5, 6], holidays: [], timeZone: "Asia/Riyadh" }) // false (Friday is weekend there) * @example isBusinessDay("invalid") // false * @example isBusinessDay("2024-02-05", { weekend: [6, 7], holidays: [], timeZone: "Invalid/Zone" }) // false (bad calendar) */ function isBusinessDay(value, calendar) { try { const resolved = resolveBusinessCalendar(calendar); const valueParsed = plainDateOf(value); if (valueParsed === null || resolved === null) return false; try { return isBusinessDate(valueParsed, resolved); } catch { return false; } } catch { return false; } } export { isBusinessDay };