@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.
97 lines • 5.68 kB
TypeScript
import { EdiKind, EdiStandard } from "./ediGrammar.js";
/**
* Read a single EDI date/time value against a format code of one kind, as one ISO 8601 string:
* the reader behind every public parser of a date, a time, a local date-time and a date-time
* with an offset.
*
* - The code must be one of `kind` in its standard's table (`ediGrammar.ts`); a code of another
* kind, a period or range code and a code that is not read return `""`.
* - The value is tested against the code's grammar, its digits are read into a field bag, and
* the bag goes through `Temporal.PlainDate.from` / `PlainTime.from` / `PlainDateTime.from` with
* `overflow: "reject"`: the grammar proves shape, Temporal proves the date is real.
* - A date is `YYYY-MM-DD`, a time `HH:MM:SS` and a local date-time `YYYY-MM-DDTHH:MM:SS`.
* Seconds are always written, `00` for a mask without them.
* - A date-time with an offset (`205`, `208`; `303` and `304`, whose `ZZZ` is `[+-]HH`, `UTC` or
* `GMT`) is the wall clock as written with its offset, `YYYY-MM-DDTHH:MM:SS±HH:MM`. The wall
* clock and the offset are joined as an extended ISO string, read by `toOffsetInstant` and
* written by `fromOffsetInstant`; no offset digit is moved by hand.
* - Never throws: any failure is `""`.
*
* @param standard which element the code belongs to
* @param kind the kind of value the calling function reads
* @param code the 2379 or 1250 format code
* @param value the element value, after the interchange has been unescaped
* @returns the value as one ISO 8601 string, or `""`
*
* @example readEdiValue("edifact", "date", "102", "20240615") // "2024-06-15"
* @example readEdiValue("edifact", "dateTime", "203", "202406151430") // "2024-06-15T14:30:00"
* @example readEdiValue("edifact", "offsetDateTime", "303", "202406151430+02") // "2024-06-15T14:30:00+02:00"
* @example readEdiValue("x12", "date", "DB", "06152024") // "2024-06-15"
* @example readEdiValue("edifact", "date", "203", "202406151430") // "" (a date-time code)
* @example readEdiValue("edifact", "date", "102", "20230229") // "" (2023 has no 29 February)
*/
export declare function readEdiValue(standard: EdiStandard, kind: EdiKind, code: string, value: string): string;
/**
* Read an EDI period or range against a format code of one kind, as its two ends: the reader
* behind every public parser of a period (UNTDID 2379) and a range (X12 1250).
*
* - The code must be one of `kind`; a single-value code and a code of another kind return null.
* - Each half is read as `readEdiValue` reads a date or a local date-time. A 2379 period is run
* together and a 1250 range has exactly one hyphen: the grammar holds that.
* - An end that precedes its start returns null (**GMT rule**: a reversed period names no span
* of time). An end equal to its start is a valid period.
* - Never throws: any failure is null.
*
* @param standard which element the code belongs to
* @param kind the kind of period or range the calling function reads
* @param code the 2379 or 1250 format code
* @param value the element value
* @returns the two ends as ISO 8601 strings, or null
*
* @example readEdiRange("edifact", "datePeriod", "718", "2024061520240620") // { start: "2024-06-15", end: "2024-06-20" }
* @example readEdiRange("x12", "dateTimeRange", "RDT", "202406151430-202406201600") // { start: "2024-06-15T14:30:00", end: "2024-06-20T16:00:00" }
* @example readEdiRange("x12", "dateRange", "RD8", "20240620-20240615") // null (the end precedes the start)
* @example readEdiRange("edifact", "datePeriod", "102", "20240615") // null (a single date code)
*/
export declare function readEdiRange(standard: EdiStandard, kind: EdiKind, code: string, value: string): {
start: string;
end: string;
} | null;
/**
* Read an X12 data element 337 (Time) value as an ISO 8601 time, `HH:MM:SS[.f[f]]`.
*
* - The value is tested against the grammar of each form in the element's layout table
* (`HHMM`, `HHMMSS`, `HHMMSSD`, `HHMMSSDD`; `ediGrammar.ts`). The four masks have four lengths,
* so at most one matches. Its digits are read by part name, and the fields go through
* `Temporal.PlainTime.from` with `overflow: "reject"`.
* - Decimal seconds become the fraction of the second exactly: one digit is tenths, two are
* hundredths. Temporal writes no trailing zero, so `14304550` is `14:30:45.5`.
* - `""` for a non-string, a value of any other length or shape, hour 24, and minute or second
* 60. Never throws.
*
* @param value the element 337 value as transmitted
* @returns the time, or `""`
*
* @example readX12Time("1430") // "14:30:00"
* @example readX12Time("143045") // "14:30:45"
* @example readX12Time("14300012") // "14:30:00.12"
* @example readX12Time("2430") // ""
*/
export declare function readX12Time(value: string): string;
/**
* Read the date (element 373, `CCYYMMDD`) and the time (element 337) of an X12 segment as one
* local date-time, `YYYY-MM-DDTHH:MM:SS[.f[f]]`.
*
* - The date is read by the `CCYYMMDD` mask of the 1250 code `D8` (one grammar, one reader) and
* the time by `readX12Time`; Temporal joins the two.
* - `""` when either is missing, is not a string or is not a real date or time. Never throws.
*
* @param date the element 373 value as transmitted
* @param time the element 337 value as transmitted
* @returns the local date-time, or `""`
*
* @example readX12DateAndTime("20240615", "1430") // "2024-06-15T14:30:00"
* @example readX12DateAndTime("20240615", "14300012") // "2024-06-15T14:30:00.12"
* @example readX12DateAndTime("20240615", "") // ""
*/
export declare function readX12DateAndTime(date: string, time: string): string;