@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
31 lines (30 loc) • 1.87 kB
TypeScript
import { Temporal } from "@js-temporal/polyfill";
import type { DateTimeDurationUnit, RoundingOptions } from "../../types/index.js";
/**
* Return the difference between two Unix timestamps measured in the given unit.
*
* - Uses Temporal.Instant.until() to calculate the difference.
* - Supports single unit or array of units.
* - Returns null for invalid input.
*
* `smallestUnit`, `roundingIncrement`, and `roundingMode` control optional rounding of the result,
* per Temporal's DifferenceOptions — e.g. `{ smallestUnit: "hour", roundingMode: "halfExpand" }`
* rounds the difference to the nearest hour before extracting the requested unit.
* - When `units` is an array, `smallestUnit` must not be coarser than the largest unit in the
* array (e.g. `["day", "hour"]` with `smallestUnit: "week"`) — this combination is rejected by
* Temporal and returns null, same as other invalid input.
*
* @param value1 first Unix timestamp
* @param value2 second Unix timestamp
* @param units DateTimeDurationUnit | DateTimeDurationUnit[] to measure the difference
* @param options optional: epochUnit ("seconds" | "milliseconds"), timeZone (IANA), smallestUnit, roundingIncrement, roundingMode (Temporal.DifferenceOptions rounding controls)
* @returns numeric difference in the requested unit, or null on invalid input
*
* @example diffUnix(1706745600000, 1706659200000, "day") // 1
* @example diffUnix(1706745600, 1706659200, "day", { epochUnit: "seconds" }) // 1
* @example diffUnix(0, -86400000, "day") // 1 (Jan 1 1970 - Dec 31 1969 = 1 day)
*/
export declare function diffUnix(value1: number, value2: number, units: DateTimeDurationUnit | DateTimeDurationUnit[], options?: {
epochUnit?: "seconds" | "milliseconds";
timeZone?: string;
} & RoundingOptions<Temporal.DateTimeUnit>): number | Record<DateTimeDurationUnit, number> | null;