@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
36 lines (35 loc) • 2.33 kB
TypeScript
import { Temporal } from "@js-temporal/polyfill";
import type { Disambiguation, Offset, Overflow } from "../../types/index.js";
/**
* Return a Unix epoch value with the given `fields` set on `value`, interpreted in `timeZone`.
*
* - Converts to ZonedDateTime, wraps `Temporal.ZonedDateTime.prototype.with()` (resolving every
* supplied field in a single atomic overflow pass), then converts back to epoch. This is the
* safe alternative to composing `addUnix()` calls field-by-field — see `setZoned`'s doc for
* why order-independent field resolution matters, and why `disambiguation`/`offset` require
* `.with()` rather than arithmetic.
* - `fields` may set any of `year`, `month`, `monthCode`, `day`, `hour`, `minute`, `second`,
* `millisecond`, `microsecond`, `nanosecond`, `era`, and/or `eraYear`; omitted fields keep
* their current value. An empty object is a no-op.
* - `overflow` ("constrain" (default) | "reject") controls out-of-range results, e.g. setting
* `month: 2` on a value whose `day` is 31: "constrain" clamps to Feb 29/28, "reject" throws
* (resulting in null).
* - Returns null for invalid input.
*
* @param value Unix timestamp (number)
* @param fields Partial<Temporal.ZonedDateTimeLike> object (excluding calendar/timeZone/offset) specifying fields to set
* @param options optional: epochUnit ("seconds" | "milliseconds"), timeZone (IANA), overflow ("constrain" | "reject"), disambiguation ("compatible" | "earlier" | "later" | "reject"), offset ("prefer" | "use" | "ignore" | "reject", default "ignore")
* @returns Unix epoch number with fields set, or null on invalid input
*
* @example setUnix(1710072000000, { hour: 9 }, { timeZone: "UTC" }) // 1710061200000 (2024-03-10T09:00:00Z)
* @example setUnix(1706659200000, { year: 2025 }, { timeZone: "UTC" }) // 1738281600000 (2025-01-31T00:00:00Z)
* @example setUnix(1706659200000, {}, { timeZone: "UTC" }) // 1706659200000 (empty fields object is a no-op)
* @example setUnix(NaN, { hour: 9 }) // null
*/
export declare function setUnix(value: number, fields: Omit<Temporal.ZonedDateTimeLike, "calendar" | "timeZone" | "offset">, options?: {
epochUnit?: "seconds" | "milliseconds";
timeZone?: string;
overflow?: Overflow;
disambiguation?: Disambiguation;
offset?: Offset;
}): number | null;