From e867ef60cffe45215cad911d4b4aa3e7449d0491 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 25 Jul 2026 11:36:28 +0000 Subject: [PATCH] Tariff: fix chargesZones boundaries not taking effect between price zones (#32143) --- tariff/fixed.go | 4 +++- tariff/fixed_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tariff/fixed.go b/tariff/fixed.go index 705b9a787..fefca4e2f 100644 --- a/tariff/fixed.go +++ b/tariff/fixed.go @@ -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())) diff --git a/tariff/fixed_test.go b/tariff/fixed_test.go index 6be7603d1..56151b33e 100644 --- a/tariff/fixed_test.go +++ b/tariff/fixed_test.go @@ -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) +}