-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
rates.go
40 lines (33 loc) · 824 Bytes
/
rates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package api
import (
"errors"
"slices"
"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
// Sort rates by start time
func (r Rates) Sort() {
slices.SortStableFunc(r, func(i, j Rate) int {
return i.Start.Compare(j.Start)
})
}
// 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")
}