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.

61 lines (59 loc) • 3.41 kB
import { isOptionsArgument } from "../../internal/isObject.js"; import { isDateTimeUnitName } from "../../internal/unitNames.js"; import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; import { startOrEndOfUnix } from "../../internal/startOrEndOfUnix.js"; // src/unix/compare/areUnixEqualBy.ts /** * Compare two Unix epoch values for equality at a given unit. * * - Both values are resolved to the start of `unit` in `options.timeZone` before comparison, * so `"day"` compares calendar days in that zone, not raw epoch buckets. * - The start of `unit` is the real local bucket (see `startOfUnix`), so two epochs are equal * only when they fall in the same bucket — including one a zone transition shortened, like * `Pacific/Chatham`'s 15-minute 03:00 hour on its 2024 spring-forward. * - `"month"` requires the same month AND year, matching `areDateTimesEqualBy`. * - Each value is a safe integer or a string of optionally negative ASCII digits; `unit` may be * singular or plural. * - Returns false for an unsupported unit (including `"quarter"`) or invalid input. * - **Limit at an offset with seconds.** The instant is placed in the offset's whole-minute zone, * moved by its seconds, and the moved instant must be inside Temporal's range. So within the * offset's seconds (under a minute) of the last instant Temporal supports * (`+275760-09-13T00:00:00Z`) for an offset east of UTC, or of the first * (`-271821-04-20T00:00:00Z`) for one west, this returns `false`. There `false` is the sentinel, * not a comparison. An offset to the minute has no such limit. * * Migrating from date-fns: * - `isSameDay(a, b)` → `areUnixEqualBy(a, b, "day")` * - `isSameMonth(a, b)` → `areUnixEqualBy(a, b, "month")` * - `isSameYear(a, b)` → `areUnixEqualBy(a, b, "year")` * * @param value1 first Unix epoch: a safe integer or a digit string * @param value2 second Unix epoch, in the same unit * @param unit Temporal.DateUnit | Temporal.TimeUnit to compare by * @param options optional: how both epochs are read, the zone the unit is found in and the first day of the week * @returns true if both values share the same start-of-unit boundary, false on an unsupported unit or invalid input * * @example areUnixEqualBy(1710498000000, 1710503000000, "day", { timeZone: "UTC" }) // true (both fall on 2024-03-15 in UTC) * @example areUnixEqualBy(1704067200000, 1735689600000, "year", { timeZone: "UTC" }) // false (2024-01-01 vs 2025-01-01) * @example areUnixEqualBy("1710498000000", 1710503000000, "days") // true (digit string, plural unit, UTC by default) * @example areUnixEqualBy(Number.NaN, 1710498000000, "day") // false * @example areUnixEqualBy(2669999, 2670000, "day", { timeZone: "-00:44:30" }) // false (the local day turns at 00:44:30Z) */ function areUnixEqualBy(value1, value2, unit, options) { if (!isOptionsArgument(options)) return false; try { const read = { epochUnit: options?.epochUnit, timeZone: options?.timeZone, weekStartsOn: options?.weekStartsOn }; const resolvedUnit = typeof unit === "string" ? resolveDateTimeUnit(unit) : unit; if (!isDateTimeUnitName(resolvedUnit)) return false; const start1 = startOrEndOfUnix(value1, resolvedUnit, read, false); const start2 = startOrEndOfUnix(value2, resolvedUnit, read, false); return start1 !== null && start1 === start2; } catch { return false; } } export { areUnixEqualBy };