Skip to content

useTime: floor the current second instead of rounding it up - #151

Open
Shaisolaris wants to merge 1 commit into
amrlabib:masterfrom
Shaisolaris:fix-usetime-second-rounding
Open

Shaisolaris wants to merge 1 commit into
amrlabib:masterfrom
Shaisolaris:fix-usetime-second-rounding

Conversation

@Shaisolaris

Copy link
Copy Markdown

useTime shows the clock one second ahead whenever the current time is not on an exact second boundary.

getFormattedTimeFromMilliseconds calls getTimeFromMilliseconds(milliseconds) with the default isCountDown = true, which uses Math.ceil on the seconds. That rounding suits a countdown, but a clock needs to round down. So at 14:30:45.500 useTime() returns seconds: 46 instead of 45, and at 14:59:59.600 it returns hours: 15, minutes: 0, seconds: 0. With the default 1000 ms interval the ticks rarely land on an exact second, so in practice the displayed time is usually one second fast.

This passes false so the time is floored, which is what useStopwatch already does.

Tests: I added two cases to __tests__/useTime.test.ts with sub-second system times. Both fail on master (Expected: 45, Received: 46 and Expected: 14, Received: 15) and pass with the change. The existing tests all used whole seconds, which is why this went unnoticed. npx jest gives 70 passed locally, and eslint src is clean.

getFormattedTimeFromMilliseconds used the countdown rounding (Math.ceil),
so useTime showed the clock one second ahead whenever the current time
was not on an exact second, e.g. 14:59:59.600 displayed as 15:00:00.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant