Skip to content

Commit f9c3962

Browse files
committed
Squash sdko/closes-13562
1 parent 4e4adcc commit f9c3962

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

Diff for: web/src/common/utils.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,26 @@ export function adaptCSS(sheet: AdaptableStylesheet | AdaptableStylesheet[]): Ad
157157
return Array.isArray(sheet) ? sheet.map(_adaptCSS) : _adaptCSS(sheet);
158158
}
159159

160-
const _timeUnits = new Map<Intl.RelativeTimeFormatUnit, number>([
161-
["year", 24 * 60 * 60 * 1000 * 365],
162-
["month", (24 * 60 * 60 * 1000 * 365) / 12],
163-
["day", 24 * 60 * 60 * 1000],
164-
["hour", 60 * 60 * 1000],
165-
["minute", 60 * 1000],
166-
["second", 1000],
167-
]);
168-
169160
export function getRelativeTime(d1: Date, d2: Date = new Date()): string {
170-
const rtf = new Intl.RelativeTimeFormat("default", { numeric: "auto" });
171161
const elapsed = d1.getTime() - d2.getTime();
162+
const rtf = new Intl.RelativeTimeFormat("default", { numeric: "auto" });
163+
164+
const _timeUnits: [Intl.RelativeTimeFormatUnit, number][] = [
165+
["year", 1000 * 60 * 60 * 24 * 365],
166+
["month", (24 * 60 * 60 * 1000 * 365) / 12],
167+
["day", 1000 * 60 * 60 * 24],
168+
["hour", 1000 * 60 * 60],
169+
["minute", 1000 * 60],
170+
["second", 1000],
171+
];
172172

173-
// "Math.abs" accounts for both "past" & "future" scenarios
174173
for (const [key, value] of _timeUnits) {
175-
if (Math.abs(elapsed) > value || key == "second") {
176-
return rtf.format(Math.round(elapsed / value), key);
174+
if (Math.abs(elapsed) > value || key === "second") {
175+
let rounded = Math.round(elapsed / value);
176+
if (!isFinite(rounded)) {
177+
rounded = 0;
178+
}
179+
return rtf.format(rounded, key);
177180
}
178181
}
179182
return rtf.format(Math.round(elapsed / 1000), "second");

0 commit comments

Comments
 (0)