impl/typescript.ts
1,024 bytes · the TypeScript implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { epochDayFromIso } from "./dates_add_days.ts"; ← from dates.add-days ^1.0.0 · built alongside by fune
function minuteOfDay(time: string): number {
const ok =
typeof time === "string" &&
time.length === 5 &&
time[2] === ":" &&
[0, 1, 3, 4].every((i) => time[i] >= "0" && time[i] <= "9");
const hours = ok ? Number(time.slice(0, 2)) : -1;
const minutes = ok ? Number(time.slice(3, 5)) : -1;
if (!ok || hours > 23 || minutes > 59) {
throw new RangeError(`"${time}" is not a time of day (HH:MM, 00:00 to 23:59)`);
}
return hours * 60 + minutes;
}
/**
* Minutes from one local date and time to another: the difference between two
* wall-clock readings. It knows nothing of time zones or daylight saving, so
* across a clock change it differs from real elapsed time by the change.
*/
export function minutesBetween(startDate: string, startTime: string, endDate: string, endTime: string): number {
const days = epochDayFromIso(endDate) - epochDayFromIso(startDate);
return days * 1440 + minuteOfDay(endTime) - minuteOfDay(startTime);
}