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.

33 lines (31 loc) • 1.71 kB
import { readSqlDateTime } from "../../internal/sqlDateTime.js"; // src/plain/parse/parseSqlDateTime.ts /** * Parse a SQL timestamp literal without a time zone — the `YYYY-MM-DD HH:MM:SS[.fff]` form of * standard SQL `TIMESTAMP` and MySQL/SQLite `DATETIME` columns — into a plain ISO 8601 datetime * string. * * - **Grammar:** SQL-92 §5.3 `<timestamp string>` and the ODBC `ts` escape, as `sqlDateTime` * matches: a space separator, a four-digit year `0001`–`9999` (no sign, no expanded year), * two-digit month, day, hour, minute and second (seconds are required), and an optional `.` * followed by up to 9 fraction digits. * - **Plain only.** SQL's offset-carrying `TIMESTAMP WITH TIME ZONE` literal has its own reader, * `parseSqlOffsetDateTime`. * - The fraction is written as `Temporal.PlainDateTime#toString` writes it: trailing zeros are * dropped (`.500` → `.5`, `.000` → none), and a `.` with no digits is dropped. `Temporal.PlainDateTime.from` * proves the value is a real calendar date and time. * * @param value SQL timestamp literal (e.g. "2024-03-15 14:30:00") * @returns plain ISO 8601 datetime string, or "" on invalid input * * @example parseSqlDateTime("2024-03-15 14:30:00") // "2024-03-15T14:30:00" * @example parseSqlDateTime("2024-03-15 14:30:00.5") // "2024-03-15T14:30:00.5" * @example parseSqlDateTime("2024-03-15 14:30") // "" (seconds are required) * @example parseSqlDateTime("0000-01-01 00:00:00") // "" (year outside 0001–9999) * @example parseSqlDateTime("2024-03-15T14:30:00") // "" (wrong separator) * @example parseSqlDateTime("not a date") // "" */ function parseSqlDateTime(value) { return readSqlDateTime(value); } export { parseSqlDateTime };