diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/GridAreas/GetRelevantGridAreasHandler.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/GridAreas/GetRelevantGridAreasHandler.cs index 0c105d248..3fa7fda4b 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/GridAreas/GetRelevantGridAreasHandler.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Application/Handlers/GridAreas/GetRelevantGridAreasHandler.cs @@ -48,7 +48,7 @@ public async Task Handle(GetRelevantGridAreasCommand reque var endDate = request.Period.End.ToDateTimeOffset(); var gridAreas = await _gridAreaRepository.GetAsync().ConfigureAwait(false); - var filteredByDateGridAreas = gridAreas.Where(ga => IsRelevantGridArea(ga, startDate, endDate)); + var filteredByDateGridAreas = gridAreas.Where(ga => DoDatesOverlap(ga, startDate, endDate)); var actor = await _actorRepository.GetAsync(new ActorId(_userContext.CurrentUser.ActorId)).ConfigureAwait(false); NotFoundValidationException.ThrowIfNull(actor, _userContext.CurrentUser.ActorId); @@ -65,15 +65,18 @@ public async Task Handle(GetRelevantGridAreasCommand reque gridArea.ValidTo))); } - private static bool IsRelevantGridArea(GridArea gridArea, DateTimeOffset startDate, DateTimeOffset endDate) + private static bool DoDatesOverlap(GridArea gridArea, DateTimeOffset startDate, DateTimeOffset endDate) { + var convertedStartDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(startDate, "Romance Standard Time"); + var convertedEndDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(endDate.AddMilliseconds(-1), "Romance Standard Time"); + if (!gridArea.ValidTo.HasValue) { - return gridArea.ValidFrom <= endDate; + return gridArea.ValidFrom <= convertedEndDate; } // formula from https://www.baeldung.com/java-check-two-date-ranges-overlap - var overlap = Math.Min(gridArea.ValidTo.Value.Ticks, endDate.Ticks) - Math.Max(gridArea.ValidFrom.Ticks, startDate.Ticks); + var overlap = Math.Min(gridArea.ValidTo.Value.Ticks, convertedEndDate.Ticks) - Math.Max(gridArea.ValidFrom.Ticks, convertedStartDate.Ticks); return overlap >= 0; } } diff --git a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Handlers/GetRelevantGridAreasHandlerTests.cs b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Handlers/GetRelevantGridAreasHandlerTests.cs index 46ae8f551..07cae9d78 100644 --- a/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Handlers/GetRelevantGridAreasHandlerTests.cs +++ b/source/marketparticipant/Energinet.DataHub.MarketParticipant.Tests/Handlers/GetRelevantGridAreasHandlerTests.cs @@ -157,7 +157,7 @@ public async Task Handle_ValidDates_ReturnsGridAreas(string validFrom, string? v [Theory] [InlineData("01/01/2023 00:00:00", "31/12/2025 23:59:59", "01/01/2022 00:00:00", "31/01/2022 23:59:59")] - [InlineData("01/01/2023 00:00:00", "31/12/2025 23:59:59", "01/01/2027 00:00:00", "31/01/2027 23:59:59")] + [InlineData("01/01/2023 00:00:00", "31/12/2025 23:59:59", "01/01/2026 00:00:00", "31/01/2026 23:59:59")] [InlineData("01/01/2027 00:00:00", null, "31/12/2025 23:59:59", "31/01/2026 23:59:59")] public async Task Handle_InvalidDates_ReturnsEmpty(string validFrom, string? validTo, string periodStart, string periodEnd) {