UNPKG

@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.

51 lines (49 loc) • 3.17 kB
import { isoStringBody } from "../../internal/isoStringBody.js"; import { isValidTimeZone } from "../validate/isValidTimeZone.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { optionOrDefault } from "../../internal/optionOrDefault.js"; import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js"; import { isValidDateTime } from "../../plain/validate/isValidDateTime.js"; // src/zoned/convert/convertPlainDateTimeToZoned.ts /** * Attach the specified `timeZone` to a plain datetime string and return a zoned ISO 8601 datetime string. * * - Combines plain datetime with timezone to create ZonedDateTime. * - There is no `offset` option (removed in 1.16.0): Temporal `PlainDateTime#toZonedDateTime` reads * only `disambiguation`, and a plain datetime has no UTC offset for `offset` to act on. * - **Output is cut to milliseconds by default**, so microseconds and nanoseconds in `value` are * dropped. Pass `{ smallestUnit: "nanoseconds" }` to keep every digit. * - Returns "" for invalid input. * - An offset with seconds (`-00:44:30`) returns `""`. A written zone cannot carry seconds (RFC * 9557 §4.1), so the result could not name it. Pass the IANA name, or call `resolveLocal` for the * instant the wall time names at that offset. * * @param value plain datetime string (e.g. "2024-02-29T14:30:45") * @param timeZone IANA name or a UTC offset to the minute (what `isValidTimeZone` accepts) * @param optionsArg optional settings for the precision of the output string and for resolving a DST gap or overlap * @returns zoned ISO 8601 datetime string or "" when invalid * * @example convertPlainDateTimeToZoned("2024-02-29T14:30:45", "Asia/Tokyo") // "2024-02-29T14:30:45.000+09:00[Asia/Tokyo]" * @example convertPlainDateTimeToZoned("2024-02-29T14:30:45.123456789", "UTC") // "2024-02-29T14:30:45.123+00:00[UTC]" * @example convertPlainDateTimeToZoned("2024-02-29T14:30:45.123456789", "UTC", { smallestUnit: "nanoseconds" }) // "2024-02-29T14:30:45.123456789+00:00[UTC]" * @example convertPlainDateTimeToZoned("invalid", "Asia/Kolkata") // "" * @example convertPlainDateTimeToZoned("2024-10-06T02:30:00", "Australia/Sydney", { disambiguation: "earlier" }) // "2024-10-06T01:30:00.000+10:00[Australia/Sydney]" (spring-forward gap) * @example convertPlainDateTimeToZoned("2024-10-27T01:30:00", "Europe/London", { disambiguation: "later" }) // "2024-10-27T01:30:00.000+00:00[Europe/London]" (fall-back overlap) * @example convertPlainDateTimeToZoned("2024-10-06T02:30:00", "Australia/Sydney", { disambiguation: "reject" }) // "" */ function convertPlainDateTimeToZoned(value, timeZone, optionsArg) { try { if (!isOptionsArgument(optionsArg)) return ""; if (!isValidDateTime(value) || !isValidTimeZone(timeZone)) return ""; const disambiguation = optionOrDefault(optionsArg?.disambiguation, "compatible"); const options = { smallestUnit: optionOrDefault(optionsArg?.smallestUnit, "milliseconds") }; try { return zonedDateTimeFrom(`${isoStringBody(value)}[${timeZone}]`, { disambiguation }).toString(options); } catch { return ""; } } catch { return ""; } } export { convertPlainDateTimeToZoned };