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.

64 lines • 3.69 kB
import { Temporal } from "../../internal/temporal.js"; import { UnixUnit } from "../validate/isValidUnixUnit.js"; /** * 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) */ export declare function areUnixEqualBy(value1: number | string, value2: number | string, unit: Temporal.DateTimeUnit | Temporal.PluralUnit<Temporal.DateTimeUnit>, options?: { /** * The unit the epoch values are counted in: `"seconds"` or `"milliseconds"`, singular or * plural. Any other value returns `false`. * * @defaultValue `"milliseconds"` */ epochUnit?: UnixUnit; /** * The time zone the unit boundaries are found in: an IANA name, a UTC offset (a time zone * identifier such as `+05:30`, `+0530` or `-08`, or a stored offset `±HH:MM[:SS]`, what * `getTimeZoneOffset` returns), or `"local"` for the system time zone. An unknown zone returns * `false`. * * @defaultValue `"UTC"` */ timeZone?: string; /** * The first day of the week, which sets where a `"week"` unit starts. `"monday"` is the ISO * 8601 week, Monday to Sunday; `"sunday"` runs Sunday to Saturday. Any other value returns * false, whatever the unit. * * @defaultValue `"monday"` */ weekStartsOn?: "monday" | "sunday"; }): boolean;