@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.
37 lines (35 loc) • 1.54 kB
JavaScript
import { isObject } from "../../internal/isObject.js";
import { writeGtfsTime } from "../../internal/gtfsTime.js";
// src/rail/format/formatGtfsTime.ts
/**
* Writes a GTFS Schedule `Time` value, keeping the days past the service day in the hour.
*
* - Writes `HH:MM:SS` with at least two hour digits: `08:05:00`, not `8:05:00`; `110:29:00`.
* - `dayOverflow` adds 24 hours a day, so `{ seconds: 5700, dayOverflow: 1 }` is `25:35:00`,
* never `01:35:00`.
* - A value over `999:59:59` cannot be written in three digits and returns `""`.
* - The argument is read like a property bag: any object, a function included; each member is
* read once, inherited members are read and extra members are ignored.
*
* Source: GTFS Schedule Reference § Field Types, Time.
*
* @param value The written time less whole 24-hour blocks (`seconds`, 0–86399) and the whole days
* past the service day (`dayOverflow`, 0 or more).
* @returns The GTFS `Time` string, or `""` on invalid input (not a `GtfsTime`, or past
* `999:59:59`)
*
* @example formatGtfsTime({ seconds: 5700, dayOverflow: 1 }) // "25:35:00"
* @example formatGtfsTime({ seconds: 29100, dayOverflow: 0 }) // "08:05:00"
* @example formatGtfsTime({ seconds: 0, dayOverflow: 1 }) // "24:00:00"
* @example formatGtfsTime({ seconds: 86400, dayOverflow: 0 }) // ""
*/
function formatGtfsTime(value) {
try {
if (!isObject(value)) return "";
const { seconds, dayOverflow } = value;
return writeGtfsTime(seconds, dayOverflow);
} catch {
return "";
}
}
export { formatGtfsTime };