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.

29 lines (27 loc) • 1.09 kB
import { Temporal } from "./temporal.js"; import { rfc3339DateTime } from "../regex/rfc-3339.js"; // src/internal/rfc3339DateTime.ts /** * The core of `parseRfc3339` and `isValidRfc3339DateTime`: the zoned date-time an RFC 3339 * `date-time` names. Never throws. * * @param value an RFC 3339 date-time * @returns an ISO 8601 zoned string, or `""` when `value` is not a valid RFC 3339 date-time * * @example readRfc3339("2024-03-15T12:30:45Z") // "2024-03-15T12:30:45+00:00[+00:00]" * @example readRfc3339("2024-02-30T12:30:45Z") // "" */ function readRfc3339(value) { if (typeof value !== "string") return ""; const match = rfc3339DateTime.exec(value); if (match === null) return ""; const [, year, month, day, hour, minute, second, fraction, offsetToken] = match; const offset = offsetToken === "Z" || offsetToken === "z" ? "+00:00" : offsetToken; const isoDateTime = `${year}-${month}-${day}T${hour}:${minute}:${second}${fraction ?? ""}`; try { return Temporal.PlainDateTime.from(isoDateTime).toZonedDateTime(offset).toString(); } catch { return ""; } } export { readRfc3339 };