@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.
41 lines (39 loc) • 2.2 kB
JavaScript
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
import { getLocaleFirstDayOfWeek } from "../../internal/getLocaleFirstDayOfWeek.js";
// src/zoned/calculate/getLocaleZonedDayOfWeek.ts
/**
* Return the locale-relative day-of-week index for a zoned `value`.
*
* - `0` = the locale's first day of week (e.g. Monday for fr-FR, Sunday for pt-BR,
* Saturday for ar-EG).
* - Resolves the locale's first day of week via `Intl.Locale.prototype.weekInfo`.
* - Falls back to Monday if the runtime's `weekInfo` data doesn't resolve
* a first day for the locale.
* - Returns `null` if `value` is not a valid zoned ISO datetime 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 zoned ISO 8601 datetime string
* @param locale BCP 47 locale tag (e.g. "fr-FR", "pt-BR", "ar-EG"), 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 null
* @returns locale-relative day-of-week index (0–6) or null on invalid input
*
* @example getLocaleZonedDayOfWeek("2024-02-25T12:00:00+00:00[UTC]", "pt-BR") // 0 (Sunday)
* @example getLocaleZonedDayOfWeek("2024-02-26T12:00:00+00:00[UTC]", "ja-JP") // 1 (Monday)
* @example getLocaleZonedDayOfWeek("2024-02-26T12:00:00+00:00[UTC]", "fr-FR") // 0 (Monday)
* @example getLocaleZonedDayOfWeek("invalid-zoned", "en-GB") // null
* @example getLocaleZonedDayOfWeek("2024-02-26T12:00:00+00:00[UTC]", "not-a-locale-!!") // null
* @example getLocaleZonedDayOfWeek("2024-05-15T12:00:00+02:00[Europe/Berlin]", ["fr-FR", "en-US"]) // 2
*/
function getLocaleZonedDayOfWeek(value, locale) {
if (!isValidZonedDateTime(value)) return null;
const firstDay = getLocaleFirstDayOfWeek(locale);
if (firstDay === null) return null;
try {
return (zonedDateTimeFrom(value).dayOfWeek - firstDay + 7) % 7;
} catch {
return null;
}
}
export { getLocaleZonedDayOfWeek };