Singlestat: fix format messes up on negative values if select duratio… (#19044)

* Singlestat: fix format messes up on negative values if select duration (hh:mm:ss) unit

* Added test for 0
This commit is contained in:
lzd 2019-09-13 22:15:31 +08:00 committed by Torkel Ödegaard
parent d55261aac7
commit fc10bd7b8e
2 changed files with 17 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import {
toDuration, toDuration,
toDurationInMilliseconds, toDurationInMilliseconds,
toDurationInSeconds, toDurationInSeconds,
toDurationInHoursMinutesSeconds,
} from './dateTimeFormatters'; } from './dateTimeFormatters';
import { toUtc, dateTime } from '@grafana/data'; import { toUtc, dateTime } from '@grafana/data';
@ -161,6 +162,18 @@ describe('duration', () => {
const str = toDuration(36993906007, 8, Interval.Millisecond); const str = toDuration(36993906007, 8, Interval.Millisecond);
expect(str).toBe('1 year, 2 months, 0 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds'); expect(str).toBe('1 year, 2 months, 0 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds');
}); });
it('1 dthms', () => {
const str = toDurationInHoursMinutesSeconds(1);
expect(str).toBe('00:00:01');
});
it('-1 dthms', () => {
const str = toDurationInHoursMinutesSeconds(-1);
expect(str).toBe('00:00:01 ago');
});
it('0 dthms', () => {
const str = toDurationInHoursMinutesSeconds(0);
expect(str).toBe('00:00:00');
});
}); });
describe('clock', () => { describe('clock', () => {

View File

@ -283,7 +283,10 @@ export function toDurationInSeconds(size: number, decimals: DecimalCount) {
return toDuration(size, decimals, Interval.Second); return toDuration(size, decimals, Interval.Second);
} }
export function toDurationInHoursMinutesSeconds(size: number) { export function toDurationInHoursMinutesSeconds(size: number): string {
if (size < 0) {
return toDurationInHoursMinutesSeconds(-size) + ' ago';
}
const strings = []; const strings = [];
const numHours = Math.floor(size / 3600); const numHours = Math.floor(size / 3600);
const numMinutes = Math.floor((size % 3600) / 60); const numMinutes = Math.floor((size % 3600) / 60);