@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.
30 lines (28 loc) • 1.02 kB
JavaScript
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
// src/zoned/parse/getZonedOffset.ts
/**
* Extract the UTC offset from an ISO 8601 zoned datetime string.
*
* - Returns the offset in Temporal's ±HH:MM string form.
* - This is a value-taking accessor (per J0b's `get/` rule), so it lives in
* `parse/` alongside `parseTimeZoneFromZoned`, not `get/`.
* - Returns "" for invalid input.
*
* @param value zoned ISO 8601 datetime string
* @returns offset string (e.g. "-04:00"), or "" on invalid input
*
* @example getZonedOffset("2024-02-29T12:34:56.789+00:00[UTC]") // "+00:00"
* @example getZonedOffset("2024-07-15T12:00:00+05:30[Asia/Kolkata]") // "+05:30"
* @example getZonedOffset("2024-05-15T12:00:00+05:45[Asia/Kathmandu]") // "+05:45"
* @example getZonedOffset("invalid") // ""
*/
function getZonedOffset(value) {
const zonedDateTime = zonedDateTimeOf(value);
if (zonedDateTime === null) return "";
try {
return zonedDateTime.offset;
} catch {
return "";
}
}
export { getZonedOffset };