UNPKG

@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.

127 lines (125 loc) • 9.51 kB
import { Temporal } from "../../internal/temporal.js"; import { instantOf } from "../../internal/instantNanoseconds.js"; import { isObject } from "../../internal/isObject.js"; import { optionOrDefault } from "../../internal/optionOrDefault.js"; // src/intermodal/calculate/demurrageClock.ts const CLOCK_EVENT_TYPES = [ "discharged", "available", "gatedOut", "emptyReturned", "emptyReleased", "gatedIn", "loaded" ]; const CLOCK_SCOPES = [ "demurrage", "detention", "storage", "combined" ]; const CLOCK_START_EVENTS = ["discharged", "available"]; /** The export clocks: fixed pairs, no start-event choice. */ const EXPORT_CLOCKS = { demurrage: ["gatedIn", "loaded"], storage: ["gatedIn", "loaded"], detention: ["emptyReleased", "gatedIn"], combined: ["emptyReleased", "loaded"] }; /** * Pick the two events a charge's clock runs between. * * No regulation or industry standard fixes these events; the carriers' published tariffs agree * on them (import: Maersk's terms, CMA CGM's general terms, Hapag-Lloyd, ACL; export: Maersk, CMA * CGM, ACL), and DCSA's glossary draws the line they rest on: demurrage is charged while the * container is inside the terminal or depot, detention while it is outside, and storage is the * charge for the terminal's space, paid to the terminal or to the carrier on its behalf. Export * storage (`gatedIn` → `loaded`) follows from that storage definition; no tariff read quotes it. * The merged clock runs demurrage and detention as one period, a named tariff product at Maersk * ("Combined"), CMA CGM ("merged") and MSC. `demurrageClock` selects the pair for the charge and * direction asked for, so the wrong pair can never reach `chargeableDays`. * * | scope | import | export | * | --- | --- | --- | * | `demurrage`, `storage` | `startEvent` → `gatedOut` | `gatedIn` → `loaded` | * | `detention` | `gatedOut` → `emptyReturned` | `emptyReleased` → `gatedIn` | * | `combined` | `startEvent` → `emptyReturned` | `emptyReleased` → `loaded` | * * - **`direction` has no default.** The same scope name selects different events on the import and * export legs, and guessing it from the events present would be a silent guess. * - **`startEvent` is import only.** It is not read for detention or export, so one options * object can serve every scope on a leg. A tariff that starts at * customs release (Hapag-Lloyd Japan) or ends export demurrage at the cut-off or the scheduled * sailing passes that instant as the event's `at`; GMT does not invent an event for one tariff. * - Events may be in any order, and every one must be `{ type, at }` with a known type and a * valid instant, used or not, with no holes in the array: a list with a bad entry is a bad list. * - The result is an `Interval`, half-open `[start, end)`, in the caller's own strings, and feeds * `chargeableDays(start, end, …)` directly. * - Some tariffs differ in words, not events: MSC USA calls the in-terminal container charge * "detention" and the out-of-terminal one "per diem". GMT's scope names follow the global usage. * - Returns `null` when `events` is not an array or holds an invalid event or a hole, `scope`, * `direction` or `startEvent` is unknown, `options` is not an object, a required event is * missing or occurs more than once (a container discharged twice is a data error, not a choice), * or the end event precedes the start event. * * @param events the container's events, `{ type, at }`, `type` one of discharged, available, gatedOut, emptyReturned, emptyReleased, gatedIn, loaded * @param scope "demurrage" | "detention" | "storage" | "combined" * @param options Which leg of the cycle the clock is on, and where an import clock starts * @returns { start, end } of the requested clock, or null on invalid input * * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "available", at: "2024-06-15T12:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }, { type: "emptyReturned", at: "2024-06-27T09:00:00Z" }], "demurrage", { direction: "import" }) // { start: "2024-06-14T19:00:00Z", end: "2024-06-20T14:30:00Z" } * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "available", at: "2024-06-15T12:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }, { type: "emptyReturned", at: "2024-06-27T09:00:00Z" }], "detention", { direction: "import" }) // { start: "2024-06-20T14:30:00Z", end: "2024-06-27T09:00:00Z" } (gate-out to empty return, not discharge) * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "available", at: "2024-06-15T12:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }, { type: "emptyReturned", at: "2024-06-27T09:00:00Z" }], "combined", { direction: "import" }) // { start: "2024-06-14T19:00:00Z", end: "2024-06-27T09:00:00Z" } (merged: discharge to empty return) * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "available", at: "2024-06-15T12:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }], "demurrage", { direction: "import", startEvent: "available" }) // { start: "2024-06-15T12:00:00Z", end: "2024-06-20T14:30:00Z" } * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }], "storage", { direction: "import" }) // { start: "2024-06-14T19:00:00Z", end: "2024-06-20T14:30:00Z" } * @example demurrageClock([{ type: "emptyReleased", at: "2024-07-01T08:00:00Z" }, { type: "gatedIn", at: "2024-07-05T16:00:00Z" }, { type: "loaded", at: "2024-07-09T03:00:00Z" }], "demurrage", { direction: "export" }) // { start: "2024-07-05T16:00:00Z", end: "2024-07-09T03:00:00Z" } (gate-in full to loaded on board) * @example demurrageClock([{ type: "emptyReleased", at: "2024-07-01T08:00:00Z" }, { type: "gatedIn", at: "2024-07-05T16:00:00Z" }, { type: "loaded", at: "2024-07-09T03:00:00Z" }], "detention", { direction: "export" }) // { start: "2024-07-01T08:00:00Z", end: "2024-07-05T16:00:00Z" } (empty picked up to gate-in full) * @example demurrageClock([{ type: "emptyReleased", at: "2024-07-01T08:00:00Z" }, { type: "gatedIn", at: "2024-07-05T16:00:00Z" }, { type: "loaded", at: "2024-07-09T03:00:00Z" }], "combined", { direction: "export" }) // { start: "2024-07-01T08:00:00Z", end: "2024-07-09T03:00:00Z" } * @example demurrageClock([{ type: "gatedOut", at: "2024-06-20T10:30:00+02:00" }, { type: "emptyReturned", at: "2024-06-27T09:00:00Z" }], "detention", { direction: "import", startEvent: "available" }) // { start: "2024-06-20T10:30:00+02:00", end: "2024-06-27T09:00:00Z" } (startEvent is not read for detention; strings are echoed as given) * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }], "demurrage") // null (direction has no default) * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }], "demurrage", { direction: "import", startEvent: "available" }) // null (no availability event) * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }], "demurrage", { direction: "import" }) // null (still in the terminal: no gate-out) * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "discharged", at: "2024-06-15T19:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }], "demurrage", { direction: "import" }) // null (discharged twice) * @example demurrageClock([{ type: "discharged", at: "2024-06-21T19:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }], "demurrage", { direction: "import" }) // null (gate-out before discharge) * @example demurrageClock([{ type: "discharged", at: "2024-06-14T19:00:00Z" }, { type: "gatedOut", at: "2024-06-20T14:30:00Z" }], "laytime", { direction: "import" }) // null (not a clock this function selects) */ function demurrageClock(events, scope, options) { try { return selectClock(events, scope, options); } catch { return null; } } /** `demurrageClock` without the guard; every read of its arguments happens here. */ function selectClock(events, scope, options) { if (!Array.isArray(events) || !CLOCK_SCOPES.includes(scope) || !isObject(options)) return null; const { direction } = options; if (direction !== "import" && direction !== "export") return null; const startEvent = optionOrDefault(options.startEvent, "discharged"); if (!CLOCK_START_EVENTS.includes(startEvent)) return null; const list = Array.from(events); const instants = []; const texts = []; if (!list.every((event, index) => { if (!isObject(event) || !CLOCK_EVENT_TYPES.includes(event.type)) return false; const at = event.at; const instant = instantOf(at); if (instant === null) return false; instants[index] = instant; texts[index] = at; return true; })) return null; const [startType, endType] = direction === "export" ? EXPORT_CLOCKS[scope] : scope === "detention" ? ["gatedOut", "emptyReturned"] : [startEvent, scope === "combined" ? "emptyReturned" : "gatedOut"]; const only = (type) => { const matches = list.flatMap((event, index) => event.type === type ? [index] : []); return matches.length === 1 ? matches[0] : null; }; const start = only(startType); const end = only(endType); if (start === null || end === null) return null; return Temporal.Instant.compare(instants[start], instants[end]) > 0 ? null : { start: texts[start], end: texts[end] }; } export { demurrageClock };