@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.
98 lines (96 loc) • 5.33 kB
JavaScript
import { durationFrom } from "../../internal/durationFrom.js";
import { isDurationString } from "../../internal/inputChecks.js";
import { instantFrom, parseInstantNanoseconds } from "../../internal/instantNanoseconds.js";
import { isObject } from "../../internal/isObject.js";
import { cutoffAtResolved, resolveCutoffTime, resolveSharedCutoffOptions } from "../../internal/cutoffAtResolved.js";
// src/transport/calculate/cutoffSchedule.ts
/** The shared options and the anchor, resolved once; `null` when either is invalid. */
function stackContext(state) {
const { timeZone, calendar, roll } = state.options;
state.shared ??= resolveSharedCutoffOptions(timeZone, calendar, roll);
if (state.shared === null) return null;
if (state.anchorInstant === null) {
if (parseInstantNanoseconds(state.anchor) === null) return null;
state.anchorInstant = instantFrom(state.anchor);
}
return {
shared: state.shared,
anchorInstant: state.anchorInstant
};
}
/** One entry's cut-off, or `null` when the entry or its cut-off is invalid. */
function scheduleEntry(cutoff, state) {
if (!isObject(cutoff)) return null;
const { name, offset, atLocalTime } = cutoff;
if (typeof name !== "string") return null;
const local = resolveCutoffTime(atLocalTime);
const context = local === null ? null : stackContext(state);
if (local === null || context === null || !isDurationString(offset)) return null;
const found = cutoffAtResolved(context.anchorInstant, durationFrom(offset), local.time, context.shared);
return found === null ? null : {
name,
at: found.text,
ns: found.ns
};
}
/**
* Compute a whole stack of cut-offs against one anchor event, earliest first.
*
* An ocean booking carries several deadlines counted back from the same departure —
* documentation, VGM, gate-in, customs — and when the vessel is rescheduled all of them move.
* `cutoffSchedule` computes each entry with `cutoffAt` against the one anchor, so a new
* departure is one call and every deadline follows it.
*
* - Each entry is `cutoffAt(anchor, entry.offset, { ...options, atLocalTime: entry.atLocalTime })`:
* the same local-time resolution, the same rolling, the same sentinel. `atLocalTime` belongs
* to the entry; one in `options` is not read.
* - Sorted earliest first by instant, not by the text of the local time: on a fall-back day
* 01:30 on the first pass is before 01:15 on the second. Entries on the same instant keep the
* order they were given in.
* - `name` is an opaque string echoed back; duplicates are kept.
* - Every entry must succeed: one invalid entry, or one cut-off in a skipped local hour,
* returns `[]` rather than a stack with a deadline silently missing. An empty `cutoffs` array
* returns `[]`.
* - An offset with seconds (`-00:44:30`) returns `[]`. A written zone cannot carry seconds (RFC
* 9557 §4.1), so the result could not name it. Pass the IANA name.
*
* @param anchor ISO 8601 instant string (`Z` or `±HH:MM`, no zone bracket) of the event the deadlines count back from
* @param cutoffs the named deadlines, each with an offset and an optional local time of day
* @param options The zone every cut-off is read in, and how one rolls off a non-business day, as cutoffAt takes them
* @returns the named cut-offs, earliest first, or [] on invalid input
*
* @example cutoffSchedule("2024-06-14T16:00:00Z", [{ name: "gate-in", offset: "P1D" }, { name: "document", offset: "P2D", atLocalTime: "17:00" }, { name: "VGM", offset: "P1D", atLocalTime: "10:00" }], { timeZone: "Europe/Amsterdam" }) // [{ name: "document", at: "2024-06-12T17:00:00+02:00[Europe/Amsterdam]" }, { name: "VGM", at: "2024-06-13T10:00:00+02:00[Europe/Amsterdam]" }, { name: "gate-in", at: "2024-06-13T18:00:00+02:00[Europe/Amsterdam]" }]
* @example cutoffSchedule("2024-06-18T16:00:00Z", [{ name: "document", offset: "P2D", atLocalTime: "17:00" }, { name: "VGM", offset: "P1D", atLocalTime: "10:00" }], { timeZone: "Europe/Amsterdam", calendar: { weekend: [6, 7], holidays: [], timeZone: "Europe/Amsterdam" }, roll: "preceding" }) // [{ name: "document", at: "2024-06-14T17:00:00+02:00[Europe/Amsterdam]" }, { name: "VGM", at: "2024-06-17T10:00:00+02:00[Europe/Amsterdam]" }] (Sunday's document cut-off rolls back to Friday)
* @example cutoffSchedule("2024-06-14T16:00:00Z", [], { timeZone: "Europe/Amsterdam" }) // []
* @example cutoffSchedule("2024-06-14T16:00:00Z", [{ name: "document", offset: "2 days" }], { timeZone: "Europe/Amsterdam" }) // []
* @example cutoffSchedule("2024-10-07T07:00:00Z", [{ name: "gate-in", offset: "P1D" }, { name: "VGM", offset: "P1D", atLocalTime: "02:30" }], { timeZone: "Australia/Sydney" }) // [] (02:30 on 6 October never happened)
*/
function cutoffSchedule(anchor, cutoffs, options) {
try {
if (!Array.isArray(cutoffs) || !isObject(options)) return [];
const { timeZone, calendar, roll } = options;
const state = {
anchor,
options: {
timeZone,
calendar,
roll
},
shared: null,
anchorInstant: null
};
const schedule = [];
for (const cutoff of cutoffs) {
const entry = scheduleEntry(cutoff, state);
if (entry === null) return [];
schedule.push(entry);
}
return schedule.sort((a, b) => a.ns < b.ns ? -1 : a.ns > b.ns ? 1 : 0).map(({ name, at }) => ({
name,
at
}));
} catch {
return [];
}
}
export { cutoffSchedule };