@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.01 kB
JavaScript
import { Temporal } from "./temporal.js";
import { utcDateTime } from "../regex/utc-date-time.js";
import { hasSecond60Field } from "./inputChecks.js";
// src/internal/utcInstant.ts
/**
* The check behind `isValidUtc`: the `Instant` a strict ISO 8601 UTC date-time string names, or
* `null`.
*
* The whole string must match `utcDateTime` (`…Z`, nothing after, no annotation of any kind) and
* have no second 60; Temporal then reads the instant.
*
* @param value the candidate; a value that is not a string is not a UTC string
* @returns the `Instant` Temporal reads, else `null`
*
* @example utcInstantOf("2024-03-10T12:00:00Z")?.epochMilliseconds // 1710072000000
* @example utcInstantOf("2024-03-10T12:00:00") // null
* @example utcInstantOf(null) // null
*/
function utcInstantOf(value) {
if (typeof value !== "string") return null;
if (hasSecond60Field(value) || !utcDateTime.test(value)) return null;
try {
return Temporal.Instant.from(value);
} catch {
return null;
}
}
export { utcInstantOf };