From: Jérôme Benoit Date: Mon, 24 Jul 2023 00:01:17 +0000 (+0200) Subject: feat: improve duration formatting X-Git-Tag: v1.2.20~162 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a675e34bb7d1711aace56f6ed8cdb4f91453e39d;p=e-mobility-charging-stations-simulator.git feat: improve duration formatting Signed-off-by: Jérôme Benoit --- diff --git a/package.json b/package.json index c5ec9e2a..f97ac6db 100644 --- a/package.json +++ b/package.json @@ -104,12 +104,12 @@ "ajv-formats": "^2.1.1", "basic-ftp": "^5.0.3", "chalk": "^5.3.0", + "date-fns": "^2.30.0", "http-status-codes": "^2.2.0", "just-clone": "^6.2.0", "just-merge": "^3.2.0", "logform": "^2.5.1", "mnemonist": "^0.39.5", - "moment": "^2.29.4", "mongodb": "^5.7.0", "poolifier": "^2.6.20", "source-map-support": "^0.5.21", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dfec5549..fd795835 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -36,6 +36,9 @@ dependencies: chalk: specifier: ^5.3.0 version: 5.3.0 + date-fns: + specifier: ^2.30.0 + version: 2.30.0 http-status-codes: specifier: ^2.2.0 version: 2.2.0 @@ -51,9 +54,6 @@ dependencies: mnemonist: specifier: ^0.39.5 version: 0.39.5 - moment: - specifier: ^2.29.4 - version: 2.29.4 mongodb: specifier: ^5.7.0 version: 5.7.0 @@ -290,6 +290,13 @@ packages: js-tokens: 4.0.0 dev: true + /@babel/runtime@7.22.6: + resolution: {integrity: sha512-wDb5pWm4WDdF6LFUde3Jl8WzPA+3ZbxYqkC6xAXuD3irdEHN1k0NfTRrJD8ZD378SJ61miMLCqIOXYhd8x+AJQ==} + engines: {node: '>=6.9.0'} + dependencies: + regenerator-runtime: 0.13.11 + dev: false + /@bcoe/v8-coverage@0.2.3: resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} dev: true @@ -3348,6 +3355,13 @@ packages: engines: {node: '>= 14'} dev: true + /date-fns@2.30.0: + resolution: {integrity: sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==} + engines: {node: '>=0.11'} + dependencies: + '@babel/runtime': 7.22.6 + dev: false + /dateformat@4.6.3: resolution: {integrity: sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA==} dev: true @@ -8042,6 +8056,10 @@ packages: /reflect-metadata@0.1.13: resolution: {integrity: sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==} + /regenerator-runtime@0.13.11: + resolution: {integrity: sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==} + dev: false + /regexp.prototype.flags@1.5.0: resolution: {integrity: sha512-0SutC3pNudRKgquxGoRGIz946MZVHqbNfPjBdxeOhBrdgDKlRoXmYLQN9xRbrR09ZXWeGAdPuif7egofn6v5LA==} engines: {node: '>= 0.4'} diff --git a/rollup.config.mjs b/rollup.config.mjs index 63f8acc7..baaeda78 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -43,12 +43,12 @@ export default { 'ajv-formats', 'basic-ftp', 'chalk', + 'date-fns', 'http-status-codes', 'just-clone', 'just-merge', 'mnemonist/lru-map-with-delete.js', 'mnemonist/queue.js', - 'moment', 'mongodb', 'node:async_hooks', 'node:crypto', diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 78b73647..09e0b8e4 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -4,7 +4,7 @@ import { basename, dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; -import moment from 'moment'; +import { addSeconds, isAfter } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; import { BaseError } from '../exception'; @@ -661,7 +661,6 @@ const getLimitFromChargingProfiles = ( matchingChargingProfile: ChargingProfile; } | null => { const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`; - const currentMoment = moment(); const currentDate = new Date(); for (const chargingProfile of chargingProfiles) { // Set helpers @@ -676,7 +675,7 @@ const getLimitFromChargingProfiles = ( if ( chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING && chargingProfile.recurrencyKind === RecurrencyKindType.DAILY && - currentMoment.isAfter(chargingSchedule.startSchedule) + isAfter(currentDate, chargingSchedule.startSchedule!) ) { if (!(chargingSchedule?.startSchedule instanceof Date)) { logger.warn( @@ -690,17 +689,15 @@ const getLimitFromChargingProfiles = ( currentDate.getDate(), ); // Check if the start of the schedule is yesterday - if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) { + if (isAfter(chargingSchedule.startSchedule, currentDate)) { chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1); } - } else if (moment(chargingSchedule.startSchedule).isAfter(currentMoment)) { + } else if (isAfter(chargingSchedule.startSchedule!, currentDate)) { return null; } // Check if the charging profile is active if ( - moment(chargingSchedule.startSchedule) - .add(chargingSchedule.duration, 's') - .isAfter(currentMoment) + isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate) ) { let lastButOneSchedule: ChargingSchedulePeriod | undefined; // Search the right schedule period @@ -719,9 +716,10 @@ const getLimitFromChargingProfiles = ( } // Find the right schedule period if ( - moment(chargingSchedule.startSchedule) - .add(schedulePeriod.startPeriod, 's') - .isAfter(currentMoment) + isAfter( + addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod), + currentDate, + ) ) { // Found the schedule: last but one is the correct one const result = { diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index c2c9d0ad..5841ca19 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,6 +1,7 @@ import { randomBytes, randomInt, randomUUID } from 'node:crypto'; import { inspect } from 'node:util'; +import { formatDuration, secondsToMilliseconds } from 'date-fns'; import clone from 'just-clone'; import { Constants } from './Constants'; @@ -26,27 +27,20 @@ export const sleep = async (milliSeconds: number): Promise => { export const formatDurationMilliSeconds = (duration: number): string => { duration = convertToInt(duration); - const hours = Math.floor(duration / (3600 * 1000)); - const minutes = Math.floor((duration / 1000 - hours * 3600) / 60); - const seconds = duration / 1000 - hours * 3600 - minutes * 60; - let hoursStr = hours.toString(); - let minutesStr = minutes.toString(); - let secondsStr = seconds.toString(); - - if (hours < 10) { - hoursStr = `0${hours.toString()}`; - } - if (minutes < 10) { - minutesStr = `0${minutes.toString()}`; - } - if (seconds < 10) { - secondsStr = `0${seconds.toString()}`; - } - return `${hoursStr}:${minutesStr}:${secondsStr.substring(0, 6)}`; + 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); + return formatDuration({ + days, + hours, + minutes, + seconds, + }); }; export const formatDurationSeconds = (duration: number): string => { - return formatDurationMilliSeconds(duration * 1000); + return formatDurationMilliSeconds(secondsToMilliseconds(duration)); }; export const convertToDate = (