From f0c6601ce4775df027891c798aee443c2ef9eab2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 24 Jul 2023 02:16:06 +0200 Subject: [PATCH] refactor: use helpers to manipulate duration MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 5841ca19..dc7ad9fe 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,7 +1,13 @@ import { randomBytes, randomInt, randomUUID } from 'node:crypto'; import { inspect } from 'node:util'; -import { formatDuration, secondsToMilliseconds } from 'date-fns'; +import { + formatDuration, + millisecondsToHours, + millisecondsToMinutes, + millisecondsToSeconds, + secondsToMilliseconds, +} from 'date-fns'; import clone from 'just-clone'; import { Constants } from './Constants'; @@ -28,9 +34,11 @@ export const sleep = async (milliSeconds: number): Promise => { export const formatDurationMilliSeconds = (duration: number): string => { duration = convertToInt(duration); const days = Math.floor(duration / (24 * 3600 * 1000)); - const hours = Math.floor(duration / (3600 * 1000) - days * 24); - const minutes = Math.floor(duration / (60 * 1000) - days * 24 * 60 - hours * 60); - const seconds = Math.floor(duration / 1000 - days * 24 * 3600 - hours * 3600 - minutes * 60); + const hours = Math.floor(millisecondsToHours(duration) - days * 24); + const minutes = Math.floor(millisecondsToMinutes(duration) - days * 24 * 60 - hours * 60); + const seconds = Math.floor( + millisecondsToSeconds(duration) - days * 24 * 3600 - hours * 3600 - minutes * 60, + ); return formatDuration({ days, hours, -- 2.34.1