@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.
59 lines (57 loc) • 3.08 kB
JavaScript
import { addToZoned } from "../../internal/zonedWallClockOperations.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { frameInstant, frameZoned, normalizeZoneFrame } from "../../internal/zoneFrame.js";
import { isDateTimeDurationUnitName } from "../../internal/unitNames.js";
import { readDurationUnits } from "../../internal/readDurationUnits.js";
import { resolveOverflow } from "../../internal/resolveOverflow.js";
import { resolveUnixEpochUnit, toUnixEpoch, unixEpochToInstant } from "../../internal/unixEpochValue.js";
// src/unix/calculate/addUnix.ts
/**
* Add a temporal amount to a Unix epoch value and return the resulting epoch.
*
* - Converts to ZonedDateTime, adds the duration, then converts back to epoch.
* - Validates duration units and values.
* - `value` is a safe integer or a digit string (`"1706659200000"`); anything else returns null.
* - Returns null for invalid input.
* - **Limit at an offset with seconds.** The instant is placed in the offset's whole-minute zone,
* moved by its seconds, and the moved instant must be inside Temporal's range. So within the
* offset's seconds (under a minute) of the last instant Temporal supports
* (`+275760-09-13T00:00:00Z`) for an offset east of UTC, or of the first
* (`-271821-04-20T00:00:00Z`) for one west, this returns `null`. An offset to the minute has no
* such limit.
*
* @param value Unix epoch: a safe integer, or a string of optionally negative ASCII digits
* @param units Partial<Record<DateTimeDurationUnit, number>> object specifying units to add
* @param options optional: how `value` is read and how an out-of-range result day is handled
* @returns Unix epoch number after addition, or null on invalid input
*
* @example addUnix(1706659200000, { days: 1 }) // 1706745600000
* @example addUnix(1706659200, { days: 1 }, { epochUnit: "seconds" }) // 1706745600
* @example addUnix(-86400000, { days: 1 }) // 0 (Dec 31 1969 + 1 day = Jan 1 1970)
* @example addUnix(1.5, { days: 1 }) // null (not an integer epoch)
* @example addUnix("1706659200000", { days: 1 }) // 1706745600000 (digit string)
* @example addUnix(1706659200000, { days: 1 }, { timeZone: "Mars/Olympus" }) // null (unknown zone)
* @example addUnix(45870000, { days: 1 }, { timeZone: "-00:44:30" }) // 132270000 (a stored offset with seconds)
*/
function addUnix(value, units, options) {
try {
if (!isOptionsArgument(options)) return null;
const epochUnit = resolveUnixEpochUnit(options?.epochUnit);
const frame = normalizeZoneFrame(options?.timeZone);
const overflow = resolveOverflow(options?.overflow);
if (frame === null || epochUnit === null) return null;
const amounts = readDurationUnits(units, isDateTimeDurationUnitName);
if (amounts === null) return null;
const instant = unixEpochToInstant(value, epochUnit);
if (instant === null) return null;
try {
const zdt = frameZoned(instant, frame);
return toUnixEpoch(frameInstant(addToZoned(zdt, amounts, { overflow }), frame), epochUnit);
} catch {
return null;
}
} catch {
return null;
}
}
export { addUnix };