@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.
74 lines • 5.45 kB
TypeScript
/**
* Which date on a bill of lading a value is rendered for, one per dated field of the DCSA
* Bill of Lading 3.0 `TransportDocument`:
*
* - `issue`: the date the B/L was issued, local to the place of issue (DCSA `issueDate`).
* - `received`: the date the last container was in the terminal, customs-cleared against the
* intended vessel, on a received-for-shipment B/L (DCSA `receivedForShipmentDate`).
* - `shippedOnBoard`: the date the last container was loaded aboard the vessel, on a
* shipped-on-board B/L (DCSA `shippedOnBoardDate`).
*
* All three are local calendar dates, so each renders the same way.
*/
export type BillOfLadingEvent = "issue" | "received" | "shippedOnBoard";
/** Options for `bolTimestamp`. */
export interface BolTimestampOptions {
/**
* The zone of the place the event happened, as an IANA name or a UTC offset: the place of issue
* for `issue`, and the terminal at the port of loading for `received` and `shippedOnBoard`. A UTC
* offset is a time zone identifier (`+05:30`, `+0530`, `-08`) or a stored offset (`±HH:MM[:SS]`,
* what `getTimeZoneOffset` returns). It is required, because every B/L date is local and a UTC
* date is a different date for part of every day.
*/
timeZone: string;
}
/**
* Render an instant as the date a bill of lading carries for an event: the calendar date on the
* local clock where the event happened.
*
* A B/L date is a contractual date, not a timestamp. The DCSA Bill of Lading 3.0 standard
* (`TransportDocument`) types `issueDate` ("Local date when the transport document has been
* issued"), `receivedForShipmentDate` and `shippedOnBoardDate` as `format: date`: a date with no
* time and no offset. A system that records the loading as an instant has to turn it back into
* that local date, and taking the UTC date gets it wrong in both directions: 22:00 in São Paulo is
* 01:00Z the next day, and 07:00 in Shanghai is 23:00Z the day before.
*
* - **The result is a date with no time component**, `YYYY-MM-DD`, as
* `Temporal.PlainDate.prototype.toString` writes it. A year outside 0000–9999 is written with a
* sign and six digits (`"+275760-09-13"`).
* - **`timeZone` is required.** Every B/L date is local, so there is no UTC default and no
* system zone: omitting it returns `""`. The zone is the caller's fact; GMT does not resolve a
* port or a place of issue to a zone.
* - **`event` names which date this is** and is checked against the three `BillOfLadingEvent`
* values; anything else returns `""`. All three are local calendar dates, so they render the
* same way: the event keeps the call site honest about which field it fills, and the zone is the
* one that differs by event.
* - `value` is an instant `isValidInstant` accepts: `Z` or an offset, with no annotation of any
* kind. `timeZone` is the zone the date is read in. A zoneless wall time or a bare date names
* no moment and returns `""`.
* - **No disambiguation arises.** The input is a moment, and a moment has exactly one local date
* in any zone. The date changes at the zone's real midnight. A date the zone deleted is never
* returned (`Pacific/Apia` skipped 2011-12-30), and after a fall-back that re-enters the
* previous date the result is that date again, as the clock showed it (`America/Goose_Bay`,
* 7 November 2010).
* - Returns `""` when `value` is not an instant, `event` is not one of the three, `options` is not
* an object, or `timeZone` is missing or invalid.
*
* @param value ISO 8601 instant string of the event (e.g. "2024-06-16T01:00:00Z")
* @param event which B/L date the value is rendered for
* @param options The zone of the place the event happened
* @returns the local calendar date as "YYYY-MM-DD", or "" on invalid input
*
* @example bolTimestamp("2024-06-16T01:00:00Z", "shippedOnBoard", { timeZone: "America/Sao_Paulo" }) // "2024-06-15" (loaded at 22:00 local; the UTC date is the 16th)
* @example bolTimestamp("2024-06-14T23:00:00Z", "shippedOnBoard", { timeZone: "Asia/Shanghai" }) // "2024-06-15" (loaded at 07:00 local; the UTC date is the 14th)
* @example bolTimestamp("2024-06-15T21:00:00+02:00", "received", { timeZone: "Africa/Johannesburg" }) // "2024-06-15"
* @example bolTimestamp("2024-06-16T09:00:00+08:00", "issue", { timeZone: "America/Sao_Paulo" }) // "2024-06-15" (the offset names the moment; `timeZone` is the zone the date is read in)
* @example bolTimestamp("2024-06-16T01:00:00Z", "shippedOnBoard", { timeZone: "+08:00" }) // "2024-06-16" (a fixed offset)
* @example bolTimestamp("2011-12-30T10:00:00Z", "shippedOnBoard", { timeZone: "Pacific/Apia" }) // "2011-12-31" (Samoa deleted 30 December)
* @example bolTimestamp("2024-06-16T01:00:00Z", "shippedOnBoard") // "" (every B/L date is local: the zone is required)
* @example bolTimestamp("2024-06-16T01:00:00Z", "loaded", { timeZone: "Europe/London" }) // "" (not a B/L event)
* @example bolTimestamp("2024-06-15T21:00:00", "shippedOnBoard", { timeZone: "Asia/Kolkata" }) // "" (no offset: not a moment)
* @example bolTimestamp("2024-06-16T01:00:00Z", "shippedOnBoard", { timeZone: "America/Nowhere" }) // ""
* @example bolTimestamp("1970-01-01T00:30:00Z", "issue", { timeZone: "-00:44:30" }) // "1969-12-31" (23:45:30 the day before, at a stored offset with seconds)
*/
export declare function bolTimestamp(value: string, event: BillOfLadingEvent, options: BolTimestampOptions): string;