@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.
76 lines (74 loc) • 6.29 kB
JavaScript
import { transitTimeText } from "../../internal/deliverySchedule.js";
// src/transport/calculate/transitTime.ts
/**
* Add a transit duration to a departure and return the arrival, in the departure's own zone.
*
* The first of the three shared transport operations: a leg is a departure plus how long it
* takes. Every mode publishes it that way, and every mode gets the same thing wrong — a leg
* that crosses a DST transition is a fixed number of *elapsed* hours, not a wall-clock
* interval, so the local arrival time reflects the shift.
*
* - **Transit time is exact time.** Hours, minutes, seconds and fractions are added as elapsed
* time, and a day component means 24 hours, exactly. That is TC39 Temporal's rule:
* `Duration.prototype.round` and `total` without a `relativeTo` treat a day as 24 hours and
* require a `relativeTo` for weeks, months and years. A 26-hour leg that starts the evening
* before a spring-forward arrives at a wall time one hour later than a wall-clock reading would
* suggest, because that is when the vessel, train or aircraft actually gets there.
* - **Calendar units are refused.** A `duration` with years, months or weeks returns `""`: no
* leg takes "a month" in a sense that time arithmetic can fix without a reference point, and
* Temporal's `round` and `total` require a `relativeTo` for them. Scheduled connections that
* are dated rather than timed belong to `scheduleDelivery`.
* - **The departure's zone is preserved.** A bracketed IANA zone (`…+09:00[Asia/Tokyo]`)
* stays that zone, so the arrival's offset is whatever is in force there at arrival. A `Z`
* instant returns a `Z` instant; an offset-only instant keeps its offset, written exactly as
* the departure wrote it, sub-minute offsets such as `+05:30:15` included (an offset is not a
* zone, so nothing else can be inferred from it — see `toOffsetInstant`). A bracketed zone
* that does not exist, or that contradicts its offset, is rejected as `isValidZonedDateTime`
* rejects it; it does not fall back to the offset. An offset written to the minute that is the
* zone's sub-minute offset rounded (`-00:45[Africa/Monrovia]`, for −00:44:30) agrees with the
* zone, as Temporal writes and reads it, and names the instant the zone gives.
* - **One zoned departure in range is refused.** A zoned departure is read as
* `Temporal.ZonedDateTime.from` reads it, which rejects a local date of −271821-04-19. A zone
* west of Greenwich shows that date for the first hours of the instant range, so a departure
* written there (`-271821-04-19T23:59:00-00:01[Europe/London]`) returns `""`, although
* `etaAtZone` writes it and `scheduleDeviation` reads it. Pass the instant in `Z` form.
* - **Local-time resolution policy (`"compatible"`).** A departure written as a wall time with a
* bracketed zone and no offset is resolved in that zone with the `"compatible"` policy,
* Temporal's default and `resolveLocal`'s. An **ambiguous** wall time — a fall-back hour the
* clock ran through twice — resolves to the **earlier** instant:
* `2024-04-07T02:30:00[Australia/Sydney]` is `02:30+11:00`. A **nonexistent** one — a
* spring-forward hour the clock skipped — resolves to the **later** instant:
* `2024-10-06T02:30:00[Australia/Sydney]` is `03:30+11:00`. Write the offset to name the other
* pass of a repeated hour; a departure that carries its offset is never re-resolved.
* - The only annotation read is one time zone after a departure body. A calendar, an elective
* or a critical unknown annotation, or a second bracket, returns `""`, on a zoned departure
* and on an instant alike.
* - A negative duration is allowed and moves backwards; that is how a departure is recovered
* from an arrival.
* - Returns `""` when `departure` is not a zoned datetime or instant string, or `duration` is
* not an ISO 8601 duration.
*
* @param departure ISO 8601 zoned datetime (bracketed zone) or instant string (`Z` or offset)
* @param duration ISO 8601 duration of the leg; time units and 24-hour days only
* @returns the arrival in the departure's zone or offset form, or "" on invalid input
*
* @example transitTime("2024-06-15T10:00:00+09:00[Asia/Tokyo]", "PT2H30M") // "2024-06-15T12:30:00+09:00[Asia/Tokyo]"
* @example transitTime("2024-03-30T23:00:00+00:00[Europe/London]", "PT4H") // "2024-03-31T04:00:00+01:00[Europe/London]" (four elapsed hours across the spring-forward, so the wall clock reads 04:00, not 03:00)
* @example transitTime("2024-03-30T12:00:00+00:00[Europe/London]", "P1D") // "2024-03-31T13:00:00+01:00[Europe/London]" (a day is 24 elapsed hours, not "the same wall time tomorrow")
* @example transitTime("2024-06-15T10:00:00Z", "PT36H") // "2024-06-16T22:00:00Z"
* @example transitTime("2024-06-15T10:00:00+09:00", "PT1H") // "2024-06-15T11:00:00+09:00" (an offset stays an offset)
* @example transitTime("2024-06-15T10:00:00+05:30:15", "PT1H") // "2024-06-15T11:00:00+05:30:15" (a sub-minute offset is kept as written)
* @example transitTime("2024-06-15T10:00:00Z[foo=bar]", "PT1H") // "" (only a time zone bracket is accepted)
* @example transitTime("2024-06-15T12:30:00+09:00[Asia/Tokyo]", "-PT2H30M") // "2024-06-15T10:00:00+09:00[Asia/Tokyo]"
* @example transitTime("2024-04-07T02:30:00[Australia/Sydney]", "PT0S") // "2024-04-07T02:30:00+11:00[Australia/Sydney]" (an ambiguous wall time resolves to the earlier instant)
* @example transitTime("2024-10-06T02:30:00[Australia/Sydney]", "PT0S") // "2024-10-06T03:30:00+11:00[Australia/Sydney]" (a nonexistent wall time resolves to the later instant)
* @example transitTime("2024-06-15T10:00:00+02:00[Africa/Johannesburg]", "P1M") // "" (calendar units need a reference point)
* @example transitTime("2024-06-15T10:00:00+00:00[Europe/London]", "PT2H") // "" (London is +01:00 in June: the offset contradicts the zone)
* @example transitTime("-271821-04-19T23:59:00-00:01[Europe/London]", "PT0S") // "" (a zoned string on a local date before Temporal's date range)
* @example transitTime("2024-06-15T10:00:00", "PT2H") // "" (no zone and no offset: not a moment)
* @example transitTime("2024-06-15T10:00:00Z", "2 hours") // ""
*/
function transitTime(departure, duration) {
return transitTimeText(departure, duration);
}
export { transitTime };