chore: drop dependency on carbon library (#10307)

This commit is contained in:
andig 2023-10-14 10:42:40 +02:00 • committed by GitHub
parent 7ecf7e6b55
commit 31bf0d193b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 28 deletions

3
go.mod
View file

@ -36,7 +36,6 @@ require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/godbus/dbus/v5 v5.1.0
github.com/gokrazy/updater v0.0.0-20230215172637-813ccc7f21e2
github.com/golang-module/carbon/v2 v2.2.8
github.com/golang/mock v1.6.0
github.com/google/go-github/v32 v32.1.0
github.com/google/uuid v1.3.1
@ -51,6 +50,7 @@ require (
github.com/itchyny/gojq v0.12.13
github.com/jeremywohl/flatten v1.0.1
github.com/jinzhu/copier v0.4.0
github.com/jinzhu/now v1.1.5
github.com/joeshaw/carwings v0.0.0-20230425210048-195f7e10e966
github.com/joho/godotenv v1.5.1
github.com/jpfielding/go-http-digest v0.0.0-20211006141426-fbc93758452e
@ -145,7 +145,6 @@ require (
github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect

2
go.sum
View file

@ -297,8 +297,6 @@ github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gokrazy/updater v0.0.0-20230215172637-813ccc7f21e2 h1:kBY5R1tSf+EYZ+QaSrofLaVJtBqYsVNVBWkdMq3Smcg=
github.com/gokrazy/updater v0.0.0-20230215172637-813ccc7f21e2/go.mod h1:PYOvzGOL4nlBmuxu7IyKQTFLaxr61+WPRNRzVtuYOHw=
github.com/golang-module/carbon/v2 v2.2.8 h1:a1VxHHKAR7fc1ho7sYXhS1s5S4x7+oqAf2EY5p8C46A=
github.com/golang-module/carbon/v2 v2.2.8/go.mod h1:XDALX7KgqmHk95xyLeaqX9/LJGbfLATyruTziq68SZ8=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=

View file

@ -3,12 +3,13 @@ package tariff
import (
"fmt"
"sort"
"time"
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/tariff/fixed"
"github.com/evcc-io/evcc/util"
"github.com/golang-module/carbon/v2"
"github.com/jinzhu/now"
)
type Fixed struct {
@ -83,20 +84,20 @@ func NewFixedFromConfig(other map[string]interface{}) (api.Tariff, error) {
func (t *Fixed) Rates() (api.Rates, error) {
var res api.Rates
start := carbon.CreateFromStdTime(t.clock.Now().Local()).StartOfDay()
start := now.With(t.clock.Now().Local()).BeginningOfDay()
for i := 0; i < 7; i++ {
dow := fixed.Day((start.DayOfWeek() + i) % 7)
dow := fixed.Day((int(start.Weekday()) + i) % 7)
zones := t.zones.ForDay(dow)
if len(zones) == 0 {
return nil, fmt.Errorf("no zones for weekday %d", dow)
}
dayStart := start.AddDays(i)
dayStart := start.AddDate(0, 0, i)
markers := zones.TimeTableMarkers()
for i, m := range markers {
ts := dayStart.AddMinutes(m.Minutes())
ts := dayStart.Add(time.Minute * time.Duration(m.Minutes()))
var zone *fixed.Zone
for j := len(zones) - 1; j >= 0; j-- {
@ -111,15 +112,15 @@ func (t *Fixed) Rates() (api.Rates, error) {
}
// end rate at end of day or next marker
end := dayStart.AddDay()
end := dayStart.AddDate(0, 0, 1)
if i+1 < len(markers) {
end = dayStart.AddMinutes(markers[i+1].Minutes())
end = dayStart.Add(time.Minute * time.Duration(markers[i+1].Minutes()))
}
rate := api.Rate{
Price: zone.Price,
Start: ts.ToStdTime(),
End: end.ToStdTime(),
Start: ts,
End: end,
}
res = append(res, rate)

View file

@ -2,11 +2,12 @@ package tariff
import (
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/tariff/fixed"
"github.com/golang-module/carbon/v2"
"github.com/jinzhu/now"
"github.com/stretchr/testify/assert"
)
@ -20,13 +21,13 @@ func TestFixed(t *testing.T) {
var expect api.Rates
for dow := 0; dow < 7; dow++ {
dayStart := carbon.CreateFromStdTime(tf.clock.Now()).StartOfDay().AddDays(dow)
dayStart := now.With(tf.clock.Now()).BeginningOfDay().AddDate(0, 0, dow)
for hour := 0; hour < 24; hour++ {
expect = append(expect, api.Rate{
Price: 0.3,
Start: dayStart.AddHours(hour).ToStdTime(),
End: dayStart.AddHours(hour + 1).ToStdTime(),
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
}
@ -53,37 +54,37 @@ func TestFixedSplitZones(t *testing.T) {
var expect api.Rates
for i := 0; i < 7; i++ {
dayStart := carbon.CreateFromStdTime(tf.clock.Now()).StartOfDay().AddDays(i)
dayStart := now.With(tf.clock.Now()).BeginningOfDay().AddDate(0, 0, i)
// 00:00-05:00 0.1
for hour := 0; hour < 5; hour++ {
expect = append(expect, api.Rate{
Price: 0.1,
Start: dayStart.AddHours(hour).ToStdTime(),
End: dayStart.AddHours(hour + 1).ToStdTime(),
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
// 05:00-05:30 0.1
expect = append(expect, api.Rate{
Price: 0.1,
Start: dayStart.AddHours(5).ToStdTime(),
End: dayStart.AddHours(5).AddMinutes(30).ToStdTime(),
Start: dayStart.Add(5 * time.Hour),
End: dayStart.Add(5*time.Hour + 30*time.Minute),
})
// 05:30-06:00 0.5
expect = append(expect, api.Rate{
Price: 0.5,
Start: dayStart.AddHours(5).AddMinutes(30).ToStdTime(),
End: dayStart.AddHours(6).ToStdTime(),
Start: dayStart.Add(5*time.Hour + 30*time.Minute),
End: dayStart.Add(6 * time.Hour),
})
// 06:00-21:00 0.5
for hour := 6; hour < 21; hour++ {
expect = append(expect, api.Rate{
Price: 0.5,
Start: dayStart.AddHours(hour).ToStdTime(),
End: dayStart.AddHours(hour + 1).ToStdTime(),
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
@ -91,8 +92,8 @@ func TestFixedSplitZones(t *testing.T) {
for hour := 21; hour < 24; hour++ {
expect = append(expect, api.Rate{
Price: 0.1,
Start: dayStart.AddHours(hour).ToStdTime(),
End: dayStart.AddHours(hour + 1).ToStdTime(),
Start: dayStart.Add(time.Hour * time.Duration(hour)),
End: dayStart.Add(time.Hour * time.Duration(hour+1)),
})
}
}