From 4a024e6f1e7b9164f4f3441973b0f6132c734174 Mon Sep 17 00:00:00 2001 From: Shaisolaris <66793913+Shaisolaris@users.noreply.github.com> Date: Tue, 13 Oct 2026 10:28:31 +0000 Subject: [PATCH] useTime: floor the current second instead of rounding it up 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. --- __tests__/useTime.test.ts | 22 ++++++++++++++++++++++ src/utils/Time.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/__tests__/useTime.test.ts b/__tests__/useTime.test.ts index f89e908..c504393 100644 --- a/__tests__/useTime.test.ts +++ b/__tests__/useTime.test.ts @@ -32,6 +32,28 @@ describe('useTime', () => { }); }); + describe('sub-second time', () => { + beforeEach(() => jest.useFakeTimers()); + afterEach(() => jest.useRealTimers()); + + test('does not round the current second up', () => { + jest.setSystemTime(getLocalDateAtTime(14, 30, 45, 500)); + const { result } = renderHook(() => useTime()); + + expect(result.current.seconds).toBe(45); + expect(result.current.milliseconds).toBe(500); + }); + + test('does not roll minutes and hours over early', () => { + jest.setSystemTime(getLocalDateAtTime(14, 59, 59, 600)); + const { result } = renderHook(() => useTime()); + + expect(result.current.hours).toBe(14); + expect(result.current.minutes).toBe(59); + expect(result.current.seconds).toBe(59); + }); + }); + describe('interval', () => { beforeEach(() => jest.useFakeTimers()); afterEach(() => jest.useRealTimers()); diff --git a/src/utils/Time.ts b/src/utils/Time.ts index 0f71e86..933578c 100644 --- a/src/utils/Time.ts +++ b/src/utils/Time.ts @@ -68,7 +68,7 @@ export default class Time { seconds: secondsValue, minutes, hours, - } = Time.getTimeFromMilliseconds(milliseconds); + } = Time.getTimeFromMilliseconds(milliseconds, false); let ampm: AMPMType = ''; let hoursValue = hours;