@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.
92 lines • 5.76 kB
TypeScript
import { EdiKind, EdiStandard } from "./ediGrammar.js";
/**
* Write one ISO 8601 value as a single EDI date/time value in a format code of one kind: the
* inverse of `readEdiValue`, driven by the same layout tables (`ediGrammar.ts`), so what one
* writes the other reads back.
*
* - The code must be one of `kind`; a code of another kind, a period or range code and a code
* that is not written return `""`.
* - The kind fixes the ISO 8601 string `value` must be, as the house plain functions fix theirs:
* a date is what `isValidDate` accepts, a time what `isValidTime` accepts, a local date-time
* what `isValidDateTime` accepts, and a date-time with an offset what `toOffsetInstant` reads
* (an offset, `Z`, or a bracketed zone that agrees). Temporal then reads the fields; no digit
* of the input is sliced.
* - Precision is cut to the mask and never refused: a fraction of a second is dropped under
* every 2379 and 1250 mask (none has decimal seconds) and the seconds under a mask with no
* `SS`. The cut is a truncation, never a rounding, so a written value never names a later
* minute or second than its input.
* - A value with an offset is written on its own wall clock, never the UTC clock. `ZHHMM` holds
* whole minutes and `ZZZ` whole hours, always written as the signed hour of UN/ECE
* Recommendation 7 ¶12 (`+02`), never `UTC` or `GMT`. An offset the field cannot hold returns
* `""`: rounding it would name another instant.
* - A year outside 0000–9999 returns `""`: `CCYY` is four digits and no sign.
* - Never throws: any failure is `""`.
*
* @param standard which element the code belongs to
* @param kind the kind of value the calling function writes
* @param code the 2379 or 1250 format code
* @param value one ISO 8601 string of that kind
* @returns the element value, or `""`
*
* @example writeEdiValue("edifact", "dateTime", "203", "2024-06-15T14:30:45") // "202406151430"
* @example writeEdiValue("edifact", "offsetDateTime", "303", "2024-06-15T14:30:00+02:00") // "202406151430+02"
* @example writeEdiValue("x12", "date", "DB", "2024-06-15") // "06152024"
* @example writeEdiValue("edifact", "offsetDateTime", "303", "2024-06-15T14:30:00+05:30") // "" (ZZZ holds whole hours)
* @example writeEdiValue("edifact", "date", "102", "2024-06-15T14:30:00") // "" (a date-time is not a date)
*/
export declare function writeEdiValue(standard: EdiStandard, kind: EdiKind, code: string, value: string): string;
/**
* Write two ISO 8601 values as an EDI period or range in a format code of one kind: the inverse
* of `readEdiRange`.
*
* - The code must be one of `kind`; a single-value code and a code of another kind return `""`.
* - Each end is read and written as `writeEdiValue` reads and writes a date or a local
* date-time. The halves are joined by what the standard transmits: nothing for UNTDID 2379,
* one hyphen for X12 1250.
* - An end that precedes its start returns `""` (**GMT rule**: a reversed period names no span
* of time). The ends are compared as given, before either is cut to the mask, so an end 35
* seconds before its start is reversed under a minute mask too. An end equal to its start is
* written.
* - Never throws: any failure is `""`.
*
* @param standard which element the code belongs to
* @param kind the kind of period or range the calling function writes
* @param code the 2379 or 1250 format code
* @param start the first end, one ISO 8601 string of the kind's half
* @param end the second end, of the same kind
* @returns the element value, or `""`
*
* @example writeEdiRange("edifact", "datePeriod", "718", "2024-06-15", "2024-06-20") // "2024061520240620"
* @example writeEdiRange("x12", "dateRange", "RD8", "2024-06-15", "2024-06-20") // "20240615-20240620"
* @example writeEdiRange("x12", "dateRange", "RD8", "2024-06-20", "2024-06-15") // "" (the end precedes the start)
*/
export declare function writeEdiRange(standard: EdiStandard, kind: EdiKind, code: string, start: string, end: string): string;
/**
* Write one ISO 8601 time as an X12 data element 337 (Time) value in one of the element's four
* forms: the inverse of `readX12Time`, driven by the same layout table (`ediGrammar.ts`), so
* what one writes the other reads back.
*
* - The form is the element's own mask (`HHMM`, `HHMMSS`, `HHMMSSD`, `HHMMSSDD`), looked up
* exactly and by own key. A data element 1250 code (`TM`, `TS`) and any other string return
* `""`.
* - `value` is a time, as `isValidTime` accepts it; Temporal then reads the fields. No digit of
* the input is sliced.
* - The form alone fixes the digits written. It is never read off the value, so the width of the
* field does not depend on the data: a time with no fraction is `14300000` under `HHMMSSDD`.
* - Precision is cut to the mask and never refused: `D` is the whole tenths of the second and
* `DD` its whole hundredths. The cut is a truncation, never a rounding, so a written value
* never names a later second than its input.
* - Never throws: any failure is `""`.
*
* @param form the element's mask
* @param value one ISO 8601 time
* @returns the element value, or `""`
*
* @example writeX12TimeElement("HHMMSSDD", "14:30:00.12") // "14300012"
* @example writeX12TimeElement("HHMMSSD", "14:30:00.12") // "1430001" (cut, not rounded)
* @example writeX12TimeElement("HHMMSSDD", "14:30") // "14300000"
* @example writeX12TimeElement("HHMM", "14:30:45.999") // "1430"
* @example writeX12TimeElement("TS", "14:30:45") // "" (a 1250 code, not a mask)
* @example writeX12TimeElement("HHMM", "2024-06-15T14:30:00") // "" (a date-time is not a time)
*/
export declare function writeX12TimeElement(form: string, value: string): string;