@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.
78 lines • 5.78 kB
TypeScript
/**
* A headway service: a departure every `headway` from `from`, up to (not including) `to` — the
* shape of a GTFS `frequencies.txt` row (`headway_secs`, `start_time`, `end_time`; GTFS Schedule
* Reference, https://github.com/google/transit/blob/master/gtfs/spec/en/reference.md#frequenciestxt).
*
* The departures are modelled as GTFS `exact_times=1` (schedule-based): each `from + k × headway`
* is a real departure. For a frequency-based service (`exact_times=0`, "a bus every 20 minutes"
* with no published times) the result is a nominal slot, not a timetabled departure.
*/
export interface Headway {
/**
* The ISO 8601 duration between departures: exact time (a day is 24 hours), greater than zero.
*/
headway: string;
/** The first departure, an exact moment: an instant (`Z`/offset) or a zoned string. */
from: string;
/** The end of the service window, excluded: an exact moment after `from`. */
to: string;
}
/** Options for `nextDeparture`. */
export interface NextDepartureOptions {
/**
* The ISO 8601 duration the connection needs after `after` before a departure can be made:
* exact time (a day is 24 hours), not negative.
*
* @defaultValue `"PT0S"`
*/
minimumConnection?: string;
}
/**
* Find the next departure a connection can make: the first scheduled departure at or after
* `after` plus the minimum connection time.
*
* This is timetable lookup, not routing: it answers "which departure does this arrival make",
* the explicit `departure` a `scheduleDelivery` leg takes. Choosing among routes is the
* consumer's.
*
* - **The threshold is `after` plus `minimumConnection`, in exact time.** A departure exactly at
* the threshold is made: a zero-slack connection is feasible, as in `scheduleDelivery`. The
* connection follows `transitTime`'s duration rule (a day is 24 hours; years, months and weeks
* return `""`) and may not be negative.
* - **List form** (`string[]`): the earliest entry at or after the threshold, in any order. Two
* entries at the same instant go to the first in the list. The entry is returned **exactly as
* written**, so it can be passed straight to `scheduleDelivery` as a leg's `departure`. An empty
* list, or no entry late enough, returns `""`.
* - **Headway form** (`{ headway, from, to }`): departures every `headway` from `from`, in the
* half-open window `[from, to)` — `to` itself is never a departure, as a GTFS
* `frequencies.txt` `end_time` is not. Departures are `from + k × headway` in exact time, so
* across a DST transition they keep their spacing and the wall clock shifts. The result is
* written the way `from` was written: in its zone, at its offset exactly as written, or as a
* `Z` instant. A headway that is not greater than zero, or `to` at or before `from`, returns
* `""`, as does a threshold past the last departure. This models GTFS `exact_times=1`
* (schedule-based) service; for frequency-based service (`exact_times=0`) the result is a
* nominal slot, not a timetabled departure.
* - **Every moment must carry its offset** — `after`, each entry, `from` and `to`: an instant
* (`Z`/offset), or a zoned string with its offset, whose bracketed zone must be real and agree
* with that offset, as `transitTime` reads a departure. A zoned wall time without an offset is
* refused, since in a repeated fall-back hour it names two departures, and a zoneless wall time
* names none; both return `""`. One invalid entry invalidates the whole list, never a partial
* answer.
* - Returns `""` on invalid input or when no departure qualifies.
*
* @param after ISO 8601 instant string, or zoned datetime string that carries its offset, of the arrival that must connect
* @param timetable the departures as exact moments, or a headway service `{ headway, from, to }`
* @param options The connection time needed before a departure can be made
* @returns the departure (a list entry as written, or a headway departure in from's notation; "" when none qualifies), or "" on invalid input
*
* @example nextDeparture("2024-06-15T09:00:00Z", ["2024-06-15T08:00:00Z", "2024-06-15T09:30:00Z", "2024-06-15T11:00:00Z"]) // "2024-06-15T09:30:00Z"
* @example nextDeparture("2024-06-15T09:00:00Z", ["2024-06-15T08:00:00Z", "2024-06-15T09:30:00Z", "2024-06-15T11:00:00Z"], { minimumConnection: "PT45M" }) // "2024-06-15T11:00:00Z" (a 45-minute connection misses the 09:30)
* @example nextDeparture("2024-06-15T09:00:00Z", ["2024-06-15T05:30:00-04:00[America/Santiago]"]) // "2024-06-15T05:30:00-04:00[America/Santiago]" (echoed as written)
* @example nextDeparture("2024-06-15T06:05:00+02:00", { headway: "PT20M", from: "2024-06-15T06:00:00+02:00", to: "2024-06-15T09:00:00+02:00" }) // "2024-06-15T06:20:00+02:00"
* @example nextDeparture("2024-06-15T09:00:00+02:00", { headway: "PT20M", from: "2024-06-15T06:00:00+02:00", to: "2024-06-15T09:00:00+02:00" }) // "" (to is excluded)
* @example nextDeparture("2024-10-27T01:30:00+01:00[Europe/London]", { headway: "PT1H", from: "2024-10-27T00:00:00+01:00[Europe/London]", to: "2024-10-27T04:00:00+00:00[Europe/London]" }) // "2024-10-27T01:00:00+00:00[Europe/London]" (hourly in exact time: the repeated hour's second pass)
* @example nextDeparture("2024-06-15T09:00:00Z", ["2024-06-15T09:30:00"]) // "" (a timetable entry needs an offset)
* @example nextDeparture("2024-06-15T09:00:00Z", ["2024-06-15T09:30:00[Europe/London]"]) // "" (a zoned entry needs its offset too)
* @example nextDeparture("2024-06-15T09:00:00Z", ["2024-06-15T09:30:00Z"], { minimumConnection: "-PT5M" }) // "" (a negative connection)
*/
export declare function nextDeparture(after: string, timetable: readonly string[] | Headway, options?: NextDepartureOptions): string;