@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.
68 lines (66 loc) • 4.92 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { nonNegativeExactDurationNanoseconds } from "../../internal/exactDuration.js";
import { formatHourDuration } from "../../internal/hourDurationString.js";
import { parseTimestampEvents } from "../../internal/timestampEvents.js";
// src/transport/calculate/estimateDrift.ts
/**
* Measure how far an estimate moved between its first and last revision.
*
* Schedule-reliability reporting asks how much an estimated arrival slid while it was being
* revised, and a revision far enough from the first estimate means something must be redone:
* rebooked, re-planned, renotified. The threshold is the caller's; the consequence is the
* consumer's.
*
* - Only `EST` records are read, in the order they were recorded (`recordedAt`). Two records at
* the same instant are ordered by their place in the array, the later one last — the same tie
* rule as `bestAvailable`. `PLN`, `REQ` and `ACT` records are validated and otherwise ignored.
* - `revisions` is the number of EST records, the first included.
* - `drift` is the last EST's `at` minus the first's, in exact elapsed time with hours as the
* largest unit: positive when the estimate moved later, negative when earlier. Across a
* fall-back night, 01:30 BST to 01:30 GMT is `PT1H`.
* - With `tolerance`, `exceedsTolerance` is `true` when the drift, in either direction, is
* **greater** than the tolerance; exactly the tolerance is `false`. Without it, `null`.
* - `first` and `last` echo `at` exactly as written. `at` and `recordedAt` are instants (`Z` or an
* offset) with no annotation of any kind, compared as instants: the offset fixes the instant.
* The drift is exact time between two moments and keeps no zone, so a zoned string, a real
* zone bracket included, is invalid.
* - Returns `null` with fewer than two EST records — there is no drift from one sample — and on
* invalid input: any invalid event, of any class, or an invalid, negative or calendar-unit
* `tolerance`.
*
* @param events the event's timestamp records, in any order
* @param options The threshold the absolute drift is compared with
* @returns the first and last estimates, the drift, the revision count and the tolerance check (null with fewer than two EST records), or null on invalid input
*
* @example estimateDrift([{ classifier: "EST", at: "2024-06-20T08:00:00Z", recordedAt: "2024-06-01T00:00:00Z" }, { classifier: "EST", at: "2024-06-20T12:00:00Z", recordedAt: "2024-06-05T00:00:00Z" }, { classifier: "EST", at: "2024-06-20T17:00:00Z", recordedAt: "2024-06-10T00:00:00Z" }]) // { first: "2024-06-20T08:00:00Z", last: "2024-06-20T17:00:00Z", drift: "PT9H", revisions: 3, exceedsTolerance: null }
* @example estimateDrift([{ classifier: "EST", at: "2024-06-20T17:00:00Z", recordedAt: "2024-06-01T00:00:00Z" }, { classifier: "EST", at: "2024-06-20T08:00:00Z", recordedAt: "2024-06-10T00:00:00Z" }], { tolerance: "PT8H" }) // { first: "2024-06-20T17:00:00Z", last: "2024-06-20T08:00:00Z", drift: "-PT9H", revisions: 2, exceedsTolerance: true } (nine hours earlier exceeds eight)
* @example estimateDrift([{ classifier: "EST", at: "2024-06-20T08:00:00Z", recordedAt: "2024-06-01T00:00:00Z" }, { classifier: "EST", at: "2024-06-20T15:00:00Z", recordedAt: "2024-06-10T00:00:00Z" }], { tolerance: "PT8H" }) // { first: "2024-06-20T08:00:00Z", last: "2024-06-20T15:00:00Z", drift: "PT7H", revisions: 2, exceedsTolerance: false }
* @example estimateDrift([{ classifier: "EST", at: "2024-06-20T08:00:00Z", recordedAt: "2024-06-01T00:00:00Z" }]) // null (no drift from one sample)
* @example estimateDrift([{ classifier: "EST", at: "2024-06-20T08:00:00Z", recordedAt: "2024-06-01T00:00:00Z" }, { classifier: "EST", at: "2024-06-20T15:00:00Z", recordedAt: "2024-06-10T00:00:00Z" }], { tolerance: "P1W" }) // null (weeks need a reference point)
*/
function estimateDrift(events, options) {
try {
if (!isOptionsArgument(options)) return null;
const tolerance = options?.tolerance;
const toleranceNanoseconds = tolerance === void 0 ? null : nonNegativeExactDurationNanoseconds(tolerance);
if (tolerance !== void 0 && toleranceNanoseconds === null) return null;
const parsed = parseTimestampEvents(events);
if (parsed === null) return null;
const estimates = parsed.filter((event) => event.classifier === "EST");
const first = estimates[0];
const last = estimates[estimates.length - 1];
if (estimates.length < 2 || first === void 0 || last === void 0) return null;
const drift = last.atNanoseconds - first.atNanoseconds;
const magnitude = drift < 0n ? -drift : drift;
return {
first: first.at,
last: last.at,
drift: `${drift < 0n ? "-" : ""}${formatHourDuration(magnitude)}`,
revisions: estimates.length,
exceedsTolerance: toleranceNanoseconds === null ? null : magnitude > toleranceNanoseconds
};
} catch {
return null;
}
}
export { estimateDrift };