@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.
84 lines (82 loc) • 5.06 kB
JavaScript
import { stringOptionOrDefault } from "../../internal/typedOptions.js";
import { withZonedFields } from "../../internal/zonedWallClockOperations.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { frameInstant, frameZoned, normalizeZoneFrame } from "../../internal/zoneFrame.js";
import { DATE_TIME_FIELDS, readSetFields } from "../../internal/readSetFields.js";
import { resolveOverflow } from "../../internal/resolveOverflow.js";
import { resolveUnixEpochUnit, toUnixEpoch, unixEpochToInstant } from "../../internal/unixEpochValue.js";
// src/unix/calculate/setUnix.ts
/**
* 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
*/
function setUnix(value, fields, options) {
try {
if (!isOptionsArgument(options)) return null;
const epochUnit = resolveUnixEpochUnit(options?.epochUnit);
const frame = normalizeZoneFrame(options?.timeZone);
if (frame === null || epochUnit === null) return null;
const instant = unixEpochToInstant(value, epochUnit);
if (instant === null) return null;
const overflow = resolveOverflow(options?.overflow);
const disambiguation = stringOptionOrDefault(options?.disambiguation, "compatible");
const offset = stringOptionOrDefault(options?.offset, "prefer");
try {
const zoned = frameZoned(instant, frame);
const set = readSetFields(fields, DATE_TIME_FIELDS);
if (set === null) return null;
const result = Object.keys(set).length === 0 ? zoned : withZonedFields(zoned, set, {
overflow,
disambiguation,
offset
});
return toUnixEpoch(frameInstant(result, frame), epochUnit);
} catch {
return null;
}
} catch {
return null;
}
}
export { setUnix };