From de3272506539665b7ae509b7c056423af1332fb2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 24 Jul 2023 12:41:15 +0200 Subject: [PATCH] fix: ensure daily recurring charging profiles are adjusted on a day granularity MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationUtils.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 1d1119d3..7865eb1f 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 { addSeconds, isAfter, isBefore } from 'date-fns'; +import { addSeconds, isAfter, isTomorrow, isYesterday } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; import { BaseError } from '../exception'; @@ -678,25 +678,21 @@ const getLimitFromChargingProfiles = ( chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!; } // Check type (recurring) and if it is already active - // Adjust the daily recurring schedule + // Adjust the daily recurring schedule to today if ( chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING && chargingProfile.recurrencyKind === RecurrencyKindType.DAILY ) { - if (isBefore(chargingSchedule.startSchedule, currentDate)) { + if (isYesterday(chargingSchedule.startSchedule)) { chargingSchedule.startSchedule.setFullYear( currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), ); - // Check if the start of the schedule must be set to yesterday - } else if (isAfter(chargingSchedule.startSchedule, currentDate)) { + } else if (isTomorrow(chargingSchedule.startSchedule)) { chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1); } } - // if (isAfter(chargingSchedule.startSchedule, currentDate)) { - // return null; - // } // Check if the charging profile is active if ( isAfter(addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration!), currentDate) -- 2.34.1