Skip to content

Commit 197fb68

Browse files
committed
duration long
1 parent a532b44 commit 197fb68

File tree

1 file changed

+5
-56
lines changed

1 file changed

+5
-56
lines changed
+5-56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { DurationFormat } from "@formatjs/intl-durationformat";
12
import type { HaDurationData } from "../../components/ha-duration-input";
23
import type { FrontendLocaleData } from "../../data/translation";
3-
import { formatListWithAnds } from "../string/format-list";
44

55
const leftPad = (num: number) => (num < 10 ? `0${num}` : num);
66

@@ -47,58 +47,7 @@ export const formatNumericDuration = (
4747
export const formatDurationLong = (
4848
locale: FrontendLocaleData,
4949
duration: HaDurationData
50-
) => {
51-
const d = duration.days || 0;
52-
const h = duration.hours || 0;
53-
const m = duration.minutes || 0;
54-
const s = duration.seconds || 0;
55-
const ms = duration.milliseconds || 0;
56-
57-
const parts: string[] = [];
58-
if (d > 0) {
59-
parts.push(
60-
Intl.NumberFormat(locale.language, {
61-
style: "unit",
62-
unit: "day",
63-
unitDisplay: "long",
64-
}).format(d)
65-
);
66-
}
67-
if (h > 0) {
68-
parts.push(
69-
Intl.NumberFormat(locale.language, {
70-
style: "unit",
71-
unit: "hour",
72-
unitDisplay: "long",
73-
}).format(h)
74-
);
75-
}
76-
if (m > 0) {
77-
parts.push(
78-
Intl.NumberFormat(locale.language, {
79-
style: "unit",
80-
unit: "minute",
81-
unitDisplay: "long",
82-
}).format(m)
83-
);
84-
}
85-
if (s > 0) {
86-
parts.push(
87-
Intl.NumberFormat(locale.language, {
88-
style: "unit",
89-
unit: "second",
90-
unitDisplay: "long",
91-
}).format(s)
92-
);
93-
}
94-
if (ms > 0) {
95-
parts.push(
96-
Intl.NumberFormat(locale.language, {
97-
style: "unit",
98-
unit: "millisecond",
99-
unitDisplay: "long",
100-
}).format(ms)
101-
);
102-
}
103-
return formatListWithAnds(locale, parts);
104-
};
50+
) =>
51+
new DurationFormat(locale.language, {
52+
style: "long",
53+
}).format(duration);

0 commit comments

Comments
 (0)