@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.
82 lines (80 loc) • 5.54 kB
JavaScript
import { spanNs } from "./spanNs.js";
// src/span/calculate/spanMs.ts
/** Nanoseconds in a millisecond. */
const NANOSECONDS_PER_MILLISECOND = 1000000n;
/**
* The largest span, in nanoseconds, whose exact millisecond value a `number` still holds.
*
* Guarding here rather than on the truncated whole-millisecond part matters: a span of
* `MAX_SAFE_INTEGER` milliseconds *plus* a fraction passes a whole-millisecond check and
* then rounds up to 2^53 on recombination, returning an unsafe integer from a function
* documented to return the sentinel past the safe range.
*/
const MAX_SAFE_NANOSECONDS = BigInt(Number.MAX_SAFE_INTEGER) * NANOSECONDS_PER_MILLISECOND;
/**
* Return the exact elapsed milliseconds between two ISO 8601 instant strings.
*
* The universal span primitive for profiling and observability. Fractional, like
* `performance.now()` — a sub-millisecond span is reported as a fraction, not rounded away.
*
* - Signed: negative when `start` is after `end`, so `spanMs(b, a)` is exactly
* `-spanMs(a, b)`.
* - Measures **exact elapsed time**, never calendar distance. A wall-clock day containing a
* DST transition is 23 or 25 hours (or 24.5, in `Australia/Lord_Howe`), and `spanMs`
* reports the hours that actually elapsed. `spanWallClock` answers the calendar question;
* conflating the two is the most common span bug there is.
* - Accepts anything `toNanoseconds` does: the full RFC 9557 instant grammar, no leap
* seconds; calendar and elective annotations are ignored, as `Temporal.Instant.from` ignores
* them. An offset designator (`Z`, `±HH:MM`) is
* required, optionally followed by a bracketed IANA zone — a bracket alone is not enough,
* which is the one string shape `spanWallClock` accepts and these two do not. The
* endpoints need not share a zone; an instant is an instant.
* - **The offset fixes the instant; a bracketed zone only resolves a rounded one.** An offset
* written to the minute that is the bracketed zone's sub-minute offset rounded
* (`-00:45[Africa/Monrovia]`, for −00:44:30) names the instant the zone gives, as
* `Temporal.ZonedDateTime.from` reads it, so a zoned string Temporal wrote is read back as
* the instant it was written for, except a wall time repeated inside a sub-minute offset
* change, which reads as its first pass (see `isValidInstant`). Otherwise the bracket is not
* checked: a zone that does not exist, or that disagrees with the offset, changes nothing. `spanWallClock`, by
* contrast, rejects a mismatched offset.
* - Returns `null` on invalid input, not `0` — `0` is the span between an instant and itself.
* `null`, not `NaN`, because that is GMT's sentinel for every number-returning function:
* it is the one a caller already checks for, and the only one `strictNullChecks` forces
* them to handle. A `NaN` would type as plain `number` and propagate silently through
* arithmetic. `spanNs` and `spanWallClock` return `null` for the same reason.
* - **Safe range.** Returns `null` when the exact span exceeds `Number.MAX_SAFE_INTEGER`
* milliseconds (±9_007_199_254_740_991 ms, about ±285,000 years), which two instants at
* opposite ends of `Temporal.Instant`'s range do. A sub-millisecond fraction counts
* toward that ceiling, so any number returned is a safe integer or below. Use `spanNs`
* past it — it carries the exact value as a `bigint`.
* Sub-millisecond fidelity goes first, long before the ceiling: a double carries the
* nanosecond digits exactly only while the span is under 2^53 ns, roughly 104 days.
* - Leap seconds are not counted. UTC repeats a second rather than numbering a 61st one, so
* a span across one is a second short of the physical elapsed time; against a smeared
* clock (Google, AWS, Meta) the error is up to a second spread over the smear window.
* For a leap-second-exact span, convert both ends with `toTAI` or add
* `leapSecondsBetween(start, end)` seconds, both from `timescale/`.
*
* @param start ISO 8601 instant string the span is measured from
* @param end ISO 8601 instant string the span is measured to
* @returns exact elapsed milliseconds, negative when start is after end, or null on invalid input
*
* @example spanMs("2024-03-10T12:00:00Z", "2024-03-10T12:00:01Z") // 1000
* @example spanMs("2024-03-10T12:00:01Z", "2024-03-10T12:00:00Z") // -1000
* @example spanMs("2024-03-10T12:00:00Z", "2024-03-10T12:00:00Z") // 0
* @example spanMs("2024-03-10T12:00:00Z", "2024-03-10T12:00:00.123456789Z") // 123.456789
* @example spanMs("2024-03-30T12:00:00+00:00[Europe/London]", "2024-03-31T12:00:00+01:00[Europe/London]") // 82800000 — 23 hours, not 24
* @example spanMs("2024-03-10T12:00:00+05:00[Asia/Tokyo]", "2024-03-10T12:00:00-04:00[Asia/Tokyo]") // 32400000 — offsets decide, the zone is not checked
* @example spanMs("1960-01-01T01:04:30Z", "1960-01-01T00:20:00-00:45[Africa/Monrovia]") // 0 — the zone's −00:44:30, which `-00:45` rounds
* @example spanMs("-271821-04-20T00:00:00Z", "+275760-09-13T00:00:00Z") // null — past the safe integer range, use spanNs
* @example spanMs("2024-03-10T12:00:00Z", "invalid") // null
*/
function spanMs(start, end) {
const nanoseconds = spanNs(start, end);
if (nanoseconds === null) return null;
if (nanoseconds > MAX_SAFE_NANOSECONDS || nanoseconds < -MAX_SAFE_NANOSECONDS) return null;
const milliseconds = nanoseconds / NANOSECONDS_PER_MILLISECOND;
const remainder = nanoseconds % NANOSECONDS_PER_MILLISECOND;
return Number(milliseconds) + Number(remainder) / 1e6;
}
export { spanMs };