MCPcopy Create free account
hub / github.com/evcc-io/evcc / AverageRate

Function AverageRate

tariff/tariffs.go:48–71  ·  view source on GitHub ↗

AverageRate returns the arithmetic mean of rates in [now, now+d), or nil if unavailable.

(t api.Tariff, d time.Duration)

Source from the content-addressed store, hash-verified

46
47// AverageRate returns the arithmetic mean of rates in [now, now+d), or nil if unavailable.
48func AverageRate(t api.Tariff, d time.Duration) *float64 {
49 rr := Rates(t)
50 if rr == nil {
51 return nil
52 }
53
54 now := time.Now()
55 end := now.Add(d)
56
57 var sum float64
58 var count int
59 for _, r := range rr {
60 if r.Start.Before(end) && r.End.After(now) {
61 sum += r.Value
62 count++
63 }
64 }
65
66 if count == 0 {
67 return nil
68 }
69
70 return new(sum / float64(count))
71}
72
73func (t *Tariffs) Get(u api.TariffUsage) api.Tariff {
74 // ensure tariff is not a wrapper

Callers 1

createSessionMethod · 0.92

Calls 2

RatesFunction · 0.70
AddMethod · 0.65

Tested by

no test coverage detected