@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.
44 lines (42 loc) • 2.35 kB
JavaScript
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
import { getLocaleWeekendDays } from "../../internal/getLocaleWeekendDays.js";
// src/zoned/compare/isZonedWeekend.ts
/**
* Return true when `value`'s local calendar day falls on a weekend for `locale`.
*
* - Uses `Intl.Locale.prototype.weekInfo` to resolve which ISO weekday
* numbers count as "weekend" for the locale (e.g. en-GB: Sat/Sun,
* he-IL/ar-SA: Fri/Sat).
* - Checks the ZonedDateTime's own local day of week — no separate timezone
* conversion needed, since `value` already carries its IANA timezone.
* - Falls back to Saturday/Sunday if the runtime's `weekInfo` data doesn't
* resolve a weekend for the locale.
* - Returns false if `value` is invalid or `locale` is not a well-formed BCP 47 tag
* (ECMA-402 `IsWellFormedLanguageTag`).
* A well-formed tag with no matching locale data is not an error: it falls back to the host's
* default locale, as ECMA-402 `ResolveLocale` requires.
*
* @param value ISO ZonedDateTime string
* @param locale BCP 47 locale tag (e.g. "en-GB", "he-IL"), or a preference list of tags (ECMA-402; the first with locale data is read). Required: omitted, or an empty list (which ECMA-402 would resolve to the host default), returns false
* @returns true if `value`'s local day is a weekend day in `locale`, false on invalid input
*
* @example isZonedWeekend("2024-02-03T10:00:00+00:00[Europe/London]", "en-GB") // true (Saturday)
* @example isZonedWeekend("2024-02-02T10:00:00+02:00[Asia/Jerusalem]", "he-IL") // true (Friday, he-IL weekend is Fri/Sat)
* @example isZonedWeekend("2024-02-04T10:00:00+02:00[Asia/Jerusalem]", "he-IL") // false (Sunday)
* @example isZonedWeekend("invalid", "ja-JP") // false
* @example isZonedWeekend("2024-02-03T10:00:00+09:00[Asia/Tokyo]", "not-a-locale-!!") // false
* @example isZonedWeekend("2024-05-17T12:00:00+02:00[Europe/Berlin]", ["ar-EG", "fr-FR"]) // true
*/
function isZonedWeekend(value, locale) {
if (!isValidZonedDateTime(value)) return false;
const weekendDays = getLocaleWeekendDays(locale);
if (!weekendDays) return false;
try {
const zonedDateTime = zonedDateTimeFrom(value);
return weekendDays.has(zonedDateTime.dayOfWeek);
} catch {
return false;
}
}
export { isZonedWeekend };