@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.
95 lines • 4.43 kB
TypeScript
import { FixPrecision, FixUtcTimestamp } from "../types/fix.js";
import { UtcReading } from "./utcReading.js";
/**
* FIX `UTCTimestamp`: date, dash, time with colons, optional period and 3, 6, 9 or 12 digits
* (FIX Latest, `UTCTimestamp`). Shape only, and the seconds field 00-60; month, day, hour and
* minute ranges are left to Temporal's `overflow: "reject"`. `\d` without the `u` flag is ASCII
* only, and with no `m` flag `$` does not match before a trailing newline.
*/
export declare const FIX_UTC_TIMESTAMP: RegExp;
/** Fraction digits by precision (FIX Latest: 3 milli-, 6 micro-, 9 nano-, 12 picoseconds). */
export declare const FIX_FRACTION_DIGITS: Readonly<Record<FixPrecision, 0 | 3 | 6 | 9 | 12>>;
/**
* The precision a fraction digit count states.
*
* @param count digits after the period, one of the counts `FIX_UTC_TIMESTAMP` matches
* @returns the precision
*
* @example fixPrecisionOfDigits(3) // "millisecond"
* @example fixPrecisionOfDigits(12) // "picosecond"
*/
export declare function fixPrecisionOfDigits(count: 0 | 3 | 6 | 9 | 12): FixPrecision;
/**
* A precision name, singular or plural.
*
* @param value candidate precision
* @returns the singular precision, or null
*
* @example resolveFixPrecision("milliseconds") // "millisecond"
* @example resolveFixPrecision("toString") // null
*/
export declare function resolveFixPrecision(value: unknown): FixPrecision | null;
/** The fields of a matched value, before any calendar check. */
export interface FixUtcFields {
readonly year: number;
readonly month: number;
readonly day: number;
readonly hour: number;
readonly minute: number;
/** 0 to 60, as sent. */
readonly second: number;
readonly fractionDigits: 0 | 3 | 6 | 9 | 12;
/** Fraction digits 1 to 3, 0 when absent. */
readonly millisecond: number;
/** Fraction digits 4 to 6. */
readonly microsecond: number;
/** Fraction digits 7 to 9. */
readonly nanosecond: number;
/** Fraction digits 10 to 12, 0 unless the fraction has 12 digits. */
readonly picoseconds: number;
}
/**
* Read the fields of a FIX `UTCTimestamp` from the grammar's own captures. No calendar check.
*
* @param value candidate string
* @returns the fields, or null for a non-string or a value that does not match the grammar
*
* @example readFixUtcFields("20240615-14:30:00.123456789012") // { …, millisecond: 123, picoseconds: 12 }
* @example readFixUtcFields("20240615T14:30:00") // null
*/
export declare function readFixUtcFields(value: unknown): FixUtcFields | null;
/**
* The ISO instant text of matched FIX fields, `YYYY-MM-DDTHH:MM:SS[.d…]Z`, written from the digits
* as sent: the second stays `60`, and a 12-digit fraction keeps its first nine digits. Nothing is
* checked here; check the fields with `Temporal.PlainDateTime.from(…, { overflow: "reject" })`
* first. The year needs no check: the grammar gives four digits, and a FIX stamp is already UTC.
*
* @param fields the fields from `readFixUtcFields`
* @returns the instant string
*
* @example fixInstantText(readFixUtcFields("20240615-14:30:00.100")!) // "2024-06-15T14:30:00.100Z"
* @example fixInstantText(readFixUtcFields("20161231-23:59:60")!) // "2016-12-31T23:59:60Z"
*/
export declare function fixInstantText(fields: FixUtcFields): string;
/**
* The FIX text of a UTC reading at a precision: `YYYYMMDD-HH:MM:SS[.fraction]`, cut and never
* rounded; `picosecond` pads `000`. Throws where Temporal throws, so call it inside `try`.
*
* @param reading the UTC reading
* @param precision how many fraction digits to write
* @returns the FIX text, or "" for a year outside 0000-9999
*
* @example fixTimestampText({ epochNanoseconds: -1n, secondSixty: false }, "nanosecond") // "19691231-23:59:59.999999999"
*/
export declare function fixTimestampText(reading: UtcReading, precision: FixPrecision): string;
/**
* The core of `parseFixUtcTimestamp` and `isValidFixUtcTimestamp`: one `UTCTimestamp` read into
* its instant, precision, leap-second flag and picoseconds. Never throws.
*
* @param value a FIX `UTCTimestamp` such as `20240315-12:30:45.123`
* @returns the reading, or `null` when `value` is not a real UTCTimestamp
*
* @example readFixUtcTimestamp("20240315-12:30:45.123") // an instant, precision and flags
* @example readFixUtcTimestamp("20240230-12:30:45") // null
*/
export declare function readFixUtcTimestamp(value: string): FixUtcTimestamp | null;