UNPKG

@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.

32 lines (30 loc) • 1.31 kB
import { plainTimeOf } from "../../internal/inputChecks.js"; // src/plain/compare/areTimesEqual.ts /** * Return true if two PlainTime ISO strings represent the same time components. * * - Compares all time components: hour, minute, second, millisecond, microsecond, nanosecond. * - Returns false if either input is invalid. * * @param value1 first ISO PlainTime string * @param value2 second ISO PlainTime string * @returns boolean indicating whether both times are equal component-wise * * @example areTimesEqual("12:34:56", "12:34:56") // true * @example areTimesEqual("12:34:56", "12:34:57") // false * @example areTimesEqual("12:34:56.789", "12:34:56.789") // true * @example areTimesEqual("12:34:56.789", "12:34:56.790") // false * @example areTimesEqual("invalid", "12:34:56") // false * @example areTimesEqual("12:34:56", "invalid") // false */ function areTimesEqual(value1, value2) { const time1 = plainTimeOf(value1); const time2 = plainTimeOf(value2); if (time1 === null || time2 === null) return false; try { return time1.hour === time2.hour && time1.minute === time2.minute && time1.second === time2.second && time1.millisecond === time2.millisecond && time1.microsecond === time2.microsecond && time1.nanosecond === time2.nanosecond; } catch { return false; } } export { areTimesEqual };