@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
26 lines (25 loc) • 1.35 kB
TypeScript
import type { DateTimeDurationUnit, Overflow } from "../../types/index.js";
/**
* Subtract a temporal amount from a Unix epoch value and return the resulting epoch.
*
* - Converts to ZonedDateTime, subtracts the duration, then converts back to epoch.
* - Validates duration units and values.
* - Returns null for invalid input.
*
* `overflow` ("constrain" (default) | "reject") controls out-of-range results, e.g. subtracting
* 1 month from Mar 31: "constrain" clamps to Feb 29/28, "reject" throws (resulting in null).
*
* @param value Unix timestamp (number)
* @param units Partial<Record<DateTimeDurationUnit, number>> object specifying units to subtract
* @param options optional: epochUnit ("seconds" | "milliseconds"), timeZone (IANA), overflow ("constrain" | "reject")
* @returns Unix epoch number after subtraction, or null on invalid input
*
* @example subtractUnix(1706745600000, { days: 1 }) // 1706659200000
* @example subtractUnix(1706745600, { days: 1 }, { epochUnit: "seconds" }) // 1706659200
* @example subtractUnix(0, { days: 1 }) // -86400000 (Jan 1 1970 - 1 day = Dec 31 1969)
*/
export declare function subtractUnix(value: number, units: Partial<Record<DateTimeDurationUnit, number>>, options?: {
epochUnit?: "seconds" | "milliseconds";
timeZone?: string;
overflow?: Overflow;
}): number | null;