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.

72 lines (70 loc) • 4.34 kB
import { Temporal } from "../../internal/temporal.js"; import { timeZoneEquals } from "../../internal/temporalCompat/timeZoneEquals.js"; import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { isDateTimeUnitName } from "../../internal/unitNames.js"; import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; import { resolveWeekStartsOn } from "../../internal/resolveWeekStartsOn.js"; import { startOrEndOfZoned } from "../../internal/startOrEndOfZoned.js"; // src/zoned/compare/areZonedEqualBy.ts /** * Compare two zoned ISO datetime strings for equality at a given unit. * * - Equality is measured on each value's own local wall-clock fields (its own * time zone), not on the underlying instant or time zone identifier — the * same instant can be "the same day" in one zone and a different day in * another, and this function answers per each value's own zone, because a * `Temporal.ZonedDateTime` reads its calendar and clock fields in its own * time zone. * - Each value's start-of-unit is its real local bucket (see `startOfZoned`), so a bucket a * zone transition shortened is still its own unit: in `Pacific/Chatham` on its 2024 * spring-forward, 03:50 and 04:05 are different hours, because the 03:00 hour lasted only * from 03:45 to 04:00. * - Two values in the same time zone (two identifiers of one zone, such as `UTC` and `Etc/UTC`, count as * one) are equal only when their bucket starts are the same * instant, as in `areUnixEqualBy`: both passes of London's repeated 01:00 on its 2024 * fall-back read the same wall clock but are different hours. Values in different zones are * compared by their buckets' local labels, since their instants never line up. * - `"month"` requires the same month AND year, matching `areDateTimesEqualBy`. * - `unit` accepts the singular or plural name (`"day"` or `"days"`), as Temporal does. * - Returns false for an unsupported unit or invalid input. * * Migrating from date-fns: * - `isSameDay(a, b)` → `areZonedEqualBy(a, b, "day")` * - `isSameMonth(a, b)` → `areZonedEqualBy(a, b, "month")` * - `isSameYear(a, b)` → `areZonedEqualBy(a, b, "year")` * * @param value1 first zoned ISO datetime string * @param value2 second zoned ISO datetime string * @param unit date or time unit, singular or plural, to compare by * @param optionsArg optional setting for the week's first day * @returns true if both values share the same local start-of-unit boundary, false on an unsupported unit or invalid input * * @example areZonedEqualBy("2024-03-15T10:00:00+09:00[Asia/Tokyo]", "2024-03-15T20:00:00+01:00[Europe/Berlin]", "day") // true (both are local March 15 in their own zone) * @example areZonedEqualBy("2024-03-15T10:00:00+09:00[Asia/Tokyo]", "2024-03-15T20:00:00+01:00[Europe/Berlin]", "days") // true (plural unit) * @example areZonedEqualBy("2024-03-15T23:30:00-03:00[America/Sao_Paulo]", "2024-03-16T02:30:00+00:00[UTC]", "day") // false (same instant, different local calendar day per zone) * @example areZonedEqualBy("2024-10-27T01:30:00+01:00[Europe/London]", "2024-10-27T01:30:00+00:00[Europe/London]", "hour") // false (both read 01:30, but they sit in the two different real 01:00 hours) * @example areZonedEqualBy("invalid", "2024-03-15T10:00:00+02:00[Africa/Johannesburg]", "day") // false */ function areZonedEqualBy(value1, value2, unit, optionsArg) { try { if (!isOptionsArgument(optionsArg)) return false; const resolvedUnit = resolveDateTimeUnit(unit); const weekStartsOn = resolveWeekStartsOn(optionsArg?.weekStartsOn); if (!isDateTimeUnitName(resolvedUnit) || weekStartsOn === null) return false; try { const start1 = startOrEndOfZoned(value1, resolvedUnit, { weekStartsOn }, false); const start2 = startOrEndOfZoned(value2, resolvedUnit, { weekStartsOn }, false); if (start1 === "" || start2 === "") return false; const zoned1 = zonedDateTimeFrom(start1); const zoned2 = zonedDateTimeFrom(start2); if (timeZoneEquals(zoned1, zoned2)) return Temporal.Instant.compare(zoned1.toInstant(), zoned2.toInstant()) === 0; return Temporal.PlainDateTime.compare(zoned1.toPlainDateTime(), zoned2.toPlainDateTime()) === 0; } catch { return false; } } catch { return false; } } export { areZonedEqualBy };