@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.
75 lines (73 loc) • 4.6 kB
JavaScript
import { stringOptionOrDefault } from "../../internal/typedOptions.js";
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { resolveOverflow } from "../../internal/resolveOverflow.js";
import { setZonedFields } from "../../internal/setZonedFields.js";
// src/zoned/calculate/setZoned.ts
/**
* Return a zoned ISO 8601 datetime string with the given `fields` set on `value`.
*
* - Wraps `Temporal.ZonedDateTime.prototype.with()`, which resolves every supplied field in a
* single atomic overflow pass. This is the safe alternative to composing `addZoned()` calls
* field-by-field, and it is the only construction path that can reproduce
* `startOfZoned`'s disambiguation-plus-offset handling — `addZoned()` has no `offset` control
* equivalent to `.with()`'s, because Temporal's `ZonedDateTime.prototype.add()` doesn't accept
* `disambiguation`/`offset` at all.
* - `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.
* - `calendar`, `timeZone`, and `offset` are deliberately excluded from `fields` — this function
* only sets date/time components, never the zone or calendar identity, and `offset` is
* controlled separately via `options.offset`.
* - **Compatibility:** before 1.16.0 `offset` defaulted to `"ignore"`. Pass `{ offset: "ignore" }`
* to keep that resolution.
* - Returns "" for invalid input.
*
* @param value zoned ISO 8601 datetime string
* @param fields Partial<Temporal.ZonedDateTimeLike> object (excluding calendar/timeZone/offset) specifying fields to set
* @param options optional settings for resolving an out-of-range date and the new wall-clock time
* @returns zoned ISO 8601 string with fields set, or "" on invalid input
*
* @example setZoned("2024-03-10T12:00:00+09:00[Asia/Tokyo]", { hour: 9 }) // "2024-03-10T09:00:00+09:00[Asia/Tokyo]"
* @example setZoned("2024-01-31T12:00:00+05:30[Asia/Kolkata]", { month: 2 }) // "2024-02-29T12:00:00+05:30[Asia/Kolkata]" (constrain clamps to the last valid day)
* @example setZoned("2024-01-31T12:00:00+05:30[Asia/Kolkata]", { month: 2 }, { overflow: "reject" }) // ""
* @example setZoned("2024-03-10T12:00:00+09:00[Asia/Tokyo]", {}) // "2024-03-10T12:00:00+09:00[Asia/Tokyo]" (empty fields object is a no-op)
* @example setZoned("2024-10-27T01:45:00+00:00[Europe/London]", { minute: 0 }, { disambiguation: "reject" }) // "2024-10-27T01:00:00+00:00[Europe/London]" (offset defaults to "prefer": the source's +00:00 is still valid, so it is kept and "reject" never fires)
* @example setZoned("2024-10-27T01:45:00+00:00[Europe/London]", { minute: 0 }, { disambiguation: "reject", offset: "ignore" }) // "" (offset "ignore" re-resolves the repeated 01:00, and "reject" throws)
* @example setZoned("2024-03-10T12:00:00+09:00[Asia/Tokyo]", { hour: "9" }) // "" (not converted)
* @example setZoned("invalid", { hour: 9 }) // ""
*/
function setZoned(value, fields, options) {
try {
if (!isOptionsArgument(options)) return "";
const zoned = zonedDateTimeOf(value);
if (zoned === null) return "";
const overflow = resolveOverflow(options?.overflow);
const disambiguation = stringOptionOrDefault(options?.disambiguation, "compatible");
const offset = stringOptionOrDefault(options?.offset, "prefer");
try {
return setZonedFields(zoned, fields, {
overflow,
disambiguation,
offset
})?.toString() ?? "";
} catch {
return "";
}
} catch {
return "";
}
}
export { setZoned };