@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.31 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { optionOrDefault } from "../../internal/optionOrDefault.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
import { zonedUnitStart } from "../../internal/zonedBucket.js";
import { getLocaleFirstDayOfWeek } from "../../internal/getLocaleFirstDayOfWeek.js";
// src/zoned/calculate/getLocaleZonedStartOfWeek.ts
/**
* Return the start of the week containing `value`, using `locale`'s
* first day of week (e.g. fr-FR: Monday, hi-IN: Sunday).
*
* - 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 the real start of the local week containing `value` in its own
* zone — the same kind of bucket `floorToZone` uses — so the result is
* never after `value`. A week whose first midnight was skipped starts at
* its first real instant.
* - Distinct from `startOfZoned(value, "week", { weekStartsOn })`, which
* takes an explicit ISO-biased `weekStartsOn` option instead of deriving
* it from a locale.
* - Returns "" 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 zoned ISO 8601 datetime string
* @param locale BCP 47 locale tag (e.g. "fr-FR", "hi-IN", "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 ""
* @param optionsArg optional setting for the precision of the output string
* @returns zoned ISO 8601 string for the start of `value`'s locale-relative week, or "" on invalid input
*
* @example getLocaleZonedStartOfWeek("2024-02-29T12:00:00+00:00[UTC]", "hi-IN") // "2024-02-25T00:00:00+00:00[UTC]" (Sunday)
* @example getLocaleZonedStartOfWeek("2024-02-29T12:00:00+00:00[UTC]", "fr-FR") // "2024-02-26T00:00:00+00:00[UTC]" (Monday)
* @example getLocaleZonedStartOfWeek("2024-09-11T12:00:00-03:00[America/Santiago]", "pt-BR") // "2024-09-08T01:00:00-03:00[America/Santiago]" (that Sunday's midnight was skipped)
* @example getLocaleZonedStartOfWeek("invalid", "ja-JP") // ""
* @example getLocaleZonedStartOfWeek("2024-02-29T12:00:00+00:00[UTC]", "not-a-locale-!!") // ""
* @example getLocaleZonedStartOfWeek("2024-05-15T12:00:00+02:00[Europe/Berlin]", ["hi-IN", "fr-FR"]) // "2024-05-12T00:00:00+02:00[Europe/Berlin]"
*/
function getLocaleZonedStartOfWeek(value, locale, optionsArg) {
try {
if (!isOptionsArgument(optionsArg)) return "";
const fractionalSecondDigits = optionOrDefault(optionsArg?.fractionalSecondDigits, 0);
if (!isValidZonedDateTime(value)) return "";
const firstDay = getLocaleFirstDayOfWeek(locale);
if (firstDay === null) return "";
try {
const start = zonedUnitStart(zonedDateTimeFrom(value), "week", firstDay);
return start ? start.toString({ fractionalSecondDigits }) : "";
} catch {
return "";
}
} catch {
return "";
}
}
export { getLocaleZonedStartOfWeek };