@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.
28 lines (26 loc) • 1.1 kB
JavaScript
import { Temporal } from "./temporal.js";
// src/internal/instantIntervalText.ts
/**
* Re-serialise instant intervals to `Temporal.Instant.prototype.toString()` spelling.
*
* The CORE-6 `interval/` functions echo the caller's strings; the positional `utc/interval/`
* functions that delegate to them return canonical `Z` strings instead (CORE-8 half-open decisions of record:
* outputs stay re-serialised), so tie-breaks between spellings of one instant never surface.
*
* - Returns `null` if any string fails to parse (callers validate first, so only a throwing
* `Temporal.Instant.from` reaches this).
*
* @example canonicalInstantIntervals([{ start: "2024-01-01T09:00:00.000Z", end: "2024-01-01T12:00Z" }]) // [{ start: "2024-01-01T09:00:00Z", end: "2024-01-01T12:00:00Z" }]
* @example canonicalInstantIntervals([]) // []
*/
function canonicalInstantIntervals(intervals) {
try {
return intervals.map(({ start, end }) => ({
start: Temporal.Instant.from(start).toString(),
end: Temporal.Instant.from(end).toString()
}));
} catch {
return null;
}
}
export { canonicalInstantIntervals };