mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix format duration rounding days error
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
parent
55687b7d35
commit
20ff8550df
47
src/renderer/utils/__tests__/formatDuration.test.ts
Normal file
47
src/renderer/utils/__tests__/formatDuration.test.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import { formatDuration } from "../formatDuration";
|
||||||
|
import { min } from "moment";
|
||||||
|
|
||||||
|
const second = 1000;
|
||||||
|
const minute = 60 * second;
|
||||||
|
const hour = 60 * minute;
|
||||||
|
const day = 24 * hour;
|
||||||
|
const week = 7 * day;
|
||||||
|
|
||||||
|
describe("human format durations", () => {
|
||||||
|
test("long formatted durations less than 24 hours long shouldn't have a 'd' component", () => {
|
||||||
|
const res = formatDuration(19 * 60 * 60 * 1000, false);
|
||||||
|
|
||||||
|
expect(res).not.toContain("d");
|
||||||
|
expect(res).toBe("19h");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("long formatted durations more than a week have correct day count", () => {
|
||||||
|
const res = formatDuration(2 * week + 2 * day, false);
|
||||||
|
|
||||||
|
expect(res).toBe("2w 2d");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("durations > 1/2 week shouldn't show 1w has passed", () => {
|
||||||
|
const res = formatDuration(5 * 24 * 60 * 60 * 1000, false);
|
||||||
|
|
||||||
|
expect(res).not.toContain("w");
|
||||||
|
expect(res).toBe("5d");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("durations shouldn't include zero magnitude parts", () => {
|
||||||
|
const res = formatDuration(6 * day + 2 * minute, false);
|
||||||
|
|
||||||
|
expect(res).not.toContain("h");
|
||||||
|
expect(res).toBe("6d 2m");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("seconds are ignored unless they are significant (< 1m)", () => {
|
||||||
|
const insignificant = formatDuration(1 * hour + 2 * minute + 31 * second, false);
|
||||||
|
|
||||||
|
expect(insignificant).not.toContain("s");
|
||||||
|
expect(insignificant).toBe("1h 2m");
|
||||||
|
|
||||||
|
const significant = formatDuration(31 * second, false);
|
||||||
|
expect(significant).toBe("31s");
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,24 +1,31 @@
|
|||||||
// Formatting date duration in shorten format, e.g. "2d", or "25m"
|
|
||||||
|
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
|
|
||||||
|
const suffixes = ["w", "d", "h", "m", "s"];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function formats durations in a more human readable form.
|
||||||
|
* @param timeValue the duration in milliseconds to format
|
||||||
|
* @param compact when true, only the largest non-zero time frame will be returned
|
||||||
|
*/
|
||||||
export function formatDuration(timeValue: number, compact: boolean) {
|
export function formatDuration(timeValue: number, compact: boolean) {
|
||||||
let result = "";
|
const duration = moment.duration(timeValue, "milliseconds");
|
||||||
const duration = moment.duration(timeValue);
|
|
||||||
const suffixes = ["d", "h", "m"];
|
|
||||||
const durationValues = [
|
const durationValues = [
|
||||||
Math.round(duration.asDays()),
|
Math.floor(duration.asWeeks()),
|
||||||
|
Math.floor(duration.asDays()) % 7,
|
||||||
duration.hours(),
|
duration.hours(),
|
||||||
duration.minutes(),
|
duration.minutes(),
|
||||||
|
duration.seconds(),
|
||||||
];
|
];
|
||||||
durationValues.forEach((value, index) => {
|
|
||||||
if (value) result += value + suffixes[index] + " ";
|
const meaningfulValues = durationValues
|
||||||
});
|
.map((a, i): [number, string] => [a, suffixes[i]])
|
||||||
|
.filter(([dur, _suf]) => dur > 0)
|
||||||
|
.filter(([_dur, suf], i) => i === 0 || suf !== "s") // remove seconds, unless it is the only one
|
||||||
|
.map(([dur, suf]) => dur + suf);
|
||||||
|
|
||||||
if (compact) {
|
if (compact) {
|
||||||
result = result.split(" ")[0];
|
return meaningfulValues[0];
|
||||||
}
|
}
|
||||||
if (!result) {
|
|
||||||
return "<1m";
|
return meaningfulValues.join(" ");
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user