diff --git a/src/module/timeLabel.js b/src/module/timeLabel.js index 49ddda8..cfe823d 100644 --- a/src/module/timeLabel.js +++ b/src/module/timeLabel.js @@ -131,8 +131,8 @@ export function simpleTimeLabel(second, fixedNumber = 0) { const hour = 60 * 60; const minutes = 60; const dd = Math.floor(second / day); - const hh = Math.floor((second / hour) / day); - const mm = Math.floor((second / hour) / minutes); + const hh = Math.floor((second % day) / hour); + const mm = Math.floor((second % hour) / minutes); if(dd > 0){ return (second / day).toFixed(fixedNumber) + "d"; diff --git a/tests/unit/module/timeLabel.test.js b/tests/unit/module/timeLabel.test.js index 0daa799..52ac23b 100644 --- a/tests/unit/module/timeLabel.test.js +++ b/tests/unit/module/timeLabel.test.js @@ -48,6 +48,16 @@ describe('simpleTimeLabel', () => { expect(simpleTimeLabel(86400)).toBe('1d'); }); + it('returns hours with h suffix', () => { + expect(simpleTimeLabel(3600)).toBe('1h'); + expect(simpleTimeLabel(7200)).toBe('2h'); + }); + + it('returns minutes with m suffix', () => { + expect(simpleTimeLabel(60)).toBe('1m'); + expect(simpleTimeLabel(120)).toBe('2m'); + }); + it('returns seconds with s suffix for small values', () => { expect(simpleTimeLabel(30)).toBe('30s'); }); @@ -55,6 +65,16 @@ describe('simpleTimeLabel', () => { it('returns 0s for zero', () => { expect(simpleTimeLabel(0)).toBe('0s'); }); + + it('correctly calculates hours for values between 1h and 1d', () => { + // 5 hours = 18000 seconds -> should be "5h" + expect(simpleTimeLabel(18000)).toBe('5h'); + }); + + it('correctly calculates minutes for values between 1m and 1h', () => { + // 30 minutes = 1800 seconds -> should be "30m" + expect(simpleTimeLabel(1800)).toBe('30m'); + }); }); describe('followTimeLabel', () => {