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.

39 lines (37 loc) • 1.33 kB
import { Temporal } from "./temporal.js"; import { epcisEventTime, epcisTimeZoneOffset } from "../regex/epcis.js"; import { parseUtcOffsetNanoseconds } from "./utcOffsetString.js"; // src/internal/epcisTime.ts /** * The check behind `isValidEpcisEventTime`: an EPCIS `eventTime`, an ISO 8601 instant with an * explicit offset. * * @param value the candidate; a value that is not a string is not an event time * @returns whether it is a real event time * * @example isEpcisEventTime("2024-03-15T12:30:45.123Z") // true * @example isEpcisEventTime("2024-02-30T12:30:45Z") // false */ function isEpcisEventTime(value) { if (typeof value !== "string" || !epcisEventTime.test(value)) return false; try { Temporal.Instant.from(value); return true; } catch { return false; } } /** * The check behind `isValidEpcisTimeZoneOffset`: an EPCIS `eventTimeZoneOffset`, `+HH:MM` or * `-HH:MM`. * * @param value the candidate; a value that is not a string is not an offset * @returns whether it is a real EPCIS offset * * @example isEpcisTimeZoneOffset("+05:30") // true * @example isEpcisTimeZoneOffset("+24:00") // false */ function isEpcisTimeZoneOffset(value) { return typeof value === "string" && epcisTimeZoneOffset.test(value) && parseUtcOffsetNanoseconds(value) !== null; } export { isEpcisEventTime, isEpcisTimeZoneOffset };