@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.
93 lines • 5.73 kB
TypeScript
import { Temporal } from "../../internal/temporal.js";
import { Disambiguation } from "../../types/disambiguation.js";
import { Offset } from "../../types/offset.js";
import { Overflow } from "../../types/overflow.js";
import { UnixUnit } from "../validate/isValidUnixUnit.js";
import "../../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` and/or `nanosecond`; omitted fields keep their current value. An
* object with no field set, such as `{}` or `{ year: undefined }`, is a no-op.
* - Values are not converted: `monthCode` must be a string and every other field an integer number.
* `null`, a numeric string, a boolean or a fraction returns null, where Temporal would convert or
* truncate it. A bigint, `NaN` or an infinity returns null too. A number too large for its
* field follows `overflow`. A month or day below 1, or a date outside Temporal's range, returns
* null.
* - An own key that is not one of these fields, such as a misspelt `yaer`, returns null. So does an
* era, an `offset`, or a `calendar` or `timeZone` property, whether own, inherited or a getter. A
* field is read with a property get, as Temporal reads it, so an inherited or getter field is
* set.
* - A Temporal date or time object returns null, as Temporal's `with` refuses one. One whose
* prototype has been replaced with `Object.prototype`, `null` or `Function.prototype` sets
* nothing instead.
* - `value` is a safe integer or a digit string (`"1710072000000"`); 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 fields Partial<Temporal.ZonedDateTimeLike> object (excluding calendar/timeZone/offset) specifying fields to set
* @param options optional: how `value` is read, and how an out-of-range field or a skipped or repeated wall-clock time is resolved
* @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(1729992600000, { minute: 45 }, { timeZone: "Europe/London" }) // 1729993500000 (the second 01:30 keeps +00:00)
* @example setUnix(1729992600000, { minute: 45 }, { timeZone: "Europe/London", offset: "ignore" }) // 1729989900000 (re-resolved: the earlier 01:45)
* @example setUnix(1710072000000, { hour: "9" }, { timeZone: "UTC" }) // null (not converted)
* @example setUnix(NaN, { hour: 9 }) // null
*/
export declare function setUnix(value: number | string, fields: Omit<Temporal.ZonedDateTimeLike, "calendar" | "timeZone" | "offset" | "era" | "eraYear">, options?: {
/**
* The unit the epoch values are counted in: `"seconds"` or `"milliseconds"`, singular or
* plural. Any other value returns `null`. The result is in the same unit.
*
* @defaultValue `"milliseconds"`
*/
epochUnit?: UnixUnit;
/**
* The time zone the fields are set in: an IANA name, a UTC offset (a time zone identifier such
* as `+05:30`, `+0530` or `-08`, or a stored offset `±HH:MM[:SS]`, what `getTimeZoneOffset`
* returns), or `"local"` for the system time zone. An unknown zone returns `null`.
*
* @defaultValue `"UTC"`
*/
timeZone?: string;
/**
* How an out-of-range field is handled, such as `month: 2` on a value whose day is 31.
* `"constrain"` clamps to the nearest valid value, and `"reject"` returns `null`.
*
* @defaultValue `"constrain"`, Temporal's default.
*/
overflow?: Overflow;
/**
* How a wall-clock time that a zone transition skips or repeats is resolved, once `offset` does
* not settle it. `"compatible"` takes the later time in a gap and the earlier of a repeat,
* `"earlier"` and `"later"` take that side, and `"reject"` returns `null`.
*
* @defaultValue `"compatible"`, Temporal's default.
*/
disambiguation?: Disambiguation;
/**
* How the source's UTC offset is weighed against the new wall-clock time, as in
* `Temporal.ZonedDateTime.prototype.with`. `"prefer"` keeps it while it is still valid, so a
* repeated hour stays on the same side of a fall-back, and `"use"` keeps it always. `"ignore"`
* re-resolves the wall clock with `disambiguation`, and `"reject"` returns `null` when the
* offset is no longer valid.
*
* @defaultValue `"prefer"`, Temporal's default.
*/
offset?: Offset;
}): number | null;