@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.
65 lines (63 loc) • 3.59 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { DATE_TIME_FIELDS, readSetFields } from "../../internal/readSetFields.js";
import { resolveOverflow } from "../../internal/resolveOverflow.js";
import { isValidUtc } from "../validate/isValidUtc.js";
// src/utc/calculate/setUtc.ts
/**
* Return a UTC Instant string with the given `fields` set on `value`.
*
* - Converts to ZonedDateTime (fixed timeZone "UTC"), wraps
* `Temporal.ZonedDateTime.prototype.with()` (resolving every supplied field in a single atomic
* overflow pass), then converts back to an Instant. This is the safe alternative to composing
* `addUtc()` calls field-by-field — see `setDate`'s doc for why order-independent field
* resolution matters.
* - `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 "", where Temporal would convert or
* truncate it. A bigint, `NaN` or an infinity returns "" too. A number too large for its
* field follows `overflow`. A month or day below 1, or a date outside Temporal's range, returns
* "".
* - An own key that is not one of these fields, such as a misspelt `yaer`, returns "". 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 "", as Temporal's `with` refuses one. One whose
* prototype has been replaced with `Object.prototype`, `null` or `Function.prototype` sets
* nothing instead.
* - There are no `disambiguation` or `offset` options (removed in 1.16.0): a UTC instant's wall
* clock is never ambiguous and its offset is always +00:00, so neither had anything to act on.
* - Returns "" for invalid input.
*
* @param value ISO UTC datetime string (e.g. "2024-03-10T12:00:00Z")
* @param fields Partial<Temporal.ZonedDateTimeLike> object (excluding calendar/timeZone/offset) specifying fields to set
* @param options How an out-of-range result is handled
* @returns UTC Instant string with fields set, or "" on invalid input
*
* @example setUtc("2024-03-10T12:00:00Z", { hour: 9 }) // "2024-03-10T09:00:00Z"
* @example setUtc("2024-01-31T12:00:00Z", { month: 2 }) // "2024-02-29T12:00:00Z" (constrain clamps to the last valid day)
* @example setUtc("2024-01-31T12:00:00Z", { month: 2 }, { overflow: "reject" }) // ""
* @example setUtc("2024-03-10T12:00:00Z", {}) // "2024-03-10T12:00:00Z" (empty fields object is a no-op)
* @example setUtc("2024-03-10T12:00:00Z", { hour: "9" }) // "" (not converted)
* @example setUtc("invalid", { hour: 9 }) // ""
*/
function setUtc(value, fields, options) {
try {
if (!isOptionsArgument(options)) return "";
if (!isValidUtc(value)) return "";
const overflow = resolveOverflow(options?.overflow);
try {
const zoned = Temporal.Instant.from(value).toZonedDateTimeISO("UTC");
const set = readSetFields(fields, DATE_TIME_FIELDS);
if (set === null) return "";
return (Object.keys(set).length === 0 ? zoned : zoned.with(set, { overflow })).toInstant().toString();
} catch {
return "";
}
} catch {
return "";
}
}
export { setUtc };