Sessions API: filter tz handling (#29676)

This commit is contained in:
Michael Geers 2026-05-05 22:05:51 +02:00 • committed by GitHub
parent 1e614279ae
commit d8730e1a60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View file

@ -49,11 +49,11 @@ func sessionHandler(w http.ResponseWriter, r *http.Request) {
filename := "session"
if year := r.URL.Query().Get("year"); year != "" {
filename += "-" + year
push("STRFTIME('%Y', created) LIKE ?", year)
push("STRFTIME('%Y', created, 'localtime') LIKE ?", year)
if month := fmt.Sprintf("%02s", r.URL.Query().Get("month")); month != "00" {
filename += "-" + month
push("STRFTIME('%m', created) LIKE ?", month)
push("STRFTIME('%m', created, 'localtime') LIKE ?", month)
}
}

View file

@ -0,0 +1,39 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/evcc-io/evcc/core/session"
"github.com/evcc-io/evcc/server/db"
"github.com/stretchr/testify/require"
)
func TestSessionHandlerTimezoneFilter(t *testing.T) {
t.Setenv("TZ", "Europe/Berlin")
loc, err := time.LoadLocation("Europe/Berlin")
require.NoError(t, err)
require.NoError(t, db.NewInstance("sqlite", ":memory:"))
require.NoError(t, db.Instance.AutoMigrate(new(session.Session)))
// 2026-05-01 00:01 CEST = 2026-04-30 22:01 UTC: local month=May, UTC month=April
ts := time.Date(2026, 5, 1, 0, 1, 0, 0, loc)
require.NoError(t, db.Instance.Create(&session.Session{
Created: ts,
Finished: ts.Add(time.Hour),
ChargedEnergy: 1.0,
}).Error)
req := httptest.NewRequest(http.MethodGet, "/?year=2026&month=5", nil)
rec := httptest.NewRecorder()
sessionHandler(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
var got session.Sessions
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
require.Len(t, got, 1)
}