evcc-io/api/rates.go
andig 7c3ca53f02
Add dedicated co2 tariff (#8089)
---------

Co-authored-by: Michael Geers <michael@geers.tv>
2023-05-31 09:19:36 +02:00

32 lines
673 B
Go

package api
import (
"errors"
"time"
)
// Rate is a grid tariff rate
type Rate struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
Price float64 `json:"price"`
}
// IsEmpty returns is the rate is the zero value
func (r Rate) IsEmpty() bool {
return r.Start.IsZero() && r.End.IsZero() && r.Price == 0
}
// Rates is a slice of (future) tariff rates
type Rates []Rate
// Current returns the rates current rate or error
func (r Rates) Current(now time.Time) (Rate, error) {
for _, rr := range r {
if (rr.Start.Before(now) || rr.Start.Equal(now)) && rr.End.After(now) {
return rr, nil
}
}
return Rate{}, errors.New("no matching rate")
}