Tariff: fix chargesZones boundaries not taking effect between price zones (#32143)

This commit is contained in:
github-actions[bot] 2026-07-25 11:36:28 +00:00 • committed by GitHub
parent 75eebf131b
commit e867ef60cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package tariff
import (
"fmt"
"slices"
"sort"
"time"
@ -77,7 +78,8 @@ func (t *Fixed) Rates() (api.Rates, error) {
return nil, fmt.Errorf("no zones for weekday %d", dow)
}
markers := zones.TimeTableMarkers()
// include chargesZones boundaries so rate changes there are not swallowed by the coarser price zone markers
markers := append(slices.Clone(zones), t.chargesZones.ForDayAndMonth(dow, month)...).TimeTableMarkers()
for i, m := range markers {
ts := dayStart.Add(time.Minute * time.Duration(m.Minutes()))

View file

@ -104,3 +104,32 @@ func TestFixedSplitZones(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, expect, rates)
}
func TestFixedChargesZonesMarkers(t *testing.T) {
at, err := NewFixedFromConfig(map[string]any{
"price": 0.5,
"chargesZones": []struct {
Charges float64
Hours string
}{
{0.1, "0-5:30"},
},
})
require.NoError(t, err)
tf := at.(*Fixed)
tf.clock = clock.NewMock()
rates, err := tf.Rates()
require.NoError(t, err)
dayStart := now.With(tf.clock.Now()).BeginningOfDay()
// charges zone ends mid-hour at 05:30 - the price change must take effect there, not at 06:00
require.GreaterOrEqual(t, len(rates), 2)
assert.Equal(t, dayStart.Add(5*time.Hour), rates[5].Start)
assert.Equal(t, dayStart.Add(5*time.Hour+30*time.Minute), rates[5].End)
assert.Equal(t, 0.6, rates[5].Value)
assert.Equal(t, dayStart.Add(5*time.Hour+30*time.Minute), rates[6].Start)
assert.Equal(t, 0.5, rates[6].Value)
}