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.

61 lines (59 loc) • 3.49 kB
import { fractionalDigitsOptionOrDefault } from "../../internal/typedOptions.js"; import { zonedDateTimeOf } from "../../internal/zonedWallClock.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { zonedUnitEnd } from "../../internal/zonedBucket.js"; import { getLocaleFirstDayOfWeek } from "../../internal/getLocaleFirstDayOfWeek.js"; // src/zoned/calculate/getLocaleZonedEndOfWeek.ts /** * Return the end of the week containing `value`, using `locale`'s first * day of week (e.g. fr-FR: week ends Sunday, ja-JP: week ends Saturday). * * - Resolves the locale's first day of week via * `Intl.Locale.prototype.weekInfo`. * - Falls back to Monday-start (so the week ends Sunday) if the runtime's * `weekInfo` data doesn't resolve a first day for the locale. * - Returns the last instant of the real local week containing `value` in * its own zone — one nanosecond before the next week bucket starts — so * the result is never before `value`. * - The end is written at nanosecond precision by default (`.999999999`), like `endOfZoned`, so the * string names the end itself. Pass `{ fractionalSecondDigits: 0 }` to keep the previous * whole-second string. * - Distinct from `endOfZoned(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", "ja-JP", "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 end of `value`'s locale-relative week, or "" on invalid input * * @example getLocaleZonedEndOfWeek("2024-02-29T12:00:00+00:00[UTC]", "ja-JP") // "2024-03-02T23:59:59.999999999+00:00[UTC]" (Saturday) * @example getLocaleZonedEndOfWeek("2024-02-29T12:00:00+00:00[UTC]", "fr-FR") // "2024-03-03T23:59:59.999999999+00:00[UTC]" (Sunday) * @example getLocaleZonedEndOfWeek("2024-02-29T12:00:00+00:00[UTC]", "ja-JP", { fractionalSecondDigits: 0 }) // "2024-03-02T23:59:59+00:00[UTC]" (the pre-1.16.0 string) * @example getLocaleZonedEndOfWeek("invalid", "de-DE") // "" * @example getLocaleZonedEndOfWeek("2024-02-29T12:00:00+00:00[UTC]", "not-a-locale-!!") // "" * @example getLocaleZonedEndOfWeek("2024-05-15T12:00:00+02:00[Europe/Berlin]", ["fr-FR", "en-US"]) // "2024-05-19T23:59:59.999999999+02:00[Europe/Berlin]" */ function getLocaleZonedEndOfWeek(value, locale, optionsArg) { try { if (!isOptionsArgument(optionsArg)) return ""; const fractionalSecondDigits = fractionalDigitsOptionOrDefault(optionsArg?.fractionalSecondDigits, 9); const zonedDateTime = zonedDateTimeOf(value); if (zonedDateTime === null) return ""; const firstDay = getLocaleFirstDayOfWeek(locale); if (firstDay === null) return ""; try { const end = zonedUnitEnd(zonedDateTime, "week", firstDay); return end ? end.toString({ fractionalSecondDigits }) : ""; } catch { return ""; } } catch { return ""; } } export { getLocaleZonedEndOfWeek };