@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.
33 lines (31 loc) • 1.85 kB
JavaScript
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
import { isValidAmount } from "../../internal/isValidAmount.js";
import { adjustZonedBusinessDays } from "../../internal/adjustZonedBusinessDays.js";
// src/zoned/calculate/subtractZonedBusinessDays.ts
/**
* Return a zoned ISO 8601 datetime string with `amount` business days subtracted from `value`.
*
* - Uses a fixed Monday–Friday business week (no locale awareness). Saturday–Sunday is
* CLDR's world-default weekend; ISO 8601 defines no weekend.
* - Has no calendar parameter, so no other weekend and no holidays. It is not the
* zoned form of the plain `subtractBusinessDays`, which takes a `BusinessCalendar`.
* - Saturday and Sunday are skipped during the count.
* - Preserves the original time component through the operation.
* - Returns "" on invalid input.
*
* @param value ISO 8601 zoned datetime string
* @param amount number of business days to subtract
* @returns Zoned ISO 8601 datetime string after subtracting business days, or "" on invalid input
*
* @example subtractZonedBusinessDays("2024-03-18T14:30:00+02:00[Africa/Johannesburg]", 1) // "2024-03-15T14:30:00+02:00[Africa/Johannesburg]"
* @example subtractZonedBusinessDays("2024-03-18T14:30:00+02:00[Africa/Johannesburg]", 2) // "2024-03-14T14:30:00+02:00[Africa/Johannesburg]"
* @example subtractZonedBusinessDays("2024-03-18T14:30:00+02:00[Africa/Johannesburg]", 0) // "2024-03-18T14:30:00+02:00[Africa/Johannesburg]"
* @example subtractZonedBusinessDays("invalid", 1) // ""
*/
function subtractZonedBusinessDays(value, amount) {
const zoned = zonedDateTimeOf(value);
if (zoned === null || !isValidAmount(amount)) return "";
if (amount === 0) return adjustZonedBusinessDays(zoned, 1, 0);
return adjustZonedBusinessDays(zoned, -1, Math.abs(amount));
}
export { subtractZonedBusinessDays };