# Transport: Multi-leg Scheduling

Industry: Transport

A multi-modal move, truck to ship to rail, is published as one departure plus a duration per
leg, with a handling time at each handoff. The naive approach that adds hours on a clock loses
hours at the handoffs. Every leg boundary here is an exact instant, and local times are
rendered only at the edges.

```typescript
import { crossingTime, scheduleDelivery } from "@northguild/gmt/transport";
```

Both live in `@northguild/gmt/transport/calculate`, beside
[`transitTime` and `dwellTime`](/gmt/guides/industries/transport-legs-and-dwell/).

## Every handoff is an exact instant: `scheduleDelivery`

[`scheduleDelivery(legs, options?)`](/gmt/reference/transport/calculate/scheduleDelivery/) chains
one to four legs. Each leg's arrival plus its `dwellAfter` is the next leg's departure.
`arrival` is a UTC instant. `localArrival` is that instant in the leg's `timeZone`, exactly as
[`etaAtZone`](/gmt/reference/transport/convert/etaAtZone/) renders it. `eta` is the last leg's
`localArrival`. A single leg is exactly `transitTime` composed with `etaAtZone`.

```typescript
import { etaAtZone, scheduleDelivery, transitTime } from "@northguild/gmt/transport";

scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT36H", timeZone: "Asia/Tokyo" }]);
// { eta: "2024-06-17T07:00:00+09:00[Asia/Tokyo]",
//   legTimes: [{ arrival: "2024-06-16T22:00:00Z", localArrival: "2024-06-17T07:00:00+09:00[Asia/Tokyo]", dwellAfter: "PT0S" }] }

transitTime("2024-06-15T10:00:00Z", "PT36H"); // "2024-06-16T22:00:00Z"
etaAtZone("2024-06-16T22:00:00Z", "Asia/Tokyo"); // "2024-06-17T07:00:00+09:00[Asia/Tokyo]"
```

A two-leg journey with a two-hour dwell at the first stop: the second leg leaves at 12:00Z,
two hours after the first arrives.

```typescript
scheduleDelivery([
  { departure: "2024-06-15T00:00:00Z", duration: "PT10H", timeZone: "UTC", dwellAfter: "PT2H" },
  { duration: "PT5H", timeZone: "Asia/Tokyo" },
]);
// { eta: "2024-06-16T02:00:00+09:00[Asia/Tokyo]",
//   legTimes: [{ arrival: "2024-06-15T10:00:00Z", localArrival: "2024-06-15T10:00:00+00:00[UTC]", dwellAfter: "PT2H" },
//     { arrival: "2024-06-15T17:00:00Z", localArrival: "2024-06-16T02:00:00+09:00[Asia/Tokyo]", dwellAfter: "PT0S" }] }
```

The journey — a truck to London, a ship to Tokyo, a train onward — chained in one call:

```typescript
const truck = { departure: "2024-03-29T08:00:00+01:00[Europe/Berlin]", duration: "PT44H", timeZone: "Europe/London", dwellAfter: "PT2H", mode: "truck" };
const ship = { duration: "P11D", timeZone: "Asia/Tokyo", dwellAfter: "PT24H", mode: "ship" };
const rail = { duration: "PT2H30M", timeZone: "Asia/Tokyo", mode: "rail" };

scheduleDelivery([truck, ship, rail]);
// { eta: "2024-04-12T16:30:00+09:00[Asia/Tokyo]",
//   legTimes: [
//     { arrival: "2024-03-31T03:00:00Z", localArrival: "2024-03-31T04:00:00+01:00[Europe/London]", dwellAfter: "PT2H", mode: "truck" },
//     { arrival: "2024-04-11T05:00:00Z", localArrival: "2024-04-11T14:00:00+09:00[Asia/Tokyo]", dwellAfter: "PT24H", mode: "ship" },
//     { arrival: "2024-04-12T07:30:00Z", localArrival: "2024-04-12T16:30:00+09:00[Asia/Tokyo]", dwellAfter: "PT0S", mode: "rail" } ] }
```

The ship has no `departure` of its own: it leaves when the truck's arrival plus its two-hour
dwell allows. The truck lands at 04:00 in London, three hours after the clocks sprang
forward that morning. An offset table holding London's winter +00:00 reads that arrival as
03:00, an hour early, and every handoff time after it inherits the error. Open the whole journey
in the [Delivery Scheduler](/gmt/tools/delivery-scheduler/) to see the itinerary flag the
London arrival 1 h early against that offset table's reading.

**The Date Line.** A westbound leg out of Tahiti at 23:00 on Friday 14 June lands in Auckland
at 02:30 on Sunday 16 June, after 5 hours 30 minutes of elapsed time.

```typescript
scheduleDelivery([{ departure: "2024-06-14T23:00:00-10:00[Pacific/Tahiti]", duration: "PT5H30M", timeZone: "Pacific/Auckland" }]);
// { eta: "2024-06-16T02:30:00+12:00[Pacific/Auckland]", … }
```

Eastbound, a flight out of Auckland at 17:00 on 17 June lands in Tahiti at 00:30 the same
date — sixteen and a half wall-clock hours before it left, by the clocks alone.

```typescript
scheduleDelivery([
  { departure: "2024-06-17T17:00:00+12:00[Pacific/Auckland]", duration: "PT5H30M", timeZone: "Pacific/Tahiti", dwellAfter: "PT3H", mode: "air" },
  { duration: "PT44H", timeZone: "Pacific/Rarotonga", mode: "ship" },
]);
// { eta: "2024-06-18T23:30:00-10:00[Pacific/Rarotonga]",
//   legTimes: [{ arrival: "2024-06-17T10:30:00Z", localArrival: "2024-06-17T00:30:00-10:00[Pacific/Tahiti]", dwellAfter: "PT3H", mode: "air" },
//     { arrival: "2024-06-19T09:30:00Z", localArrival: "2024-06-18T23:30:00-10:00[Pacific/Rarotonga]", dwellAfter: "PT0S", mode: "ship" }] }
```

Nothing special happens at the Date Line, because each end here is only an instant rendered in
a zone.

### A scheduled departure is a connection, and dwell is the minimum connect time

A later leg with its own `departure` is a scheduled connection the cargo waits for.
`dwellAfter` is the handling time at the handoff — the minimum connect time. A departure
earlier than the previous arrival plus its dwell is a missed connection and returns `null`, not
a negative wait. A departure exactly on it passes: equal passes.

```typescript
scheduleDelivery([
  { departure: "2024-06-15T00:00:00Z", duration: "PT10H", timeZone: "UTC", dwellAfter: "PT2H" },
  { departure: "2024-06-15T11:00:00Z", duration: "PT1H", timeZone: "UTC" },
]);
// null — the second leg is scheduled to leave inside the first's two-hour handoff

scheduleDelivery([
  { departure: "2024-06-15T00:00:00Z", duration: "PT10H", timeZone: "UTC", dwellAfter: "PT2H" },
  { departure: "2024-06-15T12:00:00Z", duration: "PT1H", timeZone: "UTC" },
]);
// { eta: "2024-06-15T13:00:00+00:00[UTC]", … } — scheduled exactly when the dwell ends: equal passes
```

`dwellAfter` is echoed as written: `"PT90M"` stays `"PT90M"`, and it reads `"PT0S"` when you
omit it. The last leg's dwell is validated and echoed, but moves nothing, because no leg
follows it to move.

```typescript
scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT1H", timeZone: "UTC", dwellAfter: "PT5H" }]);
// { eta: "2024-06-15T11:00:00+00:00[UTC]", legTimes: [{ …, dwellAfter: "PT5H" }] } — the eta is unchanged
scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT1H", timeZone: "UTC", dwellAfter: "P1M" }]);
// null — still validated
```

Open the handoff on its own in the [Connection Checker](/gmt/tools/connection-checker/).

### Which departure the cargo can make: `nextDeparture`

A scheduled `departure` is often the answer to "which departure can this cargo still make".
`nextDeparture` reads it off a timetable list or a service every N minutes, after the minimum
connection time, and returns the entry exactly as written so it goes straight into the leg. See
[the guide section](/gmt/guides/industries/transport-punctuality-and-timestamps/#the-next-departure-you-can-make-nextdeparture)
and the [Departure Board](/gmt/tools/departure-board/).

### A departure is exact, or a local time read in a known zone

The first leg needs a departure that is an instant or a zoned string.

```typescript
scheduleDelivery([{ departure: "2024-06-15T10:00:00", duration: "PT1H", timeZone: "UTC" }]); // null — no offset or zone
scheduleDelivery([{ duration: "PT1H", timeZone: "UTC" }]); // null — no departure at all
```

`startTimeZone` reads a zoneless first departure as a published local time. It is ignored when
the departure is already exact, and an invalid `startTimeZone` returns `null` even when nothing
needs it.

```typescript
scheduleDelivery([{ departure: "2024-06-15T10:00:00", duration: "PT1H", timeZone: "UTC" }], { startTimeZone: "Asia/Kolkata" });
// { eta: "2024-06-15T05:30:00+00:00[UTC]", … } — read as 10:00 in Kolkata

scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT1H", timeZone: "UTC" }], { startTimeZone: "Asia/Kolkata" });
// { eta: "2024-06-15T11:00:00+00:00[UTC]", … } — already exact: the option is ignored

scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT1H", timeZone: "UTC" }], { startTimeZone: "Mars/Olympus_Mons" });
// null — an invalid startTimeZone returns null even unused
```

A timetable prints a local time at every hub, not just at the origin. A zoneless departure on a
later leg is read in the previous leg's `timeZone`, because that is where the leg leaves from.
`startTimeZone` never applies to it, and the missed-connection check still runs.

```typescript
scheduleDelivery([
  { departure: "2024-06-15T00:00:00Z", duration: "PT10H", timeZone: "Asia/Tokyo" },
  { departure: "2024-06-15T20:00:00", duration: "PT1H", timeZone: "UTC" },
]);
// { eta: "2024-06-15T12:00:00+00:00[UTC]", … } — leg 1 lands in Tokyo at 19:00, and leg 2's
// printed 20:00 is read on Tokyo's clock: 11:00Z

scheduleDelivery([
  { departure: "2024-06-15T00:00:00Z", duration: "PT10H", timeZone: "Asia/Tokyo" },
  { departure: "2024-06-15T18:30:00", duration: "PT1H", timeZone: "UTC" },
]);
// null — 18:30 in Tokyo is 09:30Z, before leg 1 lands: a missed connection
```

A bracket that contradicts its offset returns `null`.

```typescript
scheduleDelivery([{ departure: "2024-06-15T10:00:00+00:00[Europe/London]", duration: "PT1H", timeZone: "UTC" }]);
// null — London is +01:00 in June: the bracket and the offset disagree
```

The resolution rule, stated once: an ambiguous wall time (a fall-back night's repeated hour)
resolves to the earlier instant, and a nonexistent one (a spring-forward night's skipped hour)
resolves to the later instant. Writing the offset picks the other pass of a repeated hour — but
names no instant in a skipped one, because none exists.

```typescript
// 02:30 on Sydney's fall-back night, with no offset: the earlier pass
scheduleDelivery([{ departure: "2024-04-07T02:30:00[Australia/Sydney]", duration: "PT1H", timeZone: "Australia/Sydney" }]);
// { eta: "2024-04-07T02:30:00+10:00[Australia/Sydney]", … } — it left 15:30Z

// The same 02:30, with +10:00 written: the later pass
scheduleDelivery([{ departure: "2024-04-07T02:30:00+10:00[Australia/Sydney]", duration: "PT1H", timeZone: "Australia/Sydney" }]);
// { eta: "2024-04-07T03:30:00+10:00[Australia/Sydney]", … } — it left 16:30Z

// 01:30 on London's spring-forward night never shows: it resolves to 02:30
scheduleDelivery([{ departure: "2024-03-31T01:30:00[Europe/London]", duration: "PT1H", timeZone: "Europe/London" }]);
// { eta: "2024-03-31T03:30:00+01:00[Europe/London]", … } — it left 01:30Z, 02:30 BST

// Writing an offset into the skipped hour still names nothing: null
scheduleDelivery([{ departure: "2024-03-31T01:30:00+00:00[Europe/London]", duration: "PT1H", timeZone: "Europe/London" }], { startTimeZone: "Europe/London" });
// null — no offset names a time the clock skipped
```

One exception applies at a hub. A printed timetable never says which pass of a repeated hour it
means, so on any leg but the first a repeated hour takes the earliest pass the cargo can catch:
the first at or after the previous arrival plus its dwell. When neither pass is that late, it
takes the earlier pass, and the connection is missed.

```typescript
// Leg 1 lands in London at 01:00 GMT (01:00Z), after the 01:30 BST pass has gone
scheduleDelivery([
  { departure: "2024-10-27T01:00:00Z", duration: "PT0S", timeZone: "Europe/London" },
  { departure: "2024-10-27T01:30:00", duration: "PT1H", timeZone: "UTC" },
]);
// { eta: "2024-10-27T02:30:00+00:00[UTC]", … } — it left at the later pass, 01:30 GMT (01:30Z)
```

Read a printed timetable this way in the [Timetable Reader](/gmt/tools/timetable-reader/), and see
the general DST rule in the [DST Inspector](/gmt/tools/dst-inspector/).

### Tags, zones and the sentinels

`mode`, `origin` and `destination` are opaque tags echoed onto each `LegTime`, and absent when
not supplied. `timeZone` is the caller's fact: GMT resolves no port or station code to a zone. A
fixed offset is accepted as a zone, but observes no DST.

```typescript
scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT1H", timeZone: "UTC", mode: "truck", origin: "CHI", destination: "LAX" }]);
// { …, legTimes: [{ …, mode: "truck", origin: "CHI", destination: "LAX" }] } — all three echoed

scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT1H", timeZone: "+09:00" }]);
// { eta: "2024-06-15T20:00:00+09:00[+09:00]", … } — a fixed offset, no DST
```

Every sentinel and its neighbour: an empty schedule is valid input, not an error; a zero-length
leg passes; a negative leg, a calendar-unit duration, a negative dwell and a non-array input
all return `null`.

```typescript
scheduleDelivery([]); // { eta: "", legTimes: [] } — valid input, not an error
scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "PT0S", timeZone: "UTC" }]);
// { eta: "2024-06-15T10:00:00+00:00[UTC]", … } — a zero-length leg passes
scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "-PT1H", timeZone: "UTC" }]); // null — negative leg
scheduleDelivery([{ departure: "2024-06-15T10:00:00Z", duration: "P1M", timeZone: "UTC" }]); // null — calendar units
scheduleDelivery([
  { departure: "2024-06-15T10:00:00Z", duration: "PT1H", timeZone: "UTC", dwellAfter: "-PT1H" },
  { duration: "PT1H", timeZone: "UTC" },
]); // null — negative dwell
scheduleDelivery("legs"); // null — not an array
```

## A crossing is exact time on the authority's clock: `crossingTime`

[`crossingTime(entry, exit, targetZone)`](/gmt/reference/transport/calculate/crossingTime/) times a
crossing — a canal transit, a strait passage, a border queue — logged as two instants and read
on the clock of the place that administers it. `duration` is exact elapsed time with hours as
the largest unit.

```typescript
crossingTime("2024-06-15T08:00:00Z", "2024-06-15T17:30:00Z", "Europe/Berlin");
// { duration: "PT9H30M", enter: "2024-06-15T10:00:00+02:00[Europe/Berlin]", exit: "2024-06-15T19:30:00+02:00[Europe/Berlin]" }

// Seven elapsed hours, while the wall clocks read 00:00 and 08:00
crossingTime("2024-03-31T00:00:00Z", "2024-03-31T07:00:00Z", "Europe/London");
// { duration: "PT7H", enter: "2024-03-31T00:00:00+00:00[Europe/London]", exit: "2024-03-31T08:00:00+01:00[Europe/London]" }

// One elapsed hour; both ends read 02:30, and the offsets tell them apart
crossingTime("2024-04-06T15:30:00Z", "2024-04-06T16:30:00Z", "Australia/Sydney");
// { duration: "PT1H", enter: "2024-04-07T02:30:00+11:00[Australia/Sydney]", exit: "2024-04-07T02:30:00+10:00[Australia/Sydney]" }
```

`targetZone` is always the rendering zone, and the inputs take no bracket. A fixed
offset renders with no DST: the spring-forward night on a fixed +00:00 reads 07:00, not
London's 08:00.

```typescript
crossingTime("2024-06-15T08:00:00+02:00", "2024-06-15T17:30:00+02:00", "America/Panama");
// { duration: "PT9H30M", enter: "2024-06-15T01:00:00-05:00[America/Panama]", exit: "2024-06-15T10:30:00-05:00[America/Panama]" } — the offset fixes each instant

crossingTime("2024-03-31T00:00:00Z", "2024-03-31T07:00:00Z", "+00:00");
// { duration: "PT7H", enter: "2024-03-31T00:00:00+00:00[+00:00]", exit: "2024-03-31T07:00:00+00:00[+00:00]" } — 07:00, not London's 08:00
```

A zero-length crossing is `PT0S`. An exit before its entry, an unknown zone, or a zoneless time
all return `null`.

```typescript
crossingTime("2024-06-15T10:00:00Z", "2024-06-15T10:00:00Z", "Asia/Tokyo");
// { duration: "PT0S", enter: "2024-06-15T19:00:00+09:00[Asia/Tokyo]", exit: "2024-06-15T19:00:00+09:00[Asia/Tokyo]" }
crossingTime("2024-06-16T01:00:00Z", "2024-06-15T22:30:00Z", "Europe/London"); // null — exit before entry
crossingTime("2024-06-15T22:30:00Z", "2024-06-16T01:00:00Z", "Europe/Londres"); // null — unknown zone
crossingTime("2024-06-15T22:30:00", "2024-06-16T01:00:00Z", "Europe/London"); // null — entry has no offset: not a moment
```

`crossingTime` counts no calendar days — a crossing that needs a day count is a dwell; see
[`dwellTime`](/gmt/reference/transport/calculate/dwellTime/). Open a crossing in the
[Crossing Clock](/gmt/tools/crossing-clock/).

## The exported types

`Crossing`, [`Leg`](/gmt/reference/types/Leg/), `LegTime`,
[`ScheduleDeliveryOptions`](/gmt/reference/types/ScheduleDeliveryOptions/) and `DeliverySchedule` are
exported as types, for the legs you pass in and the schedule you get back.

## See it break, then work

- [Delivery Scheduler: a truck, a ship and a train across the spring-forward](/gmt/tools/delivery-scheduler/)
- [Connection Checker: slide the handling time across the boundary](/gmt/tools/connection-checker/)
- [Timetable Reader: one printed time, two instants](/gmt/tools/timetable-reader/)
- [Crossing Clock: seven hours that read as eight](/gmt/tools/crossing-clock/)
- [A Connection Missed at the Spring-Forward Handoff](/gmt/scenarios/connection-missed-at-spring-forward/)
- [Transport mistakes: multi-leg scheduling](/gmt/mistakes/transport/#multi-leg-scheduling)
