@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.
107 lines • 7.49 kB
TypeScript
import { TwoDigitYearOptions } from "../../types/two-digit-year.js";
/**
* Parse a datetime string against a caller-supplied token pattern (e.g.
* `"dd/MM/yyyy HH:mm:ss"`, `"dd-MMM-yyyy h:mm a"`) and return it as an
* ISO `PlainDateTime` string.
*
* - **Decoding, not display.** This is for consuming a *known, fixed*
* producer format — a CSV column, a legacy API field, a partially-typed
* form value — not for generating locale-correct output. For display,
* use `formatDateTime`/`formatDateToParts`, which order fields per
* locale instead of hard-coding a field order (a token *formatter*,
* the inverse of this function, is deliberately out of scope for GMT).
* - Accepts the full combined date + time token set (unlike
* `parseDateWithPattern`/`parseTimeWithPattern`, which each reject the
* other's tokens).
*
* ### Token table
* | Token | Field | Width | Range | Notes |
* |---|---|---|---|---|
* | `yyyy` | year | 4 digits | 0000–9999 | |
* | `yy` | year | 2 digits | 00–99 | Needs `options.yearWindow`; returns `""` without it |
* | `MM` | month | 2 digits | 01–12 | |
* | `M` | month | 1-2 digits | 1–12 | |
* | `MMMM` | month name (long) | locale | — | Reads the format-context name ("марта") or the stand-alone name ("март"); UTS #35 Part 4, Parsing Dates and Times |
* | `MMM` | month name (short) | locale | — | Reads either context, as `MMMM` ("März" and "Mär" in `de`) |
* | `LL` | month | 2 digits | 01–12 | Stand-alone context; read as `MM` |
* | `L` | month | 1-2 digits | 1–12 | Stand-alone context; read as `M` |
* | `LLLL` | month name (long) | locale | — | Same names as `MMMM` |
* | `LLL` | month name (short) | locale | — | Same names as `MMM` |
* | `dd` | day | 2 digits | 01–31 | |
* | `d` | day | 1-2 digits | 1–31 | |
* | `HH` | hour (24h) | 2 digits | 00–23 | |
* | `H` | hour (24h) | 1-2 digits | 0–23 | |
* | `hh` | hour (12h) | 2 digits | 01–12 | needs `a` to resolve to 24h — see below |
* | `h` | hour (12h) | 1-2 digits | 1–12 | same |
* | `mm` | minute | 2 digits | 00–59 | |
* | `m` | minute | 1-2 digits | 0–59 | |
* | `ss` | second | 2 digits | 00–59 | |
* | `s` | second | 1-2 digits | 0–59 | |
* | `SSS` | millisecond | 3 digits | 000–999 | |
* | `EEEE` | weekday name (long) | locale | — | consumed, NOT cross-validated against the date (see below) |
* | `EEE` | weekday name (short) | locale | — | same |
* | `a` | meridiem | locale | — | `getLocaleMeridiems(locale)` → `[AM-label, PM-label]` |
* | `GGGG` | era name (long) | locale | — | BCE label ⇒ final year = 1 − parsed year |
* | `GG` | era name (short) | locale | — | same resolution |
*
* - Literal text follows the Unicode UTS #35 date format pattern rules.
* Text in `'single quotes'` is a literal, letters included. Two
* adjacent quotes `''` are one literal `'`, inside or outside quoted
* text: `"yyyy''MM"` reads `"2024'03"`, and `"'o''clock'"` reads
* `"o'clock"`. A quote that never closes makes the pattern malformed
* (`""`). Any other character that is not a letter (`/ - , space :`,
* digits and so on) is a literal with no quoting.
* - `MMMM`/`MMM`/`LLLL`/`LLL`/`EEEE`/`EEE`/`a`/`GGGG`/`GG` are locale-aware. If
* `locale` is omitted (or an empty list) and the pattern uses any of
* them, GMT reads English names in `"en"`, the language with no
* region, rather than returning `""`. CLDR gives `"en"` the same names
* as `"en-US"`. Pass `locale` for any other language.
* - `EEEE`/`EEE` are matched against the locale's weekday names but the
* result is **not** cross-checked against the constructed date — e.g.
* `"Monday, 2024-03-15 14:00"` parses successfully against
* `"EEEE, yyyy-MM-dd HH:mm"` even though 2024-03-15 is actually a
* Friday. This is a deliberate scope limit (no second
* weekday-index-to-ISO-dayOfWeek mapping layer).
* - `h`/`hh` resolve to 24-hour using a matched `a` token: if the label
* is the PM label and hour !== 12, add 12; if the AM label and hour
* === 12, set hour to 0. With no `a` token in the pattern at all, the
* pattern is still valid but the hour stays ambiguous — GMT resolves
* it as if AM had matched (12 → 0, otherwise unchanged).
* - Adjacent variable-width numeric tokens with no literal separator
* (e.g. `"dMyyyy"`) are inherently ambiguous: each becomes a greedy
* `\d{1,2}`/etc. in sequence, so the split between fields depends on
* regex backtracking, not on any documented rule. Prefer zero-padded
* tokens (`MM`, `dd`) or an explicit separator for reliable parsing.
* - A shape-valid match does not by itself prove a real date/time:
* extracted fields are always handed to
* `Temporal.PlainDateTime.from(fields, { overflow: "reject" })` — the
* regex only proves the shape, Temporal proves the value is real.
* - Fields absent from `pattern` default the way `Temporal.PlainDateTime.from`
* defaults an omitted field (time fields default to `0`; `year`/`month`/`day`
* are still required for a valid result).
* - A `yy` token needs the caller's hundred-year window,
* `options.yearWindow`: no standard says which century a two-digit
* year belongs to, and a cut-off built into a library is right today
* and wrong later. Without a valid window a `yy` pattern returns `""`.
* A pattern with no `yy` never reads the option, so a `"rolling"`
* window reads no clock for it. A two-digit year is a legacy form; a
* four-digit year is the one to ask a data source for.
*
* @param value The string to decode (e.g. "15/03/2024 14:30:00")
* @param pattern The token pattern describing `value`'s shape (e.g. "dd/MM/yyyy HH:mm:ss")
* @param locale Optional BCP 47 locale tag, or a preference list of tags, for name-based tokens (default "en")
* @param options The hundred-year window a `yy` token resolves in
* @returns ISO `PlainDateTime` string, or "" on no match, malformed pattern, or invalid input
*
* @example parseDateTimeWithPattern("15/03/2024 14:30:00", "dd/MM/yyyy HH:mm:ss") // "2024-03-15T14:30:00"
* @example parseDateTimeWithPattern("15-Mar-2024 02:30 PM", "dd-MMM-yyyy hh:mm a") // "2024-03-15T14:30:00"
* @example parseDateTimeWithPattern("15 Mar '24 at 14:30", "d MMM ''yy 'at' HH:mm", undefined, { yearWindow: 2000 }) // "2024-03-15T14:30:00" ('' is one literal quote)
* @example parseDateTimeWithPattern("99-03-15 14:30", "yy-MM-dd HH:mm", undefined, { yearWindow: 1950 }) // "1999-03-15T14:30:00" (1950–2049 window)
* @example parseDateTimeWithPattern("24-03-15 14:30", "yy-MM-dd HH:mm", undefined, { yearWindow: "rolling" }) // "2024-03-15T14:30:00" (while the current UTC year is 1975–2074)
* @example parseDateTimeWithPattern("24-03-15 14:30", "yy-MM-dd HH:mm") // "" (yy with no window)
* @example parseDateTimeWithPattern("03/15/2024 14:30:00", "MM/dd/yyyy HH:mm:ss", undefined, { yearWindow: 2000 }) // "2024-03-15T14:30:00" (US order; yyyy ignores the window)
* @example parseDateTimeWithPattern("31/02/2024 14:30:00", "dd/MM/yyyy HH:mm:ss") // "" (shape-valid, not a real date)
* @example parseDateTimeWithPattern("not a date", "dd/MM/yyyy HH:mm:ss") // ""
* @example parseDateTimeWithPattern("19 mai 2024 10:20", "d MMMM yyyy HH:mm", ["fr-FR", "en-US"]) // "2024-05-19T10:20:00"
*/
export declare function parseDateTimeWithPattern(value: string, pattern: string, locale?: string | string[], options?: TwoDigitYearOptions): string;