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.

59 lines (57 loc) • 3.45 kB
import { plainDateOf } from "../../internal/inputChecks.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { getLocaleFirstDayOfWeek } from "../../internal/getLocaleFirstDayOfWeek.js"; import { resolveMinimalDaysInFirstWeek } from "../../internal/resolveMinimalDaysInFirstWeek.js"; import { getLocaleWeekYearBounds } from "../../internal/getLocaleWeekYearBounds.js"; // src/plain/calculate/getWeeksInLocaleWeekYear.ts /** * Return the number of weeks (52 or 53) in the locale week-numbering year containing `value`, from * `locale`'s first day of week and a minimal-days-in-first-week rule. * * - `getWeeksInYear`'s locale-relative counterpart: that function uses the fixed ISO rule, this one * uses `locale`'s first day of week, and the two can disagree on the same date. * - Computed as the calendar days between this week-year's start and the next one's, divided by 7 — * always whole, since both starts fall on the locale's first day of week. Counted by day * arithmetic, so a start outside the representable range still counts. * - The minimal-days rule comes from the caller, never the runtime: ECMA-402 does not expose a * locale's minimal days (tc39/proposal-intl-locale-info#86). CLDR 48's world default is `1` * (`US` included) and `4` is set for mostly European regions; pass the locale's value when you * need its own rule. See `getLocaleWeekYear`. * - Returns null if `value` or `locale` is invalid. * - **Compatibility:** before 1.16.0 the default read the runtime's `minimalDays` where one was * exposed (Node 22), so a locale such as `en-US` got `1` there and `4` on Node 24 and later. Pass * `{ minimalDays: 1 }` to keep the Node 22 result for such a locale on every runtime. * * @param value ISO PlainDate string * @param locale BCP 47 locale tag (e.g. "fr-FR", "ja-JP", "en-US"), 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 * @param options The minimal-days-in-first-week rule * @returns 52 or 53, or null on invalid input * * @example getWeeksInLocaleWeekYear("2024-06-15", "pt-BR") // 52 * @example getWeeksInLocaleWeekYear("2022-06-15", "ja-JP", { minimalDays: 1 }) // 53 — Dec 26, 2021 to Jan 1, 2023 * @example getWeeksInLocaleWeekYear("2022-06-15", "ja-JP") // 52 — ISO default: Jan 2, 2022 to Jan 1, 2023 * @example getWeeksInLocaleWeekYear("2020-06-15", "de-DE") // 53 * @example getWeeksInLocaleWeekYear("2024-06-15", "hi-IN", { minimalDays: 8 }) // null * @example getWeeksInLocaleWeekYear("+275760-09-13", "pt-BR") // 53 (its end, the week 1 of +275761, lies past the range) * @example getWeeksInLocaleWeekYear("invalid", "ar-EG") // null * @example getWeeksInLocaleWeekYear("2024-12-29", ["fr-FR", "en-US"]) // 52 */ function getWeeksInLocaleWeekYear(value, locale, options) { try { if (!isOptionsArgument(options)) return null; const date = plainDateOf(value); if (date === null) return null; const firstDay = getLocaleFirstDayOfWeek(locale); const minimalDays = resolveMinimalDaysInFirstWeek(options); if (firstDay === null || minimalDays === null) return null; try { const { startOffsetDays, endOffsetDays } = getLocaleWeekYearBounds(date, firstDay, minimalDays); return (endOffsetDays - startOffsetDays) / 7; } catch { return null; } } catch { return null; } } export { getWeeksInLocaleWeekYear };