From: Jérôme Benoit Date: Mon, 24 Jul 2023 00:16:06 +0000 (+0200) Subject: refactor: use helpers to manipulate duration X-Git-Tag: v1.2.20~161 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=f0c6601ce4775df027891c798aee443c2ef9eab2;p=e-mobility-charging-stations-simulator.git refactor: use helpers to manipulate duration Signed-off-by: Jérôme Benoit --- 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,