Skip to content

Commit

Permalink
Use DurationFormat for calendar trigger (#23020)
Browse files Browse the repository at this point in the history
duration long
  • Loading branch information
silamon authored Nov 27, 2024
1 parent a532b44 commit 2782b2f
Showing 1 changed file with 5 additions and 56 deletions.
61 changes: 5 additions & 56 deletions src/common/datetime/format_duration.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -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);

0 comments on commit 2782b2f

Please sign in to comment.