Skip to content

Commit

Permalink
fix scenario in FixedPriceWeekly provider where the timezone jumps to…
Browse files Browse the repository at this point in the history
… the previous day
  • Loading branch information
MattJeanes committed Nov 5, 2024
1 parent 4fa6523 commit 4fe3eb0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
23 changes: 22 additions & 1 deletion TeslaMateAgile.Tests/Services/FixedPriceWeeklyServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ private FixedPriceWeeklyService Setup(string timeZone, List<string> prices)
new List<Price>
{
// Monday
new() { ValidFrom = new DateTimeOffset(new DateTime(2023, 1, 1, 5, 0, 0, DateTimeKind.Utc)), ValidTo = new DateTimeOffset(new DateTime(2023, 1, 2, 5, 0, 0, DateTimeKind.Utc)), Value = 0.042m },
new() { ValidFrom = new DateTimeOffset(new DateTime(2023, 1, 2, 5, 0, 0, DateTimeKind.Utc)), ValidTo = new DateTimeOffset(new DateTime(2023, 1, 2, 8, 30, 0, DateTimeKind.Utc)), Value = 0.04m },
new() { ValidFrom = new DateTimeOffset(new DateTime(2023, 1, 2, 8, 30, 0, DateTimeKind.Utc)), ValidTo = new DateTimeOffset(new DateTime(2023, 1, 2, 11, 0, 0, DateTimeKind.Utc)), Value = 0.035m },
new() { ValidFrom = new DateTimeOffset(new DateTime(2023, 1, 2, 11, 0, 0, DateTimeKind.Utc)), ValidTo = new DateTimeOffset(new DateTime(2023, 1, 2, 13, 0, 0, DateTimeKind.Utc)), Value = 0.02m },
Expand Down Expand Up @@ -256,7 +257,27 @@ private FixedPriceWeeklyService Setup(string timeZone, List<string> prices)
new DateTimeOffset(new DateTime(2024, 11, 5, 20, 50, 33, DateTimeKind.Utc)),
new List<Price>
{
new() { ValidFrom = new DateTimeOffset(new DateTime(2024, 11, 5, 17, 00, 00, DateTimeKind.Utc)), ValidTo = new DateTimeOffset(new DateTime(2024, 11, 5, 22, 0, 0, DateTimeKind.Utc)), Value = 0.052004m },
new() { ValidFrom = new DateTimeOffset(new DateTime(2024, 11, 5, 17, 0, 0, DateTimeKind.Utc)), ValidTo = new DateTimeOffset(new DateTime(2024, 11, 5, 22, 0, 0, DateTimeKind.Utc)), Value = 0.052004m },
}
},
new object[]
{
"TimeZonePreviousDay",
"America/Denver",
new List<string>
{
"Mon-Fri=00:00-08:00=0.052004",
"Mon-Fri=08:00-10:00=0.253532",
"Mon-Fri=10:00-15:00=0.052004",
"Mon-Fri=15:00-20:00=0.253532",
"Mon-Fri=20:00-00:00=0.052004",
"Sat-Sun=0.052004"
},
new DateTimeOffset(new DateTime(2024, 11, 4, 1, 22, 55, DateTimeKind.Utc)),
new DateTimeOffset(new DateTime(2024, 11, 4, 1, 40, 58, DateTimeKind.Utc)),
new List<Price>
{
new() { ValidFrom = new DateTimeOffset(new DateTime(2024, 11, 3, 6, 0, 0, DateTimeKind.Utc)), ValidTo = new DateTimeOffset(new DateTime(2024, 11, 4, 7, 0, 0, DateTimeKind.Utc)), Value = 0.052004m },
}
}
};
Expand Down
7 changes: 4 additions & 3 deletions TeslaMateAgile/Services/FixedPriceWeeklyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public Task<IEnumerable<Price>> GetPriceData(DateTimeOffset from, DateTimeOffset

// Get all days between the range inclusive

var fromDate = from.Date;
var toDate = to.Date;
var fromDate = from.Add(_timeZone.GetUtcOffset(from)).Date;
var toDate = to.Add(_timeZone.GetUtcOffset(to)).Date;
var days = new Dictionary<DateTimeOffset, List<Price>>();
for (var date = from.Date; date <= to.Date; date = date.AddDays(1))
for (var date = fromDate; date <= toDate; date = date.AddDays(1))
{
prices.AddRange(GetPriceDataForDate(date));
}
Expand All @@ -40,6 +40,7 @@ public Task<IEnumerable<Price>> GetPriceData(DateTimeOffset from, DateTimeOffset

prices = prices.Where(x => x.ValidFrom < to && x.ValidTo > from).ToList();


return Task.FromResult((IEnumerable<Price>)prices);
}

Expand Down

0 comments on commit 4fe3eb0

Please sign in to comment.