import { _randomInteger } from "./_randomInteger"; import { _ILengthProps, _RANDOM_LENGTH_ERROR, _randomLengthPick, _randomLengthWindow, } from "./_randomStringLength"; import { __randomDigits } from "./private/__randomComposition"; /** * Generate an RFC 3339 time with an offset at a length the bounds allow. * * Constrained output lengths are nine or at least 11 characters; ten is * unreachable because a fractional part needs a dot and a digit. An allowed * window containing no such length throws. Samples use ordinary seconds from a * UTC clock, with either Z or a numeric offset, rather than leap seconds. * * @evidence contracts/common.md#principled-implementation An unconstrained draw is the clock part of a random instant, and a constrained one writes `HH:MM:SS` with either a `Z` offset or a numeric offset and a fraction, choosing the form that fits the window, with the single unreachable length of ten excluded and an empty window throwing. * @evidence contracts/common.md#clear-and-simple-design One function with small offset helpers. * @evidence contracts/common.md#prohibited-implementation-shortcuts The length arithmetic follows RFC 3339 and is explained inline. * @evidence contracts/common.md#meaningful-documentation Native prose states the length gap, impossible-window failure and ordinary-second sample policy; inline comments justify the fraction/offset arithmetic. */ export const _randomFormatTime = (props?: _ILengthProps): string => { const clock = (): string => new Date( _randomInteger({ type: "integer", minimum: 0, maximum: DAY }), ).toISOString(); if (props?.minLength === undefined && props?.maxLength === undefined) return clock().substring(11); // `HH:MM:SS` carries a mandatory offset — one character as `Z`, six as // `+HH:MM` — and an optional fraction between them that RFC 3339 leaves // open above. Together they express 9 and every length from 11 upward; 10 is // the one gap, because a fraction needs both a dot and a digit. const window = _randomLengthWindow(props, { minimum: CLOCK + 1, spread: 6, }); let length: number = _randomLengthPick(window); if (length === CLOCK + 2) length = window.high >= CLOCK + 3 ? CLOCK + 3 : CLOCK + 1; if (length < window.low || length > window.high) throw new Error(_RANDOM_LENGTH_ERROR); const base: string = clock().substring(11, 11 + CLOCK); if (length === CLOCK + 1) return `${base}Z`; // The `Z` form reaches every remaining length by itself, so the numeric // offset is a choice rather than a fallback: it spends six characters, which // leaves either nothing (14, `HH:MM:SS+HH:MM`) or a whole fraction (16 and // up). At 15 only the `Z` form fits, since the offset would demand a dot // with no digit after it. const fraction: number = length - CLOCK - OFFSET - 1; return (fraction === -1 || fraction >= 1) && _randomInteger({ type: "integer", minimum: 0, maximum: 1 }) === 1 ? fraction === -1 ? `${base}${offset()}` : `${base}.${__randomDigits(fraction)}${offset()}` : `${base}.${__randomDigits(length - CLOCK - 2)}Z`; }; const DAY = 86_400_000; const CLOCK = "00:00:00".length; const OFFSET = "+00:00".length; const offset = (): string => `${_randomInteger({ type: "integer", minimum: 0, maximum: 1 }) === 0 ? "+" : "-"}${pad( _randomInteger({ type: "integer", minimum: 0, maximum: 23 }), )}:${pad(_randomInteger({ type: "integer", minimum: 0, maximum: 59 }))}`; const pad = (value: number): string => String(value).padStart(2, "0");