Skip to content

Commit 1c43556

Browse files
fix: do not display -0 downtime, add downtime tests
1 parent 82d9ec3 commit 1c43556

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/utils/dataFormatters/__test__/formatUptime.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getUptimeFromDateFormatted} from '../dataFormatters';
1+
import {getDowntimeFromDateFormatted, getUptimeFromDateFormatted} from '../dataFormatters';
22

33
describe('getUptimeFromDateFormatted', () => {
44
it('should calculate and format uptime', () => {
@@ -8,3 +8,14 @@ describe('getUptimeFromDateFormatted', () => {
88
expect(getUptimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0:00:00');
99
});
1010
});
11+
describe('getDowntimeFromDateFormatted', () => {
12+
it('should calculate and format downtime as -uptime', () => {
13+
expect(getDowntimeFromDateFormatted(3_600_000, 7_200_000)).toBe('-1:00:00');
14+
});
15+
it('should not add sign if downtime is 0', () => {
16+
expect(getDowntimeFromDateFormatted(3_600_000, 3_600_000)).toBe('0:00:00');
17+
});
18+
it('should return 0 if dateFrom after dateTo', () => {
19+
expect(getDowntimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0:00:00');
20+
});
21+
});

src/utils/dataFormatters/dataFormatters.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ export function getUptimeFromDateFormatted(dateFrom?: number | string, dateTo?:
6666
}
6767

6868
export function getDowntimeFromDateFormatted(dateFrom?: number | string, dateTo?: number | string) {
69-
return '-' + getUptimeFromDateFormatted(dateFrom, dateTo);
69+
let diff = calcTimeDiffInSec(dateFrom, dateTo);
70+
71+
// Our time and server time could differ a little
72+
// Prevent wrong negative uptime values
73+
diff = diff < 0 ? 0 : diff;
74+
75+
const formattedUptime = formatUptimeInSeconds(diff);
76+
77+
// Do not add sign to 0 values to prevent -0:00:00 uptime
78+
return diff === 0 ? formattedUptime : '-' + formattedUptime;
7079
}
7180

7281
export function calcTimeDiffInSec(

0 commit comments

Comments
 (0)