From 2782b2fb1b51d6dba1bbacaa689d9bdab545a28b Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:47:14 +0100 Subject: [PATCH] Use DurationFormat for calendar trigger (#23020) duration long --- src/common/datetime/format_duration.ts | 61 +++----------------------- 1 file changed, 5 insertions(+), 56 deletions(-) diff --git a/src/common/datetime/format_duration.ts b/src/common/datetime/format_duration.ts index 973d7ce9273f..1639e08bdae0 100644 --- a/src/common/datetime/format_duration.ts +++ b/src/common/datetime/format_duration.ts @@ -1,6 +1,6 @@ +import { DurationFormat } from "@formatjs/intl-durationformat"; import type { HaDurationData } from "../../components/ha-duration-input"; import type { FrontendLocaleData } from "../../data/translation"; -import { formatListWithAnds } from "../string/format-list"; const leftPad = (num: number) => (num < 10 ? `0${num}` : num); @@ -47,58 +47,7 @@ export const formatNumericDuration = ( export const formatDurationLong = ( locale: FrontendLocaleData, duration: HaDurationData -) => { - const d = duration.days || 0; - const h = duration.hours || 0; - const m = duration.minutes || 0; - const s = duration.seconds || 0; - const ms = duration.milliseconds || 0; - - const parts: string[] = []; - if (d > 0) { - parts.push( - Intl.NumberFormat(locale.language, { - style: "unit", - unit: "day", - unitDisplay: "long", - }).format(d) - ); - } - if (h > 0) { - parts.push( - Intl.NumberFormat(locale.language, { - style: "unit", - unit: "hour", - unitDisplay: "long", - }).format(h) - ); - } - if (m > 0) { - parts.push( - Intl.NumberFormat(locale.language, { - style: "unit", - unit: "minute", - unitDisplay: "long", - }).format(m) - ); - } - if (s > 0) { - parts.push( - Intl.NumberFormat(locale.language, { - style: "unit", - unit: "second", - unitDisplay: "long", - }).format(s) - ); - } - if (ms > 0) { - parts.push( - Intl.NumberFormat(locale.language, { - style: "unit", - unit: "millisecond", - unitDisplay: "long", - }).format(ms) - ); - } - return formatListWithAnds(locale, parts); -}; +) => + new DurationFormat(locale.language, { + style: "long", + }).format(duration);