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.

26 lines (24 loc) • 931 B
import { Temporal } from "./temporal.js"; import { sqlDateTime } from "../regex/sql-date-time.js"; // src/internal/sqlDateTime.ts /** * The shared core of `parseSqlDateTime` and `isValidSqlDateTime`: read a SQL timestamp literal * without a time zone and return it as a plain ISO 8601 datetime string, or `""` when it is not * one. Never throws. * * @param value - The text to read. * @returns `YYYY-MM-DDTHH:MM:SS[.f]`, or `""` for any other value. * @example readSqlDateTime("2024-03-15 14:30:00.500") // "2024-03-15T14:30:00.5" * @example readSqlDateTime("2024-02-30 14:30:00") // "" * @example readSqlDateTime(42) // "" */ function readSqlDateTime(value) { if (typeof value !== "string") return ""; if (!sqlDateTime.test(value)) return ""; try { return Temporal.PlainDateTime.from(value.replace(" ", "T").replace(/\.$/, "")).toString(); } catch { return ""; } } export { readSqlDateTime };