Fix secondsToTime rounding

This commit is contained in:
Chocobozzz
2026-07-13 14:01:17 +02:00
parent dab0422bf7
commit 84f9172347
2 changed files with 8 additions and 1 deletions
+3 -1
View File
@@ -104,6 +104,8 @@ export function secondsToTime (options: {
if (seconds === 0 && format !== 'full') return '0s'
seconds = Math.round(seconds)
const formatNumber = (value: number) => {
if (format === 'locale-string') return value.toLocaleString()
@@ -125,7 +127,7 @@ export function secondsToTime (options: {
else if (minutes >= 1) time += formatNumber(minutes) + minuteSymbol
else if (format === 'full') time += '00' + minuteSymbol
seconds = Math.round(seconds) % 60
seconds %= 60
if (seconds >= 1 && seconds < 10 && format === 'full') time += '0' + seconds + secondsSymbol
else if (seconds >= 1) time += formatNumber(seconds) + secondsSymbol
else if (format === 'full') time += '00'
+5
View File
@@ -121,6 +121,11 @@ describe('Seconds to time', function () {
expect(secondsToTime(61.6)).to.equals('1m2s')
expect(secondsToTime(61.51)).to.equals('1m2s')
})
it('Carries the rounding into minutes/hours when crossing a boundary', function () {
expect(secondsToTime(59.6)).to.equals('1m')
expect(secondsToTime(3599.6)).to.equals('1h')
})
})
describe('Milliseconds to time', function () {